unity-container

The right way to do property dependency injection using Unity

只谈情不闲聊 提交于 2019-12-13 01:16:43
问题 I have a class that needs a dependency injecting. As the class is already an implementation of another abstraction, and its 'sibling' implementations may not share the same dependencies, I am attempting to use property injection and not constructor injection. (All these classes/interface names are just for illustrative purposes) My IProvider abstraction: public interface IProvider { void ProviderMethod(); } My IProvider implementation (with the IData dependency I want to inject) : public

When is the Unity InjectionConstructor acually run?

混江龙づ霸主 提交于 2019-12-13 01:13:33
问题 I have the following code: IOC.Container.RegisterType<IRepository, GenericRepository> ("Customers", new InjectionConstructor(new CustomerEntities())); What I am wondering is if the new CustomerEntities() will be called once when the type registration happens OR if every time IRepository (with name "Customers") is resolved a new CustomerEntities will be made. If it is not the latter, then is there a way to make it work more like a delegate? (So it will make a new one every time it Resolves?) I

How to inject IEnumerable using Microsoft Unity IOC container

纵饮孤独 提交于 2019-12-12 13:28:29
问题 I have a Service that need inject more than one provider, see below for example. How to use Unity to implement this feature? public class MyService: IMyService { public MyService(IEnumerable<Provider> Providers); } 回答1: I know this is an old question, but maybe this will help someone else that stumbles upon this. As long as you register the implementations with a specific name, this is possible to easily inject. You will then get all registered implementations. public class MyService:

Reflecting over assemblies causes Unity to require Microsoft.Practices.ServiceLocation

霸气de小男生 提交于 2019-12-12 10:59:00
问题 We typically only reference Microsoft.Practices.Unity.dll in our applications. We're only using basic capabilities, and this works fine. In one application, the act of using reflection is causing Unity to require another DLL. For example, create a console app and reference only Microsoft.Practices.Unity (file version 2.0.414.0). Enter the following code and run it: class Program { static void Main() { using (var container = new UnityContainer()) { container.RegisterType<IDoSomething,

Unity Static Property Injection

我们两清 提交于 2019-12-12 10:48:50
问题 I have two classes, one which sets up the container by registering types and one which contains a static property which I want to inject into. My issue is the property is never set by injection so when I call a method on it, the property is always null. public class ClassOne { public void Method() { Container.RegisterType<IClass, ClassImplOne>("ImplOne"); Container.RegisterType<IClass, ClassImplTwo>("ImplTwo"); } } public static class ClassTwo { [Dependency] public static IClass SomeProperty

Repository Pattern universal application

拟墨画扇 提交于 2019-12-12 10:18:54
问题 When I started learning Repository Pattern with Unity few days ago I was under impression that the main benefit of this pattern is the separation of data layer from the business layer. In other words, if there is a need to change the way, how application stores the data, it's very easy as only one main model takes care of the communication. This means, that if application currently saves data into a serialized XML files, it would not be very difficult to change this logic to connect to

Including a generic class in Unity App.Config file

无人久伴 提交于 2019-12-12 09:35:14
问题 I have a class of type ISimpleCache<IBrokeredDataObject> that I want to add as a type alias (then a type) in the App.Config file the line <typeAlias alias="ISimpleCacheOfIBrokeredDataObject" type="MyApplication.ISimpleCache<IBrokeredDataObject>, MyApplication" /> is obviously wrong due to the <>, however I'm not convinced escaping them; <typeAlias alias="ISimpleCacheOfIBrokeredDataObject" type="MyApplication.ISimpleCache<IBrokeredDataObject>, MyApplication" /> is correct either. I am

Get All Types That Implement An Interface In Unity

亡梦爱人 提交于 2019-12-12 08:55:32
问题 Please skip to the UPDATE if you would like to just know the solution: I have an application that uses the following code to get and run a number of worker methods var type = typeof(IJob); var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(x => x.GetTypes()) .Where(x => x.IsClass && type.IsAssignableFrom(x)); foreach (Type t in types) { IJob obj = Activator.CreateInstance(t) as IJob; obj.Run(); } This code works perfectly as is. However, some of the newer jobs utilize dependency

How should I implement an MVC Bootstrapper for Unity and AutoMapper?

Deadly 提交于 2019-12-12 08:46:46
问题 What is the best way to create a bootstrapper for my MVC 2 app? I'm using Unity and AutoMapper and want to abstract the loading and configuration of them as much as possible. A decent example is here (http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-code-smell.aspx ), but UnityContainer implements IDisposable and in that example it is never cleaned up. This (Configuring Automapper in Bootstrapper violates Open-Closed Principle?) is

Custom ActionInvoker vs custom FilterProvider for ActionFilter dependency injection in MVC 3

时光总嘲笑我的痴心妄想 提交于 2019-12-12 08:39:40
问题 Can anyone shed some light on the advantages and disadvantages of using a custom ActionInvoker like so to perform dependency injection on custom ActionFilters as opposed to using a custom FilterProvider as demonstrated here? In both cases you are going to still want to avoid constructor injection on your ActionFilters, and to me it seems that all the custom FilterProvider does in this case is adds additional overhead of having to register your ActionFilters and the provider in your container.