-
Notifications
You must be signed in to change notification settings - Fork 152
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
@inject razor views #362
Comments
UPDATE: There is Blazor Server App integration guide in the Simple Injector documentation.
|
After some investigation, I have to come to the conclusion that ASP.NET Core MVC currently lacks the proper hook that allows intercepting injection of Razor View Properties (i.e. services that are injected using You can see that the way to resolve properties is hard-wired into the The only way to intercept this is by replacing the default This design flaw means that the only way to add this behavior is to copy all the code from the original This solution however isn't really feasible, because it causes a severe 'synchronization burden' on you, because you will have to keep these three files in sync with any possible changes and bug fixes that Microsoft will do on these three classes itself. This does mean that the statements I made months ago about MVC having all the required interception points in place, where unfortunately false. This is an omission in the design of MVC. But as I said previously, IMO from a design perspective, views should not have any dependencies; not even for "localization or data required only for populating view elements". This is better done at the controller level (or perhaps at a level between the controller and the view, by using decoration or interception to enrich this data). This keeps the views truly dumb. |
Thanks for looking into this and I agree with you that views should be kept simple. I have a shared layout that displays some information about the current user and I don't want every controller to get this information and "push" it to the layout. But for now I think I can solve it with a view component that does support constructor injection. As a last resort I could cross-wire the asp.mvc container to make use of the simple injector container to resolve dependencies for the views. Something like:
|
That isn't that hacky at all, I must say. What you are doing is called "cross-wiring" and is almost always required when to run an application-specific container besides the built-in framework container. You sometimes have to cross-wire registrations from one side to the other. You want to keep the number of cross-wired dependencies (on both sides) to a minimum, but you will always need a few. |
ASP.NET Core 5.0 will allow the creation of Razor Components to be intercepted. Here's how to integrate it with Simple Injector. |
@irmtim, I finally had some time to deep deeper into this, and unfortunately due to current limitations of Blazor (.NET 5.0) it is practically impossible to use the |
There is Blazor Server App integration guide in the Simple Injector documentation. |
Good job on the migration to .net core!
Will there be support for @Inject inside razors views? Or did I miss something?
As explained here
The text was updated successfully, but these errors were encountered: