如何配置Unity以创建一个包含两个相同类型的不同项的类?

时间:2022-04-11 14:07:56

I am still getting started with Unity, and have what seems to be a simple question.

我仍然开始使用Unity,并且看起来似乎是一个简单的问题。

I have a class that has a dependency on two different instances of the same interface. How do I configure and resolve this class?

我有一个类依赖于同一接口的两个不同实例。如何配置和解决此课程?

I.E. Given:

public interface ILogger
{
    void Write(string message);
}
public class ConsoleLogger : ILogger
{
    public void Write(string message) 
    {
        Console.WriteLine(message);
    }
}
public class AnotherLogger : ILogger
{
    public void Write(string message)
    {
        Console.WriteLine(DateTime.Now.ToString() + " " + message);
    }
}
public class CombinedLogger : ILogger
{
    IList<ILogger> _loggers;
    public CombinedLogger(params ILogger[] loggers)
    {
         _loggers = new List<ILogger>(loggers);
    }
    public void Write(string message)
    {
         foreach(var logger in _loggers) logger.Write(message);
    }
}

I know how to configure for ConsoleLogger, and AnotherLogger. I also know how to access them in the actual code. What I seem to be blocking on is figuring out how to configure and use CombinedLogger, passing in the instances of ConsoleLogger and AnotherLogger.

我知道如何配置ConsoleLogger和AnotherLogger。我也知道如何在实际代码中访问它们。我似乎阻止的是弄清楚如何配置和使用CombinedLogger,传入ConsoleLogger和AnotherLogger的实例。

3 个解决方案

#1


Read the documentation on configuration support for arrays.

阅读有关阵列配置支持的文档。

#2


IUnityContainer container = new UnityContainer();
container.RegisterType<ILogger, ConsoleLogger>();
container.RegisterType<ILogger, AnotherLogger>("another");
container.RegisterType<ILogger, CombinedLogger>("combined");
var instances = container.ResolveAll<ILogger>();

#3


You use a named registration.

您使用命名注册。

myContainer.RegisterType("ConsoleLogger"); myContainer.RegisterType("AnotherLogger"); myContainer.RegisterType("CombinedLogger");

myContainer.RegisterType( “ConsoleLogger”); myContainer.RegisterType( “AnotherLogger”); myContainer.RegisterType( “CombinedLogger”);

Then when you resolve the type, you used the name to get the specific one

然后,当您解析该类型时,您使用该名称来获取特定的名称

public class CombinedLogger : ILogger{    
IList<ILogger> _loggers;    
public CombinedLogger(params ILogger[] loggers)    
{         
_loggers = new List<ILogger>();    
_loggers.Add(myContainer.Resolve(Of ILogger)("ConsoleLogger")
_loggers.Add(myContainer.Resolve(Of ILogger)("AnotherLogger")
}    
public void Write(string message)    
{         
foreach(var logger in _loggers) logger.Write(message);    
}
}

#1


Read the documentation on configuration support for arrays.

阅读有关阵列配置支持的文档。

#2


IUnityContainer container = new UnityContainer();
container.RegisterType<ILogger, ConsoleLogger>();
container.RegisterType<ILogger, AnotherLogger>("another");
container.RegisterType<ILogger, CombinedLogger>("combined");
var instances = container.ResolveAll<ILogger>();

#3


You use a named registration.

您使用命名注册。

myContainer.RegisterType("ConsoleLogger"); myContainer.RegisterType("AnotherLogger"); myContainer.RegisterType("CombinedLogger");

myContainer.RegisterType( “ConsoleLogger”); myContainer.RegisterType( “AnotherLogger”); myContainer.RegisterType( “CombinedLogger”);

Then when you resolve the type, you used the name to get the specific one

然后,当您解析该类型时,您使用该名称来获取特定的名称

public class CombinedLogger : ILogger{    
IList<ILogger> _loggers;    
public CombinedLogger(params ILogger[] loggers)    
{         
_loggers = new List<ILogger>();    
_loggers.Add(myContainer.Resolve(Of ILogger)("ConsoleLogger")
_loggers.Add(myContainer.Resolve(Of ILogger)("AnotherLogger")
}    
public void Write(string message)    
{         
foreach(var logger in _loggers) logger.Write(message);    
}
}