Structuremap3 DecorateAllWith [英] Structuremap3 DecorateAllWith

查看:44
本文介绍了Structuremap3 DecorateAllWith的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力让DecorateAllWith在通用接口上工作.我在这里阅读了一些帖子,他们在其中使用拦截器解决了该问题,但似乎使用的是较早的结构图版本,这似乎不是干净"的解决方案.

I've been struggling getting DecorateAllWith working on generic interfaces. I've read some posts here where they solved it using interceptors but they seem to be using an older structure map version and it doesn't seem like a "clean" solution.

我真的需要一些帮助才能使其与结构图3一起使用

I would really need some help to get it working with structure map 3

我有一个通用的存储库,我想用日志记录和缓存来装饰

I have a generic repository which i would like to decorate with both logging and caching

public interface IEntityRepository<T> where T : Entities.IEntity
{
}

我有大约20个继承IEntityRepository的接口.例子mu UserRepository

I have about 20 interfaces that inherit IEntityRepository. Example mu UserRepository

public interface IUserEntityRepository : IEntityRepository<User>
{
}

然后我有一个日志装饰器具体类型,我希望用IEntityRepository的所有实例装饰

And then I have the logging decorator concrete type which I would like all instances of IEntityRepository to be decorated with

public class LoggingEntityRepository<T> : IEntityRepository<T> where T : Entities.IEntity
{
    private readonly IEntityRepository<T> _repositoryDecorated;

    public LoggingEntityRepository(IEntityRepository<T> repositoryDecorated)
    {
        _repositoryDecorated = repositoryDecorated;
    }
}

或者还有其他IoC容器更适合我要完成的工作吗?

Or are there other IoC containers better suited for what I am trying to accomplish?

有没有办法装饰从IEntityRepository继承的所有接口

Is there a way to decorate all interfaces that inherit from IEntityRepository

推荐答案

这是一个有效的示例,可以回答您的第一个问题

Here's a working example that answers your first question

[Fact]
public void DecorateAllWith_AppliedToGenericType_IsReturned()
{
    var container = new Container(registry =>
    {
        registry.Scan(x =>
        {
            x.TheCallingAssembly();
            x.ConnectImplementationsToTypesClosing(typeof(IEntityRepository<>));

        });

        registry.For(typeof(IEntityRepository<>))
            .DecorateAllWith(typeof(LoggingEntityRepository<>));
    });

    var result = container.GetInstance<IEntityRepository<Entity1>>();

    Assert.IsType<LoggingEntityRepository<Entity1>>(result);
}

为回答您的第二个问题,我个人使用(并做出了贡献)简单注射器-它是最快容器,它对<一个href ="https://simpleinjector.org/generics" rel ="nofollow">泛型,并提供了一些功能强大的诊断服务.

To answer your second question, I personally use (and contribute to) Simple Injector - it is one of the fastest containers available, has comprehensive support for generics and offers some powerful diagnostic services.

Simple Injector中的注册如下所示:

The registrations in Simple Injector would look like this:

[Fact]
public void RegisterDecorator_AppliedToGenericType_IsReturned()
{
    var container = new SimpleInjector.Container();

    container.RegisterManyForOpenGeneric(
        typeof(IEntityRepository<>), 
        typeof(IEntityRepository<>).Assembly);

    container.RegisterDecorator(
        typeof(IEntityRepository<>), 
        typeof(LoggingEntityRepository<>));

    var result = container.GetInstance<IEntityRepository<Entity1>>();

    Assert.IsType<LoggingEntityRepository<Entity1>>(result);
}

这篇关于Structuremap3 DecorateAllWith的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆