Asp.Net MVC3: Set custom IServiceProvider in ValidationContext so validators can resolve services

On MVC 5.2, you can leveragesteal @Andras’s answer and the MVC source and: 1. Derive a DataAnnotationsModelValidatorEx from DataAnnotationsModelValidator namespace System.Web.Mvc { // From https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/DataAnnotationsModelValidator.cs // commit 5fa60ca38b58, Apr 02, 2015 // Only diff is adding of secton guarded by THERE_IS_A_BETTER_EXTENSION_POINT public class DataAnnotationsModelValidatorEx : DataAnnotationsModelValidator { readonly bool _shouldHotwireValidationContextServiceProviderToDependencyResolver; public DataAnnotationsModelValidatorEx( ModelMetadata metadata, ControllerContext … Read more

Using Simple Injector with Unit Of Work & Repository Pattern in Windows Form

The problem you have is the difference in lifestyles between your service, repository, unitofwork and dbcontext. Because the MemberRepository has a Singleton lifestyle, Simple Injector will create one instance which will be reused for the duration of the application, which could be days, even weeks or months with a WinForms application. The direct consequence from … Read more

Which Dependency Injection Tool Should I Use? [closed]

Having recently spiked the use of 6 of these (Windsor, Unity, Spring.Net, Autofac, Ninject, StructureMap) I can offer a quick summary of each, our selection criteria and our final choice. Note: we did not look at PicoContainer.Net as one of our team considered the .Net port to be quite poor from the Java version. We … Read more

IoC / Dependency Injection – please explain code versus XML

Hot-swapping dependencies is not the only goal of using a DI Container. Dependency Injection (DI) is a principle that helps us develop loosely coupled code. Loose coupling only means that we can vary consumer and service independently of each other. How we achieve this is not addressed at this level. DI Containers are frameworks that … Read more

Dependency Injection – new instance required in several of a classes methods

Most of the answers here so far suggest that you change the injected dependency type to some sort of Abstract Factory (a Func<T> is also an Abstract Factory) to address the issue. However, if you do that it would be a leaky abstraction because you would let the knowledge of a particular implementation determine the … Read more

With Unity how do I inject a named dependency into a constructor?

You can configure dependencies with or without names in the API, attributes, or via the config file. You didn’t mention XML above, so I’ll assume you’re using the API. To tell the container to resolve a named dependency, you’ll need to use an InjectionParameter object. For your ClientModel example, do this: container.RegisterType<IClientModel, ClientModel>( new InjectionConstructor( … Read more

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

tech