unity-container

Unity IOC container and how to resolve different instances of the same interface

你。 提交于 2019-12-18 13:54:04
问题 I have a unity container that I am registering types within like so: IUnityContainer container = new UnityContainer() .RegisterType<ITaxAuthorityRateService, TaxAuthorityPopulationRateService>( "PopulationRate" ) .RegisterType<ITaxAuthorityRateService, TaxAuthorityBusinessLicenseRateService>( "BusinessLicenseRate" ); Then I also want to register 2 different services that take a ITaxAuthorityRateService variable in their constructor. Both services need a different class that derives from

Unity DI Inject DbContext with PerRequestLifetimeManager

岁酱吖の 提交于 2019-12-18 13:39:26
问题 I have the following code that initialize instances with Unity: IUnityContainer container = new UnityContainer(); container.RegisterType<DbContext, VotingSystemContext>(new PerRequestLifetimeManager(), new InjectionConstructor()); container.RegisterType(typeof(IGenericRepository<>), typeof(GenericRepository<>)); container.RegisterType<IUnitOfWork, UnitOfWork>(new PerRequestLifetimeManager()); container.RegisterTypes( AllClasses.FromAssemblies( Assembly.GetAssembly(typeof(IUserService)),

Unity Container Multiple Implementations of same interface

丶灬走出姿态 提交于 2019-12-18 13:21:32
问题 I'm studying up on the unity containers and have a quick question on how to resolve a class's construction to multiple different implementations of an interface. Here's my code: public interface IRenderer { void DrawSquare(Square square); void DrawCircle(Circle circle); } public interface IShape { void Draw(IRenderer renderer); } public class Dx11Renderer : IRenderer { public void DrawSquare(Square square) { } public void DrawCircle(Circle circle) { } } public class GlRenderer : IRenderer {

Register the same type to multiple interfaces

浪尽此生 提交于 2019-12-18 12:25:47
问题 It is possible to register one type to multiple interfaces? I have class that implement two interfaces MyService : IService1, IServier2 {} I would like to register this type for both interfaces. container.RegisterType<IService1, MyService>(CreateLifetime()); container.RegisterType<IService2, MyService>(CreateLifetime()); Unfortunately during after resolving I have two different instances. I tried use common lifetime but then I got message that I can't. 回答1: I usually write this: .RegisterType

Unity: The type LogWriter cannot be constructed

泪湿孤枕 提交于 2019-12-18 08:11:17
问题 For the following "project" I am getting a very annoying and inexplicable error when resolving Unity for DI. InvalidOperationException - The type LogWriter cannot be constructed. You must configure the container to supply this value. ?ex.Message; "Resolution of the dependency failed, type = \"WindowsFormsApplication1.Performance\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The type LogWriter cannot be constructed. You must

Unity: Change default lifetime manager for implicit registrations and/or disable them

喜欢而已 提交于 2019-12-18 05:45:06
问题 The Unity container will automatically resolve any type that it can figure out on its own without the need for manual registration. That's good in some ways, but the problem I have is that it uses a TransientLifetimeManager for this type of resolution, while I almost always want a ContainerControlledLifetimeManager . I can still register my types as singletons manually, of course, but if I forget, instead of getting an unhandled exception at startup, the app will launch successfully and

Injecting arrays with Unity

回眸只為那壹抹淺笑 提交于 2019-12-18 04:48:06
问题 My goal is to constructor inject an array of objects implementing an interface. The following is the way I currently have it. Container .RegisterInstance<Company>(ParseCompany(args[1]) .RegisterInstance<eTargets>(ParseTargets(args[2])) .RegisterInstance<ILoader[]>(new ILoader[] { Container.Resolve<CustomerLoader>(), Container.Resolve<PaymentLoader(), Container.Resolve<InvoiceLoader() }); Is it typical to call Resolve in container configuration this way or is there a more standard way to

IoC in class library. Where to bootstrap

試著忘記壹切 提交于 2019-12-17 21:53:19
问题 I'm using a class library that can be reused by other components. In this class library I'm using unity for dependency injection. For this class library I create a test project. The caller also gets a test project. One thing I'm uncertain about is the location of the bindings. Should I incorporate this in the class library or should I do this from the calling application? 回答1: This is an interesting problem. How can you dependency inject re-usable assemblies that do not have an entry point. I

Using Unity, how do you register type mappings for generics?

ぐ巨炮叔叔 提交于 2019-12-17 20:19:03
问题 I'm trying to implement a repository solution for Entity Framework but I am having trouble registering the types that include generics using Unity. Given: // IRepository interface public interface IRepository<TEntity> { // omitted for brevity } // Repository implementation public class Repository<TEntity, TContext> : IRepository<TEntity>, IDisposable where TEntity : class where TContext : DbContext { // omitted for brevity } // service layer constructor public MyServiceConstructor(IRepository

Bootstrapping Unity - Composition Root location

我只是一个虾纸丫 提交于 2019-12-17 19:58:09
问题 I have a simple .NET project and I am wondering what's the best approach for bootstrapping Unity. I started with a WebApp with a bunch of controllers. Each of these controllers has its own Handler class to which the controller delegates the implementation. Something in the lines of: public class UsersHandler : IUsers { IAuthenticationClient authenticationClient; ILogger logger; public UsersHandler(IAuthenticationClient authClient, ILogger logger) { ... } } In the Application_Start method of