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

The best way to add injection #1

Closed
tossnet opened this issue Sep 16, 2021 · 8 comments
Closed

The best way to add injection #1

tossnet opened this issue Sep 16, 2021 · 8 comments
Labels
enhancement New feature or request

Comments

@tossnet
Copy link
Contributor

tossnet commented Sep 16, 2021

I always wonder where to put my injections...

  1. in the RAZOR page (@Inject ...)
  2. in the partial class ([injec] ...

What is your opinion?

@dvoituron
Copy link
Collaborator

In my opinion, I prefer to put all the code in the "Code Behind" file.
And therefore, to put the injection in the .cs.
The .razor is only used for the design part and the layout of this HTML design (via loops and Razor tests)

@dvoituron dvoituron added the enhancement New feature or request label Sep 17, 2021
@dvoituron
Copy link
Collaborator

Do you inject in private or public properties?

  • private Factory Factory { get; set; }
  • public Factory Factory { get; set; }

@tossnet
Copy link
Contributor Author

tossnet commented Sep 17, 2021

in my case, all in private

@dvoituron
Copy link
Collaborator

So, a best practice will be

[Inject]
private IMemoryCache MemoryCache { get; set; }

@tossnet
Copy link
Contributor Author

tossnet commented Sep 17, 2021

strangely, I don't like the BR ^^
image
and you ?

@dvoituron
Copy link
Collaborator

In my best practices, all attributes are above of the property/method... so, including [Inject]
The first reason is, because you can have multiple attributes : serialization, authorizations, decoration, injection, ...

@tossnet
Copy link
Contributor Author

tossnet commented Sep 17, 2021

OK, Right ! I'll change my code
I need to use #REGION now... :/
image

@dvoituron
Copy link
Collaborator

It's for this reason, I'm using a Factory pattern to include all service references into a Factory class.
So, in my component / page, when I need to use a service of my project, I inject Factory and I use the property Factory.Profiles.

Factory class

public virtual ProfileService Profiles => _profileService ?? (_profileService = new ProfileService(this));

public virtual AccountingService Accounting => _accountingService ?? (_accountingService = new AccountingService(this));

If the service is provided my Microsoft or external library, I inject the service directly to my component.
The Factory is used only for my services.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants