Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 简单注入器-注入器容器属性_C#_Containers_Ioc Container_Simple Injector - Fatal编程技术网

C# 简单注入器-注入器容器属性

C# 简单注入器-注入器容器属性,c#,containers,ioc-container,simple-injector,C#,Containers,Ioc Container,Simple Injector,我想通过SimpleInjector注入容器属性。我没有找到SimpleInjector的任何功能 然后我想将self容器注册到它自己,但容器并没有接口 我想要这个功能,因为我不需要通过构造函数传输容器对象——因为如果我可以使用寄存器对象的自动注入,为什么要这样做呢 我的使用理念: var container = new Container(); container.Options.AutowirePropertiesWithAttribute<InjectableProperty>

我想通过SimpleInjector注入容器属性。我没有找到SimpleInjector的任何功能

然后我想将self容器注册到它自己,但容器并没有接口

我想要这个功能,因为我不需要通过构造函数传输容器对象——因为如果我可以使用寄存器对象的自动注入,为什么要这样做呢

我的使用理念:

var container = new Container();
container.Options.AutowirePropertiesWithAttribute<InjectableProperty>();
container.Register<ISomething, Something>(Lifestyle.Singleton);
某类事物:

public class Something : ISomething 
{
    public void SomeMethod() 
    {
       var environment = _container.GetInstance<IEnvironment>();
       environment.DoSomething();
    }

    [InjectableProperty] // - maybe it is not possible (I don't know it)
    Container Container {get;set;}
}
公共类某物:等轴物
{
公共方法()
{
var环境=_container.GetInstance();
环境。DoSomething();
}
[InjectableProperty]/-可能不可能(我不知道)
容器容器{get;set;}
}
你有什么办法来达到这个目的吗


非常感谢。

防止应用程序代码依赖于容器。应用程序中唯一应该知道DI库存在的地方是(注册所有依赖项的地方)

与其让每个类调用回容器(称为),不如使用依赖项注入。使用依赖项注入,您可以注入依赖项,而不是请求依赖项

因此,您可以将类重写为以下内容:

public class Something : ISomething 
{
    private readonly IEnvironment environment;

    public Something (IEnvironment environment)
    {
       this.environment = environment;
    }

    public void SomeMethod()
    {
       this.environment.DoSomething();
    }
}
此外,除了存储传入的依赖项之外,还要防止在构造函数中执行任何逻辑。这使您可以

但是,在某些情况下,将
容器
注入另一个类仍然很有用。例如,当创建位于合成根中的工厂类时。在这种情况下,您仍然可以使用构造函数注入,如下所示:

// Defined in an application layer
public interface IMyFactory
{
    IMyService CreateService();
}

// Defined inside the Composition Root
public class MyFactory : IMyFactory
{
    private readonly Container container;

    public MyFactory(Containter container)
    {
        this.container = container;
    }

    public IMyService CreateService(ServiceType type)
    {
        return type == ServiceType.A
            ? this.container.GetInstance<MyServiceA>()
            : this.container.GetInstance<MyServiceB>();
    }
}
//在应用程序层中定义
公共接口IMyFactory
{
IMyService CreateService();
}
//在合成根中定义
公共类MyFactory:IMyFactory
{
专用只读容器;
公共工厂(集装箱)
{
this.container=容器;
}
公共IMyService CreateService(服务类型)
{
返回类型==ServiceType.A
?this.container.GetInstance()
:this.container.GetInstance();
}
}

如果Simple Injector检测到一个
容器
构造函数参数,它会自动将自身注入构造函数。

你能发布一些代码吗?添加了示例代码…非常感谢。这对我来说是正确的答案!我猜第一个使用environment param的SomeMethod是错误的,它是构造函数,不是吗?@Frk:应该是构造函数。修复了它。您好,先生@Steven,所以我可以使用工厂内的容器来创建以前注册的服务的实例“如果Simple Injector检测到容器构造函数参数,它将自己注入构造函数。对此您无需做任何事。”=>最后一行很有用。
// Defined in an application layer
public interface IMyFactory
{
    IMyService CreateService();
}

// Defined inside the Composition Root
public class MyFactory : IMyFactory
{
    private readonly Container container;

    public MyFactory(Containter container)
    {
        this.container = container;
    }

    public IMyService CreateService(ServiceType type)
    {
        return type == ServiceType.A
            ? this.container.GetInstance<MyServiceA>()
            : this.container.GetInstance<MyServiceB>();
    }
}