标签: unity-container

RegisterType与UnityContainer中的接口

我正在使用UnityContainer,我想注册一个不是类型的接口,而是使用另一个接口.不幸的是,我无法干净利落地做到这一点.

我有几个通用的接口,它们在一个接口中统一,我需要在容器中注册它们.代码如下:

interface IDeviceImporter {
    void ImportFromDevice();
}

interface IFileImporter {
    void ImportFromFile();
}

interface IImporter : IDeviceImporter, IFileImporter {
}


class Importer1: IImporter {
}
class Importer2: IImporter {
}
Run Code Online (Sandbox Code Playgroud)

进入库时,我知道要使用哪个导入器,因此代码如下:

var container = new UnityContainer();
if (useFirstImport) {
    container.RegisterType<IImporter, Importer1>();
} else {
    container.RegisterType<IImporter, Importer2>();
}
Run Code Online (Sandbox Code Playgroud)

然后我想用IDeviceImporter和IFileImporter注册这个特定的类.我需要这样的东西:

container.RegisterType<IDeviceImporter, IImporter>();
Run Code Online (Sandbox Code Playgroud)

但是那个代码我得到了一个错误: IImporter is an interface and cannot be constructed.

我可以在条件内完成,但那时它将是复制粘贴.我可以

container.RegisterInstance<IDeviceImporter>(container.Resolve<IImporter>());
Run Code Online (Sandbox Code Playgroud)

但它真的很脏.有人请,建议我:)

c# unity-container

4
推荐指数
2
解决办法
1万
查看次数

我需要一个Unity容器的单个实例吗?

对不起,这个noob问题.我开始在Silverlight中使用Unity 2.0容器,并在其中添加一些单例.为了让单身人士工作,我是否需要在我的应用程序中只有一个容器实例?我假设每当我想查找我的单身时,创建一个新的单位容器实例将导致单独的容器与单独的单例.

谢谢,-Jon

c# silverlight dependency-injection inversion-of-control unity-container

4
推荐指数
2
解决办法
1445
查看次数

通过XML配置统一字典

如何使用Unity容器通过XML配置字典?这有效:

<register type="System.Collections.Generic.Dictionary[string,int]" >
<constructor>
    <param name="capacity">
    <value value="10" />
    </param>
</constructor>
</register>
Run Code Online (Sandbox Code Playgroud)

但我需要能够在XML配置中添加元素.

xml dependency-injection inversion-of-control unity-container

4
推荐指数
1
解决办法
3524
查看次数

Ninject父子容器层次结构(如继承)

是否Ninject有类似于Unity父/子容器的概念来提供基本的继承模型?我用谷歌搜索但没有找到任何东西.

inheritance dependency-injection ninject ioc-container unity-container

4
推荐指数
1
解决办法
2178
查看次数

相当于Bind <>.Unity中的ToMethod?

在Unity中是否有相当于Ninject Factory的方法?我正在寻找以下示例的统一等价物:

Bind<IWeapon>().ToMethod(context => new Sword());
Run Code Online (Sandbox Code Playgroud)

ninject unity-container

4
推荐指数
1
解决办法
502
查看次数

如何拦截和取消方法执行

有没有办法拦截方法执行并取消它?我只是找到了一种Invoke方法来使用Microsoft.Practices.Unity和从方法中抛出异常ICallHandler,但是在Invoke方法实现上,我只能返回getNext()(input, getNext);.

这是代码:

public class TestHandler : ICallHandler
{
    public int Order { get; set; }

    public IMethodReturn Invoke(IMethodInvocation input, 
                                GetNextHandlerDelegate getNext)
    {
        Console.WriteLine("It's been intercepted.");

        // Here... please Lord, tell me I can cancel it, I'm a good person.
        return getNext()(input, getNext);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# aop unity-container interceptor

4
推荐指数
1
解决办法
1678
查看次数

使用Unity容器注册递归结构

是否可以使用以下递归结构向unity容器注册:

public interface IFoo
{
    IBar[] Bars { get; set; }
}

public interface IBar
{
    IFoo[] Foos { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

假设每个接口都存在多个命名实例:

public class Foo1 : IFoo 
{
    public IBar[] Bars { get; set; }
}

public class Foo2 : IFoo
{
    public IBar[] Bars { get; set; }
}

public class Bar1 : IBar
{
    public IFoo[] Foos { get; set; }
}

public class Bar2 : IBar
{
    public IFoo[] Foos { get; set; }
} …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection circular-dependency unity-container

4
推荐指数
1
解决办法
1628
查看次数

Unity:注入多个PerResolveLifetimeManager注册类型

我正在使用使用工作单元模式的存储库:

public BaseRepository(IUnitOfWork unitOfWork, IEntityFactory factory) { ... }
Run Code Online (Sandbox Code Playgroud)

以前我只需要将一个IUnitOfWork实例注入存储库(使用Unity),如下所示:

// Unit of work for the UserDbContext
container.RegisterType<IUnitOfWork, EntityFrameworkUnitOfWork>(new PerResolveLifetimeManager(), new InjectionConstructor(new UserDbContext()));

container.RegisterType<IUserRepository, UserRepository>();
container.RegisterType<ITokenRepository, TokenRepository>();
Run Code Online (Sandbox Code Playgroud)

现在我需要引入另一个存储库,但是这个存储库需要使用以下的不同实例IUnitOfWork:

// Unit of work for the OrderDbContext
container.RegisterType<IUnitOfWork, EntityFrameworkUnitOfWork>(new PerResolveLifetimeManager(), new InjectionConstructor(new OrderDbContext()));

container.RegisterType<IOrderRepository, OrderRepository>();
Run Code Online (Sandbox Code Playgroud)

如何使用Unity明确指定将哪个IUnitOfWork注入哪个存储库?

编辑:

使用Daniel JG的答案,我有以下代码:

container.RegisterType<IUnitOfWork, EntityFrameworkUnitOfWork>(new PerResolveLifetimeManager(), new InjectionConstructor(new UserDbContext()));
container.RegisterType<IUnitOfWork, EntityFrameworkUnitOfWork>("OrderDbContext", new PerResolveLifetimeManager(), new InjectionConstructor(new OrderDbContext()));

container.RegisterType<IUserRepository, UserRepository>();
container.RegisterType<ITokenRepository, TokenRepository>();

container.RegisterType<IOrderRepository, OrderRepository>(
    new InjectionConstructor(
        new ResolvedParameter<IUnitOfWork>("OrderDbContext"),
        new ResolvedParameter<IEntityFactory<Order, int, OrderTbl>>()
    )
);
Run Code Online (Sandbox Code Playgroud)

但是抛出了以下异常:

[ResolutionFailedException:依赖项的解析失败,type …

c# dependency-injection ioc-container unity-container repository-pattern

4
推荐指数
1
解决办法
3234
查看次数

Unity - 通过调用类的方法将对象注入到构造函数中

我有以下界面及其实现

public class DummyProxy : IDummyProxy
{
    public string SessionID { get; set; }
    public DummyProxy(string sessionId)
    {
        SessionId = sessionId;
    }
}

public interface IDummyProxy
{
}
Run Code Online (Sandbox Code Playgroud)

然后我有另一个类来获取会话ID

public class DummySession
{
    public string GetSessionId()
    {
        Random random = new Random();
        return random.Next(0, 100).ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,在我的Unity容器中,每次容器尝试解析IDummyProxy时,我都想向DummyProxy注入'session id'.但是这个'session id'必须从DummySession类生成.

container.RegisterType<IDummyProxy, DummyProxy>(
    new InjectionConstructor(new DummySession().GetSessionId()));
Run Code Online (Sandbox Code Playgroud)

这甚至可能吗?

c# ioc-container unity-container

4
推荐指数
1
解决办法
1171
查看次数

如何在app.config中指定类的类型?

我试图了解如何在app.config中引用一个类.在这种情况下,我想在app.config中配置和microsoft unity扩展

<extension type="?, ?" />
Run Code Online (Sandbox Code Playgroud)

type="assemblyName, namespace.class"吗?

c# app-config unity-container

4
推荐指数
1
解决办法
1721
查看次数