c# - 开放泛型类型的 CaSTLe Windsor Complex 注册

标签 c# .net castle-windsor windsor-facilities

我有 2 个数据处理类:

public class SqlDataProvider<T> : IDataProvider<T> where T : IEntity
public class MongoDataProvider<T> : IDataProvider<T> where T : IEntity

我有很多这样的模型:

public class Account : ISqlDbEntity
public class Log : IMongoDbEntity

使用这些接口(interface):

public interface IMongoDbEntity : IEntity
public interface ISqlDbEntity : IEntity

我应该如何注册或是否可以将 Windsor 容器注册为泛型,但 MondoDbEntity 与 MongoDbProvider 一起使用,MsSQl 模型与 SqlDataProvider 一起使用。

    container.Register(Component.For(typeof(IDataProvider<>))
        .ImplementedBy(typeof(SqlDataProvider<>))
        .LifestylePerWebRequest()); //I want this work only with ISqlEntity

   container.Register(Component.For(typeof(IDataProvider<>))
        .ImplementedBy(typeof(MongoDataProvider<>))
        .LifestylePerWebRequest()); //I want this work only with IMongoDbEntity

我试过这些,但没有用:

container.Register(Component.For(typeof(IDataProvider<ISqlDbEntity>))
                .ImplementedBy(typeof(SqlDataProvider<>))
                .LifestylePerWebRequest());

            container.Register(Component.For(typeof(IDataProvider<IMongoDbEntity>))
                .ImplementedBy(typeof(MongoDataProvider<>))
                .DependsOn(Dependency.OnAppSettingsValue("databaseName", "MongoDatabaseName"))
                .LifestylePerWebRequest());

提前致谢!

最佳答案

如果每个提供者只使用一种类型的实体,那么为什么不在提供者类型注册中使用独特的实体接口(interface)呢?

container.Register(Component.For(typeof(IDataProvider<ISqlDbEntity>))
    .ImplementedBy(typeof(SqlDataProvider<>))
    .LifestylePerWebRequest()); 

关于c# - 开放泛型类型的 CaSTLe Windsor Complex 注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34815869/

相关文章:

c# - 使用 JavaScript 在 GridView 中选择行

c# - 我应该如何管理类依赖/重构?

c# - 可用于测试国际化和本地化库的单数和复数形式的非英语短语有哪些?

c# - excel 2010 文件的 XlFileFormat

c# - 在 C# 的 app.config 文件中存储 < 和 > 字符时出错

c# - 认购期满了该如何限制?

c# - 用属性覆盖属性

c# - 打开其他程序的配置文件

c# - 使用 CaSTLe windsor 创建对象而不是工厂类

c# - CaSTLe 依赖注入(inject) 生活方式