How to use Dependency Injection with ASP.NET Web Forms

UPDATE 2019: With the introduction of Web Forms 4.7.2, there is now better support for DI. This invalidates the below. See: Wiring up Simple Injector in WebForms in .NET 4.7.2 You can use automatic constructor injection by replacing the default PageHandlerFactory with a custom one. This way you can use an overloaded constructor to load … Read more

Arguments against Inversion of Control containers

There are certainly people who think that DI Containers add no benefit, and the question is valid. If you look at it purely from an object composition angle, the benefit of a container may seem negligible. Any third party can connect loosely coupled components. However, once you move beyond toy scenarios you should realize that … Read more

Castle Windsor are there any downsides?

To answer your questions: That is using reflection and each time that an object is called from the container, reflection must used so performance will be terrible. (Is this the case? does it use reflection on every call?) No, it does not. Most of the time it uses little reflection when you register the component. … Read more

How to deal with run-time parameters when using lifetime scoping?

Autofac now supports this out of the box with an extension to the lifetime scopes. The BeginLifetimeScope() method has an overload that takes an Action<ContainerBuilder> that allows for adding new registrations specific to only that lifetime scope. So for the given example it would look something like: var builder = new ContainerBuilder(); builder.RegisterType<MyService>().As<IMyService>().InstancePerLifetimeScope(); var container … Read more

When not to use IoC and DI? [closed]

About your question about having only one interface implementation. When you use IoC, it is still useful to use an interface. It will be much easier to create real unit tests (that doesn’t depend on the interface implementation to be working correctly) using mocks for these interfaces. The core of using IoC is making code … Read more

Proper Hub dependency lifetime management for SignalR and Castle Windsor

I’ve had a bit similar problem but with Unity instead of Castle Windsor. My requirements: I wanted to avoid singleton registrations on the container. All objects are resolved in Hub and should be disposed on Hub destruction. Registrations reused across Web Api and SignalR. Object lifetime is managed by HierarchicalLifetimeManager – child containers resolve and … Read more

Is Dependency Injection possible with a WPF application?

It’s actually very easy to do. We have examples of this in Prism as jedidja mentioned. You can either have the ViewModel get injected with the View or the View get injected with the ViewModel. In the Prism StockTraderRI, you will see that we inject the View into the ViewModel. Essentially, what happens is that … Read more

Autofac with multiple implementations of the same interface

Autofac implicitly supports this by default via the use of IEnumerable<T>. Instead of having your depending class’s constructor take in a single instance of T, you make it take in an instance of IEnumerable<T> that will contain every T registered: public interface IMessageHandler { void HandleMessage(Message m); } public class MessageProcessor { private IEnumerable<IMessageHandler> _handlers; … Read more

Base controller constructor injection in ASP.NET MVC with Unity

The first thing you have to understand is that you aren’t instantiating the base controller. You’re instantiating the child controller, which inherits the base controllers interface and functionality. This is an important distinction. When you say “the ChildControllers are not making use of the additional dependencies”, then you’re absolutely wrong. Because the ChildController IS the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)