我有 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
使用这些接口:
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());
提前谢谢!