Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IApplication #3863

Open
tznind opened this issue Nov 27, 2024 · 3 comments
Open

Add IApplication #3863

tznind opened this issue Nov 27, 2024 · 3 comments
Labels
enhancement v2 For discussions, issues, etc... relavant for v2

Comments

@tznind
Copy link
Collaborator

tznind commented Nov 27, 2024

Is your feature request related to a problem? Please describe.
Currently Application is almost completely static methods and properties. This prevents defining alternative implementations and tests cannot Moq expectations of the static methods their members invoke.

Describe the solution you'd like

  1. Change all Application methods to not be static (adding virtual where it makes sense)
  2. Implement Singleton pattern i.e. everyone accesses Application.Instance which is an IApplication
  3. Update all call sites

This will enable adding alternative implementations to IApplication e.g. #3837

To do this I could for example subclass Application but override certain methods, or implement IApplication from scratch. We would then change the instance in Program.cs (i.e. at start of program)

// This would throw unless the current instance is Shutdown or not yet Init
Application.ChangeInstance(myAltApplicationClass)

Describe alternatives you've considered
An alternative is to keep public API how it is and add the singleton pattern behind the static methods i.e. the static methods all forward to an internal singleton. This would be more code to write but would look prettier for the user.

Additional context
Add any other context or screenshots about the feature request here.

@tznind tznind added enhancement v2 For discussions, issues, etc... relavant for v2 labels Nov 27, 2024
@tig
Copy link
Collaborator

tig commented Nov 27, 2024

Big fan. Big!

@tig
Copy link
Collaborator

tig commented Nov 27, 2024

Only issue I think I have is 'Application.Instance.xyz' is the opposite of terse.

One solution is to name 'Instance' 'App' and add a global using for 'Application'.

'App.xyz'

@tznind
Copy link
Collaborator Author

tznind commented Nov 27, 2024

There is also this approach:

An alternative is to keep public API how it is and add the singleton pattern behind the static methods i.e. the static methods all forward to an internal singleton. This would be more code to write but would look prettier for the user.

But this means you have 4 copies of each method signature

  • In Interface IApplication
  • In static form on Application
  • In ApplicationImpl class - the one that implements IApplication and gets hosted as the singleton instance
  • In any derrived/alternative IApplication class

You also want the xmldoc on the statics and the IApplication members as they wont benefit from <inheritdoc/>

Still its probably the cleanest for external library user perspective.

@tznind tznind mentioned this issue Nov 30, 2024
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement v2 For discussions, issues, etc... relavant for v2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants