How do you reconcile IDisposable and IoC?

You definitely do not want to call Dispose() on an object that was injected into your class. You can’t make the assumption that you are the only consumer. Your best bet is to wrap your unmanaged object in some managed interface: public class ManagedFileReader : IManagedFileReader { public string Read(string path) { using (StreamReader reader … Read more

Unity InjectionConstructor for multiparam constructor overriding only single one

Unity can’t do this out of the box. The best you can do is: container.RegisterType<Bar>( new InjectionConstructor( typeof(IFoo), typeof(IFoo2), typeof(IFoo3), typeof(IFooN), “123”)); Or you can use the SmartConstructor provided by the TecX project. This blog post describes some background. Registration would look like this: container.RegisterType<Bar>(new SmartConstructor(“text”, “123”));

Exception is: InvalidOperationException – The current type, is an interface and cannot be constructed. Are you missing a type mapping?

Just for others (like me) who might have faced the above error. The solution in simple terms. You might have missed to register your Interface and class (which implements that inteface) registration in your code. e.g if the error is “The current type, xyznamespace. Imyinterfacename, is an interface and cannot be constructed. Are you missing … Read more

Singleton Per Call Context (Web Request) in Unity

Neat solution, but each instance of LifetimeManager should use a unique key rather than a constant: private string _key = string.Format(“PerCallContextLifeTimeManager_{0}”, Guid.NewGuid()); Otherwise if you have more than one object registered with PerCallContextLifeTimeManager, they’re sharing the same key to access CallContext, and you won’t get your expected object back. Also worth implementing RemoveValue to ensure … Read more

Register IAuthenticationManager with Unity

Here is what I did to make Unity play nice with ASP.NET Identity 2.0: I added the following to the RegisterTypes method in the UnityConfig class: container.RegisterType<DbContext, ApplicationDbContext>( new HierarchicalLifetimeManager()); container.RegisterType<UserManager<ApplicationUser>>( new HierarchicalLifetimeManager()); container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>( new HierarchicalLifetimeManager()); container.RegisterType<AccountController>( new InjectionConstructor());

Is it better to create a singleton to access unity container or pass it through the application? [closed]

The correct approach to DI is to use Constructor Injection or another DI pattern (but Constructor Injection is the most common) to inject the dependencies into the consumer, irrespective of DI Container. In your example, it looks like you require the dependencies TestSuite and TestCase, so your TestSuiteParser class should statically announce that it requires … Read more