简体   繁体   English

Autofac注册多个接口层次结构,多个具体类

[英]Autofac register multiple interface hierachy, multiple concrete classes

i don't seem to find an answer for my kind of problem.我似乎没有为我的问题找到答案。 So, let's say have an interface所以,假设有一个接口

public interface IBase {        
void MethodBase();    
}

and a class implementing this, BaseClass not abstract).和一个 class 实现这个,BaseClass 不是抽象的)。 Now i must have another class that should implement the base interface but also to add new methods (extending the default behaviour), so i'm making my second interface like this现在我必须有另一个 class 应该实现基本接口但还要添加新方法(扩展默认行为),所以我正在制作这样的第二个接口

public interface IChild : IBase {        
void MethodChild();    
}

and a class that implements this one, say ChildClass.和一个实现这个的 class,比如 ChildClass。

All good for now.目前一切都很好。 Now the problem with IoC and Autofac.现在是 IoC 和 Autofac 的问题。 I want, on application start up, to conditionally register as IBase (because the project allready uses IBase in other parts) the first or the second concrete class.我希望在应用程序启动时有条件地注册为 IBase(因为该项目已经在其他部分使用 IBase)第一个或第二个具体 class。

The question is, in a consuming class, in its constructor, should i have a parameter of type IBase and in one of its methods should i do问题是,在使用 class 的构造函数中,我是否应该有一个 IBase 类型的参数,并且在它的一种方法中我应该这样做

(baseTypeParam as IChild)

and use one of the new methods added by IChild interface?并使用 IChild 接口添加的新方法之一? Because if the constructor parameter is of type IChild and as i said, i register IBase with ChildClass then everything fails because, obviously, there is no IChild registered.因为如果构造函数参数的类型是 IChild 并且正如我所说,我使用 ChildClass 注册 IBase 那么一切都会失败,因为显然没有注册 IChild。

Or i am missing something about how to extend (design pattern?) the base interface with new methods, that the BaseClass should not implement.或者我遗漏了一些关于如何使用新方法扩展(设计模式?)基本接口的东西,BaseClass 不应该实现。

You can't cast a parent to a child as you don't know that the implementation you are referencing by IBase actually implements IChild methods.您不能将父级强制转换为子级,因为您不知道 IBase 引用的实现实际上实现了 IChild 方法。 If your class requires methods defined in IChild, it should ask for IChild.如果您的 class 需要在 IChild 中定义的方法,它应该要求 IChild。

A cleaner solution would to have two implementations of IBase, and not have IChild inherit from IBase.一个更干净的解决方案是有两个 IBase 实现,而不是 IChild 从 IBase 继承。 Then you can register which ever implementation of IBase you want depending on your start up parameter, have have a separate class implement IChild.然后你可以根据你的启动参数注册你想要的 IBase 实现,有一个单独的 class 实现 IChild。

public interface IBase {        
void DoSomething();    
}

public class Base1 : IBase{
public void DoSomething() { \\do something } 
}

public class Base2 : IBase{
public void DoSomething() { \\do something } 
}

public interface IChild{
void DoSomethingElse();
}

public class Child1{
public void DoSomethingElse() { \\do something else }
}

public class IDoSomething{
public IDoSomething(IBase base){}
}

public class IDoMoreThings{
public IDoMoreThings(IBase base, IChild child){}
}

I understand that it might have made sense for IChild to inherit from IBase but without more context I can't really say.我知道 IChild 从 IBase 继承可能是有意义的,但如果没有更多上下文,我真的不能说。

Hope I understood you question correctly.希望我能正确理解你的问题。

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

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