在 StructureMap 中使用开放式泛型进行自定义构造

发布于 2024-09-30 15:08:38 字数 750 浏览 0 评论 0 原文

我知道对于结构图,您可以通过指定以下内容来注册泛型类型:

StructureMapConfiguration 
   .For(typeof(IRepository<>)) 
   .Use(typeof(RepositoryImplementation<>));

当调用 ObjectFactory.GetInstance>() 时,返回相应的实现 RepositoryImplementation< /代码>。

但是如果我想要存储库的包装版本怎么办?还实现了 IRepository 的版本 - 假设 CachedRepository 具有一个采用 IRepository 实现的构造函数构造函数:CachedRepository(IRepositoryinnerRepository)

当请求使用 concreate RepositoryImplementation 作为innerRepository 的 IRepository 时,如何让结构图返回 CachedRepository

I know that for structuremap you can register generic types by specifying the following:

StructureMapConfiguration 
   .For(typeof(IRepository<>)) 
   .Use(typeof(RepositoryImplementation<>));

When ObjectFactory.GetInstance<IRepository<Entity>>() is called the corresponding implementation is returned RepositoryImplementation<Entity>.

But what if I want a wrapped version of the repository? A version that also implements IRepository<Entity> - lets say CachedRepository<Entity> that has a constructor that takes an implementation of IRepository<TEntity> ctor: CachedRepository(IRepository<Entity> innerRepository).

How do I get structuremap to return CachedRepository<Entity> when asking for an IRepository with the concreate RepositoryImplementation as innerRepository?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-10-07 15:08:38

一种方法是创建一个自定义类型拦截器:

public class CacheMyRepos : TypeInterceptor
{
    private readonly Type _openTargetType;
    private readonly Type _openWrapperType;

    public CacheMyRepos(Type openTargetType, Type openWrapperType)
    {
        _openTargetType = openTargetType;
        _openWrapperType = openWrapperType;
    }

    public object Process(object target, IContext context)
    {
        var closedWith = target.GetType().GetGenericArguments()[0];
        var closedWrapperType = _openWrapperType.MakeGenericType(closedWith);
        return Activator.CreateInstance(closedWrapperType, target);
    }

    public bool MatchesType(Type type)
    {
        return type.ImplementsInterfaceTemplate(_openTargetType);
    }
}

然后将其注册为:

var container = new Container(x =>
{
    x.For(typeof (IRepository<>)).Use(typeof (RepositoryImplementation<>));
    x.RegisterInterceptor(
      new CacheMyRepos(typeof(IRepository<>), typeof(CachedRepository<>)));
});

One approach is to create a custom type interceptor:

public class CacheMyRepos : TypeInterceptor
{
    private readonly Type _openTargetType;
    private readonly Type _openWrapperType;

    public CacheMyRepos(Type openTargetType, Type openWrapperType)
    {
        _openTargetType = openTargetType;
        _openWrapperType = openWrapperType;
    }

    public object Process(object target, IContext context)
    {
        var closedWith = target.GetType().GetGenericArguments()[0];
        var closedWrapperType = _openWrapperType.MakeGenericType(closedWith);
        return Activator.CreateInstance(closedWrapperType, target);
    }

    public bool MatchesType(Type type)
    {
        return type.ImplementsInterfaceTemplate(_openTargetType);
    }
}

And then register it with:

var container = new Container(x =>
{
    x.For(typeof (IRepository<>)).Use(typeof (RepositoryImplementation<>));
    x.RegisterInterceptor(
      new CacheMyRepos(typeof(IRepository<>), typeof(CachedRepository<>)));
});
椒妓 2024-10-07 15:08:38

CachedRepository 是否必须与任何具体实现配合使用,或者将其绑定到 RepositoryImplementation 是否安全?如果可以绑定,这应该可以解决问题:

StructureMapConfiguration
   .For(typeof(IRepository<>)) 
   .Use(typeof(CachedRepository<>));

然后将 CachedRepository 构造函数更改为 CachedRepository(RepositoryImplementation insideRepository)。

Does CachedRepository<Entity> have to work with any concrete implementation, or is it safe to tie it to RepositoryImplementation<Entity>? If it can be tied, this should do the trick:

StructureMapConfiguration
   .For(typeof(IRepository<>)) 
   .Use(typeof(CachedRepository<>));

Then change the CachedRepository constructor to CachedRepository(RepositoryImplementation<Entity> innerRepository).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文