We have following implementation of IProduct & ILogger class. Manager classes are being used as communication channel. We have used unity as given below to resolve manager class but it giving following error-
===================unity configuration==========
IUnityContainer container = new UnityContainer();
container.RegisterType<ILogger, FileLogger>("File");
container.RegisterType<ILogger, BDLogger>("DB");
container.RegisterType<IProduct, ProductA>("productA");
container.RegisterType<IProduct, ProductB>("productB");
container.RegisterType<ProducManager>(new InjectionConstructor(container.Resolve<IProduct>("productA")));
container.Resolve<ProducManager>("productA");
========================================================
"Resolution of the dependency failed, type = \"usingDI.IProduct\", name = \"productA\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The current type, usingDI.ILogger, is an interface and cannot be constructed. Are you missing a type mapping?\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n Resolving usingDI.ProductA,productA (mapped from usingDI.IProduct, productA)\r\n Resolving parameter \"logger\" of constructor usingDI.ProductA(usingDI.ILogger logger)\r\n Resolving usingDI.ILogger,(none)\r\n"}"
Please suggest me best way to resolve dependency for manager classes.
public interface IProduct
{
void dosomething();
}
public class ProducManager
{
IProduct _product;
public ProducManager(IProduct product)
{
_product = product;
}
public void DoSomething()
{
_product.dosomething();
}
}
public class ProductA : IProduct
{
ILogger _logger;
public ProductA(ILogger logger)
{
_logger = logger;
}
public void dosomething()
{
try
{
//code
//
}
catch (Exception ex)
{
_logger.WriteLog(ex.Message);
}
}
}
public class ProductB : IProduct
{
ILogger _logger;
public ProductB(ILogger logger)
{
_logger = logger;
}
public void dosomething()
{
try
{
//code
//
}
catch (Exception ex)
{
_logger.WriteLog(ex.Message);
}
}
}
public class LogManager
{
ILogger _logger;
public LogManager(ILogger logger)
{
_logger = logger;
}
public void WriteLog(string exception)
{
_logger.WriteLog(exception);
}
}
public interface ILogger
{
void WriteLog(string exception);
}
public class FileLogger : ILogger
{
public void WriteLog(string exception)
{
}
}
public class BDLogger : ILogger
{
public void WriteLog(string exception)
{
}
}