显然我正在尝试注册一个接口,但是看来我无法在非具体服务中注册它,这是实现的一个示例
interface IClassA
{
void Students(List<string> students);
}
class School
{
public School(IClassA classA)
{
}
}
class Program
{
public static Rules DefaultRules => Rules.Default.WithConcreteTypeDynamicRegistrations()
.With(Made.Of(FactoryMethod.ConstructorWithResolvableArguments))
.WithFuncAndLazyWithoutRegistration()
.WithTrackingDisposableTransients()
.WithoutFastExpressionCompiler()
.WithDefaultIfAlreadyRegistered(IfAlreadyRegistered.Replace)
static void Main(string[] args)
{
var container = new Container(DefaultRules);
//this throws
container.Register<IClassA>();
}
}
以下是错误异常:
DryIoc.ContainerException:代码:RegisteringAbstractImplementationTypeAndNoFactoryMethod;消息:应在具体时注册抽象实现类型IClassA
答案 0 :(得分:0)
您需要注册IClassA
接口的具体实现,因为容器需要具体的实例化。
规则WithConcreteTypeDynamicRegistrations
用于具体的实现依赖项(用于类而不是用于接口),因此您根本不需要注册它们。像这样:
class School
{
public School(MyClassA classA)
{
}
}
public static class Program
{
static void Main(string[] args)
{
var container = new Container(DefaultRules);
// it works even without registering School and MyClassA
container.Register<School>();
}
}