1

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)
    {

    }
}
1
  • ADDED UNITY CONFIGURATION. Commented Feb 22, 2014 at 17:25

1 Answer 1

1

You need to register a type mapping for your interface ILogger. Otherwise does Unity not now which implementation of ILogger it should use (see your exception).

Here´s an example registering the interface ILogger with a mapping to FileLogger:

_container.RegisterType<ILogger, FileLogger>();

IProduct product = _container.Resolve<ProductA>(); //ILogger is injected to Product
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Jehof. IProduct have different product implementation and inside the specific product class we need to use some other classes object like FileLogger which are derived from some interface having different implentation. When we resolve ProductManager Class for ProductA want to resolve FileLogger and for ProductB DBLogger. Please suggest.
Then you need to do a similar registration for ProductA which has an InjectionConstructor(container.Resolve<ILogger>("File")) and also ProductB.
Thanks Tyler, you are right. But finally i have decided to use CI for ProductA,ProductB instead of registering manager class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.