unity-container

Resolving classes without registering them using Castle Windsor

陌路散爱 提交于 2019-11-27 08:53:34
Take the following useless program: class Program { static void Main(string[] args) { IUnityContainer unityContainer = new UnityContainer(); IWindsorContainer windsorContainer = new WindsorContainer(); Program unityProgram = unityContainer.Resolve<Program>(); Program castleProgram = windsorContainer.Resolve<Program>(); } } The UnityContainer will return me an instance of Program, where as the Windsor container will throw a ComponentNotFoundException. I can see arguments for both behaviours and don't mind which I end up with, however Prism V2 Drop 8 (the latest at time of writing) relies on the

Configure Unity DI for ASP.NET Identity

强颜欢笑 提交于 2019-11-27 08:21:33
I'm using Unity successfully for all regular constructor injection such as repositories etc., but I can't get it working with the ASP.NET Identity classes. The setup is this: public class AccountController : ApiController { private UserManager<ApplicationUser> _userManager { get; set; } public AccountController(UserManager<ApplicationUser> userManager) { if (userManager == null) { throw new ArgumentNullException("userManager"); } _userManager = userManager; } // ... } with these configs for Unity: unity.RegisterType<AccountController>(); unity.RegisterType<UserManager<ApplicationUser>>(new

Unity.AutoRegistration ?? Convention Based Registration Unity

天涯浪子 提交于 2019-11-27 08:01:20
问题 My code is as follows IUnityContainer container = new UnityContainer(); container .ConfigureAutoRegistration() .LoadAssemblyFrom(typeof(Test).Assembly.Location) .LoadAssemblyFrom(typeof(ITest).Assembly.Location) .ApplyAutoRegistration(); This is my first question. I'm not sure whether I have used the LoadAssemblyFrom method correctly here: ITest test = container.Resolve<ITest>(); When I try to compile I get the exception "ResolutionFailedException". What am I doing wrong? Thanks for your time

Unity.MVC4 lazy<T> is not working in ASP.NET MVC app

孤街醉人 提交于 2019-11-27 07:13:08
问题 I am using ASP.NET MVC 4 application. Home controller’s constructor is parameterized with 2 parameter(Iservice1 service1, Iservice2 service2) Not all the code path uses any of the Service (service1, service2), only in some code path I need service1 instance/object or service2 instance/object. I don’t want to use container.Resolve< <Lazy<IService1> >(); From this link (http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx) I understood that unity.mvc 4 use unity 3 which has Lazy

How do I use the Decorator Pattern with Unity without explicitly specifying every parameter in the InjectionConstructor

前提是你 提交于 2019-11-27 06:58:16
This helpful article from David Haydn (EDIT: scam link removed, it could have been this article ) shows how you can use the InjectionConstructor class to help you set up a chain using the decorator pattern with Unity. However, if the items in your decorator chain have other parameters in their constructor, the InjectionConstructor must explicitly declare each one of them (or Unity will complain that it can't find the right constructor). This means that you can't simply add new constructor parameters to items in the decorator chain without also updating your Unity configuration code. Here's

Where does Unit Of Work belong w/ EF4, IoC (Unity), and Repository?

梦想与她 提交于 2019-11-27 06:54:34
问题 I see several questions relating somewhat to this, but I still can't find the answer I'm looking for, so I'm posting my question. If another question holds the answer (and I'm just not seeing it), please point me to it. I'm trying to figure out where my UnitOfWork belongs -- and specifically, gets created -- when using EF4 and Unity with the Repository pattern. Basically, I have a service that is used to implement my business logic. This service constructor takes in the repository, so the

Is there TryResolve in Unity?

£可爱£侵袭症+ 提交于 2019-11-27 06:40:52
问题 How can I make Unity not to throw ResolutionFailedException if Resolve fails? Is there something like TryResolve<IMyInterface> ? var container = new UnityContainer(); var foo = container.TryResolve<IFoo>(); Assert.IsNull(foo); 回答1: This has been an issue on the codeplex site, you can find the code here (look at the bottom of that thread and they have made an extension method...very handy) http://unity.codeplex.com/Thread/View.aspx?ThreadId=24543 and the you can use code like this: if

using a Handler in Web API and having Unity resolve per request

吃可爱长大的小学妹 提交于 2019-11-27 06:01:11
问题 I am using Unity as my IoC framework and I am creating a type based on the value in the header of each request in a handler: var container = new UnityContainer(); container.RegisterType<IFoo,Foo>(new InjectionConstructor(valuefromHeader)); GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); The problem is that the handler's SendAsync means that the global container is getting overwritten by different requests and the controllers that use

Way to fill collection with Unity

不打扰是莪最后的温柔 提交于 2019-11-27 05:45:12
问题 I have two example classes class ClassToResolve { private List<CollectionItem> _coll; public ClassToResolve(List<CollectionItem> coll) { _coll = coll; } } class CollectionItem { //... } and I need to resolve ClassToResolve var classToResolve = new ClassToResolve( new List<CollectionItem>() { new CollectionItem(), new CollectionItem(), new CollectionItem() } ); Now I resolve it in a way var classToResolve = new ClassToResolve( new List<CollectionItem>() { unity.Resolve<CollectionItem>(), unity

Unity Register two interfaces as one singleton

左心房为你撑大大i 提交于 2019-11-27 05:15:55
问题 how do I register two different interfaces in Unity with the same instance... Currently I am using _container.RegisterType<EventService, EventService>(new ContainerControlledLifetimeManager()); _container.RegisterInstance<IEventService>(_container.Resolve<EventService>()); _container.RegisterInstance<IEventServiceInformation>(_container.Resolve<EventService>()); which works, but does not look nice.. So, I think you get the idea. EventService implements two interfaces, I want a reference to