unity-container

Can I pass constructor parameters to Unity's Resolve() method?

假装没事ソ 提交于 2019-11-26 15:14:19
I am using Microsoft's Unity for dependency injection and I want to do something like this: IDataContext context = _unityContainer.Resolve<IDataContext>(); var repositoryA = _unityContainer.Resolve<IRepositoryA>(context); //Same instance of context var repositoryB = _unityContainer.Resolve<IRepositoryB>(context); //Same instance of context IDataContext context2 = _unityContainer.Resolve<IDataContext>(); //New instance var repositoryA2 = _unityContainer.Resolve<IRepositoryA>(context2); RepositoryA and RepositoryB both have a constructor that takes an IDataContext parameter, and I want Unity to

How better to resolve dependencies in object created by factory?

不打扰是莪最后的温柔 提交于 2019-11-26 14:58:49
问题 For example I have dependency: public interface IMyDependency { } public class MyDependency : IMyDependency { } That injects in MyClass object: public interface IMyInterface { } public class MyClass : IMyInterface { [Dependency] public IMyDependency MyDependency { get; set; } } Also I have a Factory, that creates MyClass instance: public interface IFactory { IMyInterface CreateMyObject(); } public class Factory : IFactory { public IMyInterface CreateMyObject() { // some checks before creation

Resolving classes without registering them using Castle Windsor

依然范特西╮ 提交于 2019-11-26 14:21:35
问题 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

Using Dependency Injection without any DI Library

女生的网名这么多〃 提交于 2019-11-26 14:09:43
问题 I am new to Repository and DI and trying to implement in my MVC 5 project. I implemented Constructor Injection where in my controller has a constructor like this: IBook _ibook; public Test(IBook ibook) { _ibook = ibook; } Without any DI library, it throws an error: There is no empty constructor. To avoid this, I added one more constructor as below: public Test ():this(new Book()) { } Since I am new to DI, I don't want to risk my project by using DI library which can later throw some error

Configure Unity DI for ASP.NET Identity

大城市里の小女人 提交于 2019-11-26 14:07:31
问题 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

Dependency Injection in attributes

倖福魔咒の 提交于 2019-11-26 13:48:38
I am trying to inject a dependency into a custom AuthorizeAttribute as follows: public class UserCanAccessArea : AuthorizeAttribute { readonly IPermissionService permissionService; public UserCanAccessArea() : this(DependencyResolver.Current.GetService<IPermissionService>()) { } public UserCanAccessArea(IPermissionService permissionService) { this.permissionService = permissionService; } protected override bool AuthorizeCore(HttpContextBase httpContext) { string AreaID = httpContext.Request.RequestContext.RouteData.Values["AreaID"] as string; bool isAuthorized = false; if (base.AuthorizeCore

MEF vs. any IoC

旧巷老猫 提交于 2019-11-26 10:20:03
问题 Looking at Microsoft\'s Managed Extensibility Framework (MEF) and various IoC containers (such as Unity), I am failing to see when to use one type of solution over the other. More specifically, it seems like MEF handles most IoC type patterns and that an IoC container like Unity would not be as necessary. Ideally, I would like to see a good use case where an IoC container would be used instead of, or in addition to, MEF. 回答1: When boiled down, the main difference is that IoC containers are

Can Unity be made to not throw SynchronizationLockException all the time?

扶醉桌前 提交于 2019-11-26 10:18:49
问题 The Unity dependency injection container has what seems to be a widely known issue where the SynchronizedLifetimeManager will often cause the Monitor.Exit method to throw a SynchronizationLockException which is then caught and ignored. This is a problem for me because I like to debug with Visual Studio set to break on any thrown exception, so every time my application starts up I\'m breaking on this exception multiple times for no reason. How can I prevent this exception from being thrown?

Comparing Castle Windsor, Unity and StructureMap

孤者浪人 提交于 2019-11-26 10:09:54
问题 In a follow up to Krzysztof’s statement that Windsor does a lot more than other IoC’s, I wanted to understand how these IoC’s stack up against each other and the benefits/additional facilities that castle Windsor provides. Are there any comparisons? Can someone help me understand the additional features that Castle Windsor provides over other IoC’s 回答1: See here and here for a pretty thorough technical comparison of several IoC containers, although somewhat outdated by now (they're from

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

隐身守侯 提交于 2019-11-26 09:24:21
问题 I have the IRespository registered twice (with names) in the following code: // Setup the Client Repository IOC.Container.RegisterType<ClientEntities>(new InjectionConstructor()); IOC.Container.RegisterType<IRepository, GenericRepository> (\"Client\", new InjectionConstructor(typeof(ClientEntities))); // Setup the Customer Repository IOC.Container.RegisterType<CustomerEntities>(new InjectionConstructor()); IOC.Container.RegisterType<IRepository, GenericRepository> (\"Customer\", new