C# 使用unity通过构造函数注入实现DI

C# 使用unity通过构造函数注入实现DI,c#,dependency-injection,unity-container,C#,Dependency Injection,Unity Container,我是DI模式的新手…现在刚刚开始学习。我得到了一个使用unity的构造函数注入代码。这是代码 public class CustomerService { public CustomerService(LoggingService myServiceInstance) { // work with the dependent instance myServiceInstance.WriteToLog("SomeValue"); } } IUnityContain

我是DI模式的新手…现在刚刚开始学习。我得到了一个使用unity的构造函数注入代码。这是代码

public class CustomerService
{
  public CustomerService(LoggingService myServiceInstance)
  { 
    // work with the dependent instance
    myServiceInstance.WriteToLog("SomeValue");
  }
} 

IUnityContainer uContainer = new UnityContainer();
CustomerService myInstance = uContainer.Resolve<CustomerService>();
公共类客户服务
{
公共客户服务(LoggingService myServiceInstance)
{ 
//使用依赖实例
myServiceInstance.WriteToLog(“SomeValue”);
}
} 
IUnityContainer uContainer=新的UnityContainer();
CustomerService myInstance=uContainer.Resolve();

在这里,我们可以看到CustomerService正在查找LoggingService实例,但在这里,当我们通过resolve创建CustomerService实例时,我们没有传递LoggingService实例。那么告诉我,威尔是如何工作的。任何一个解释它的小完整的样本代码。谢谢

代码如下所示:

public interface ILoggingService
{
    void WriteToLog(string logMsg);
}

public class LoggingService : ILoggingService
{
    public void WriteToLog(string logMsg)
    {
        ... WriteToLog implementation ...
    }
}

public interface ICustomerService
{
    ... Methods and properties here ...
}

public class CustomerService : ICustomerService
{

    // injected property
    public ISomeProperty SomeProperty { get; set; }

    public CustomerService(ILoggingService myServiceInstance)
    { 
        // work with the dependent instance
        myServiceInstance.WriteToLog("SomeValue");
    }
} 

...
...

// Bootstrap the container. This is typically part of your application startup.
IUnityContainer container = new UnityContainer();
container.RegisterType<ILoggingService, LoggingService>();

// Register ICustomerService along with injected property
container.RegisterType<ICustomerService, Customerservice>(
                            new InjectionProperty("SomeProperty", 
                                new ResolvedParameter<ISomeInterface>()));
...
...

ICustomerService myInstance = container.Resolve<ICustomerService>();
公共接口ILoggingService
{
void WriteToLog(字符串logMsg);
}
公共类日志服务:ILoggingService
{
public void WriteToLog(字符串logMsg)
{
…写日志实现。。。
}
}
公共接口ICCustomerService
{
…这里的方法和属性。。。
}
公共类CustomerService:ICCustomerService
{
//注入特性
公共属性SomeProperty{get;set;}
公共CustomerService(ILoggingService myServiceInstance)
{ 
//使用依赖实例
myServiceInstance.WriteToLog(“SomeValue”);
}
} 
...
...
//引导容器。这通常是应用程序启动的一部分。
IUnityContainer容器=新的UnityContainer();
container.RegisterType();
//将ICustomerService与注入的属性一起注册
container.RegisterType(
新建InjectionProperty(“SomeProperty”,
新解析参数());
...
...
ICCustomerService myInstance=container.Resolve();
因此,当您解析ICCustomerService接口时,unity将返回CustomerService的新实例。当它实例化CustomerService对象时,它将看到它需要ILogingService实现,并将确定LoggingService是它想要实例化的类

还有很多,但这是最基本的


更新-添加了参数注入

您的问题并不完全清楚。LoggingService可能是一个接口,而不是一个具体类型,当您引导容器时,您将为它提供从LoggingService接口到具体类型的映射。当您要求容器重新调用CustomerService时,它会看到构造函数采用LoggingService接口,并尝试解决该问题,然后将LoggingService实例传递给构造函数。您真正需要的是ICCustomerService和ILoggingService接口,而这些正是您要解决的问题。当您引导容器时,您将创建从ICCustomerService到CustomerService的映射,以及从ILoggingService到LoggingServices的映射。因此,我可以更好地可视化,因为我在DI方面比较弱。u plzz是否可以使用setter而不是构造函数为依赖项注入提供另一个示例代码。只需复制您的第一个代码,并进行一些更改,以便使用setter实现它。thanksI在示例中添加了一个属性注入示例。