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

How to do dependency injection for ASP.NET Web FormsSystem.Web.UI.UserControl (.ascx) #770

Open
dworthem440 opened this issue Nov 15, 2019 · 12 comments
Labels

Comments

@dworthem440
Copy link

@dworthem440 dworthem440 commented Nov 15, 2019

Do you have sample code to register the user controls to enable dependency injection? I imagine it would have to be property injection like the web pages.

@dworthem440

This comment has been minimized.

Copy link
Author

@dworthem440 dworthem440 commented Nov 15, 2019

I should clarify that I already followed the examples to allow dependency injection on the pages.

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Nov 15, 2019

Although it is possible to walk the page control hierarchy and look for UserControls and initialize them the same way as the documentation shows how to initialize page classes, I would instead go with the Humble Object approach.

What this means is that you should view your UserControl as a Humble Object, a thin layer that dispatches everything to a 'real' service. In other words, hide all the control's dependencies and their logic behind a new service and resolve that service directly from Simple Injector inside that user control. Let the user control then invoke that resolved service.

@dworthem440

This comment has been minimized.

Copy link
Author

@dworthem440 dworthem440 commented Nov 15, 2019

How do I resolve the service directly from Simple Injector? Do I have to have global access available to the Container instance initialized during Application_Start?

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Nov 15, 2019

Yes, you do have access when you store the Container in a static field inside your Global :)

@dworthem440

This comment has been minimized.

Copy link
Author

@dworthem440 dworthem440 commented Nov 15, 2019

I can do that, was trying to avoid that. I'll also try registering the user controls. Do you have any example of code for that or is it almost identical to that for the pages?

@dworthem440

This comment has been minimized.

Copy link
Author

@dworthem440 dworthem440 commented Nov 15, 2019

Perhaps the global access might be good in cases where we might want to resolve a "service" outside a hierarchy of dependencies

@dworthem440

This comment has been minimized.

Copy link
Author

@dworthem440 dworthem440 commented Nov 16, 2019

I tried code very similar to that in https://github.com/simpleinjector/SimpleInjector/blob/v3.3.x/src/SimpleInjector.Integration.Web.WebForms/SimpleInjectorWebFormsHttpModule.cs.
I have a public property with the the ImportAttribute in a UserControl with "StopGap" in the type name but when loaded DI does not occur into the property in the user control. Can you take a quick glance at the code to provide what might be missing. Files Attached.
DependencyInjectionInitializer.txt
Global.txt
PageInitializerModule.txt

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Nov 17, 2019

Hi @dworthem440,

The SimpleInjector.Integration.Web.WebForms has long been a project of the Simple Injector solution that was meant to become an official integration library, but never made it to become an official library. This has to do with the legacy status of Web Forms and our lack of interest and time in spending much time in maintaining and supporting such library. Still, that code base might be an interesting starting point for you to try applying DI to user controls—most of the complexity in that library focusses on allowing to initialize user controls.

I did a quick pass over your code, but can't give you any hints. Unfortunately, I lack the time to do a thorough dive into your code. I'm sorry about that.

@dworthem440

This comment has been minimized.

Copy link
Author

@dworthem440 dworthem440 commented Nov 17, 2019

I had heard or read somewhere that Microsoft is going to include Web Forms in .NET Core. Have you considered that? Does it have the same page pipeline as in legacy ASP.NET Web Forrms?

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Nov 17, 2019

I have no idea what Microsoft's plans are with Web Forms concerning .NET Core. But even if they do so, Web Forms is still a legacy product. The number of DI questions I get that involve Web Forms has been very low since I started creating Simple Injector years bacj. And it only declined ever since. This is one reason why it hasn't got my attention, and this won't change.

@dworthem440

This comment has been minimized.

Copy link
Author

@dworthem440 dworthem440 commented Nov 17, 2019

Ok, well at least we have the global access to the Container object.

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Nov 25, 2019

btw, if you're using ASP.NET v4.7.2 or higher, you can use the new HttpRuntime.WebObjectActivator hook. I'm unsure whether that would initialize User Controls as well, but that is easy to find out. Here is a StackOverflow q/a that describes hows to implement a Simple Injector-specific IServiceProvider provider implementation for ASP.NET Web Forms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.