Entity framework 如何在Castle Windsor中基于泛型类型参数解析服务?
我有这样一个安装程序:Entity framework 如何在Castle Windsor中基于泛型类型参数解析服务?,entity-framework,generics,inversion-of-control,castle-windsor,repository-pattern,Entity Framework,Generics,Inversion Of Control,Castle Windsor,Repository Pattern,我有这样一个安装程序: public void Install(IWindsorContainer container, IConfigurationStore store) { //Services container.Register( Classes.FromAssemblyNamed(ASSEMBLY_NAME) .BasedOn<IService>()
public void Install(IWindsorContainer container, IConfigurationStore store) {
//Services
container.Register(
Classes.FromAssemblyNamed(ASSEMBLY_NAME)
.BasedOn<IService>()
.WithServiceFirstInterface()
.LifestyleTransient());
//Repository
container.Register(
Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(Repository<>))
.LifestyleTransient());
//Contexts
container.Register(
Component.For(typeof(Context<IGlobalObject>))
.ImplementedBy(typeof(GlobalContext<>)).LifestyleTransient());
}
然而,温莎无法解析背景,我也无法找出原因?它已注册并在容器中,但无法解析
编辑:
GlobalContext的代码:
public class GlobalContext<T> : Context<T>
where T : IGlobalObject
{
private const string GLOBAL_CSTR = "Global";
public GlobalContext() : base(ConfigurationManager.ConnectionStrings[GLOBAL_CSTR].ConnectionString) {}
public DbSet<Company> Companies { get; set; }
public DbSet<ConnectionString> ConnectionStrings { get; set; }
public DbSet<Server> Servers { get; set; }
}
public类GlobalContext:Context
其中T:IGlobalObject
{
private const string GLOBAL_CSTR=“GLOBAL”;
public GlobalContext():base(ConfigurationManager.ConnectionString[GLOBAL_CSTR].ConnectionString{}
公共数据库集公司{get;set;}
公共数据库集连接字符串{get;set;}
公共数据库集服务器{get;set;}
}
背景:
//Wrapper around dbcontext which enforces type
public abstract class Context<T> : DbContext where T : IObject
{
protected Context() {}
protected Context(string connectionString) : base(connectionString){}
}
//围绕dbcontext的包装器,它强制类型
公共抽象类上下文:DbContext,其中T:ioobject
{
受保护的上下文(){}
受保护上下文(字符串connectionString):基(connectionString){}
}
编辑2:
如果我为它工作的每个场景指定具体类型,那么这显然与接口上的匹配有关
//Contexts
container.Register(
Component.For(typeof(Context<Server>))
.ImplementedBy(typeof(GlobalContext<Server>)).LifestyleTransient());
//上下文
集装箱。登记(
组件。For(类型(上下文))
.ImplementedBy(typeof(GlobalContext)).LifestyleTransient();
我觉得这是个问题:
//Contexts
container.Register(
Component.For(typeof(Context<IGlobalObject>))
.ImplementedBy(typeof(GlobalContext<>)).LifestyleTransient());
//上下文
集装箱。登记(
组件。For(类型(上下文))
.ImplementedBy(typeof(GlobalContext)).LifestyleTransient();
这里你是说,当有人要求上下文注入GlobalContext时,问题是windsor是如何知道GlobalContext的一般参数的
如果看不到GlobalContext对象,很难看到它,但它应该是:
container.Register(
Component.For(typeof(Context<>))
.ImplementedBy(typeof(GlobalContext<>)).LifestyleTransient());
container.Register(
组件。For(类型(上下文))
.ImplementedBy(typeof(GlobalContext)).LifestyleTransient();
这并不是对你问题的直接回答。但我觉得这种方法可能是错误的
考虑到您的存储库是由泛型库
存储库实现的,我看不到将泛型类型与正确的上下文关联起来的干净方法。我认为您可能需要切换到带有显式上下文的“风味”存储库,并且/或者在注册上下文的方式上更加详细。这完全是有道理的,但遗憾的是没有起作用。我已经添加了上下文和GlobalContext的代码。CheersOk-我想我现在更明白了,我做了一个小的编辑-删除了一般的参数。
container.Register(
Component.For(typeof(Context<>))
.ImplementedBy(typeof(GlobalContext<>)).LifestyleTransient());