繁体   English   中英

自动解析界面 <T> 实施 <T> 在StructureMap中(仅由通用类型T不同)

[英]Automatically resolve Interface<T> to Implementation<T> in StructureMap (differ only by generic type T)

我有一个接口( IRepository<T> ),当前正在为每个特定的存储库进行扩展,即: IUserRepository : IRepository<User>

每个接口都有相应的具体类,即: UserRepository : Repository<User>, IUserRepository

这些单独的存储库不添加任何其他功能,它们都是空接口/类,仅用于传递泛型。

我用StructureMap解决IUserRepositoryUserRepository使用注册表与组装扫描仪和一些命名约定。

我希望这可以转向更优化的状态,而不是传递IUserRepository实例并将其解析为UserRepository ,我可以传递IRepository<User>并将其解析为Repository<User>

这将消除创建这些额外的空接口和类的需要。

我无法找到一种方法来使用StructureMap的配置来设置这种通用映射。 像这样的东西:

For(typeof(IRepository<>).Use(typeof(Repository<>)).WithTheGenericTypeFromTheInterfaceSuppliedAsATypeParameter();

编辑

得到前几个答案之后,我想再澄清一下这个问题。

我不想为配置的For位创建单独的类。 我希望在我的代码中有以下类/接口:

  • IRepository<T> where T : Entity
  • Repository<T> : IRepository<T> where T : Entity
  • Person : Entity
  • Product : Entity
  • Order : Entity
  • Whatever : Entity

并按照惯例实现以下映射:

IRepository<Person> => Repository<Person>
IRepository<Product> => Repository<Product>
IRepository<Order> => Repository<Order>
IRepository<Whatever> => Repository<Whatever>

但我不想为每个人创建一个映射,ala:

For<IRepository<Person>>().Use<Repository<Person>>();
For<IRepository<Product>>().Use<Repository<Product>>();
For<IRepository<Order>>().Use<Repository<Order>>();
For<IRepository<Whatever>>().Use<Repository<Whatever>>();

我想要一个适用于任何IRepository的映射:

For<IRepository<>>().Use<Repository<>>().WithTheSameGenericType();

然后我会使用它将存储库注入服务:

public MyService(IRepository<User> userRepository)

并希望在运行时将其解析为Repository<User>

事实证明没有花哨的方法来调用,或者没有花哨的布线,只需使用ForUse (非泛型版本):

public class DataRegistry : Registry
{
    public DataRegistry()
    {
        For(typeof (IRepository<>)).Use(typeof(Repository<>));
    }
}

当我注入IRepository<Person>它现在被解析为Repository<Person>

我遇到错误104,说Repository不能插入IRepository。 这是因为Repository被标记为abstract 使它成为非抽象的,修复了错误并且它正在按预期工作。

我建议你看一下AddAllTypesOf方法。 我有一些类似的代码,并通过使用它来实现我的目标(并保持自动注册功能工作)。

在你的情况下,你应该改变

For<IRepository<Person>>().Use<Repository<Person>>();
For<IRepository<Product>>().Use<Repository<Product>>();
For<IRepository<Order>>().Use<Repository<Order>>();
For<IRepository<Whatever>>().Use<Repository<Whatever>>();

AddAllTypesOf(typeof(IRepository<>));

最后,您的容器将类似于:

return new Container(x =>
        {
            x.Scan(y =>
            {
                y.TheCallingAssembly();
                y.AddAllTypesOf(typeof(IRepository<>));
                y.WithDefaultConventions();
            });
        });

这是一个例子。 结构图也允许您同时执行这两项操作。

 //IRepository
 For<IMemberRepository>().Add<MemberRepository>();

 //IRepository<T>
 For<IRepository<Member>>().Add<MemberRepository>();

然后通过在运行时知道泛型类型来询问类型是有用的:

Type repositoryType = typeof(IRepository<>).MakeGenericType(modelType);
IocResolve.Resolve(repositoryType);

看看IRegistrationConvention界面。

public class DomainRegistry : Registry
{
    public DomainRegistry()
        : base()
    {
        Scan(y =>
        {
            y.AssemblyContainingType<IRepository>();
            y.Assembly(Assembly.GetExecutingAssembly().FullName);
            y.With(new RepositoryTypeScanner());
        });

RepositoryTypeScanner的位置:

public class RepositoryTypeScanner : IRegistrationConvention
{
    public void Process(Type type, Registry registry)
    {
      ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM