2
votes

Question Background:

I am simply setting up a Unity container object, registering the types of the selected interface and the class that inherits it, then trying to return the implementation of it.

The code:

Unity:

namespace ABC.Tools.VersionControl
{
internal class Unity
{
    internal static ITfsVcPromotionManager CreateUnityObjects()
    {
        var unityContainer = new UnityContainer();

        unityContainer.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>();

        //****ERROR*****
        return unityContainer.Resolve<ITfsVcPromotionManager>();
    }
  }
}

The interface - ITfsVcPromotionManager:

namespace ABC.Tools.VersionControl.TfsVersionControl
{ 
    interface ITfsVcPromotionManager
    {
        void CheckoutTfsItem(IVersionControlItem tfsItem);

        int CheckinTfsItem(IVersionControlItem tfsItem);
    }
}

The class that inherits the above interface - TfsVcPromotionManager

namespace ABC.Tools.VersionControl.TfsVersionControl
{
internal class TfsVcPromotionManager:ITfsVcPromotionManager
{
    private ITfsVcQaCheckoutWorker _checkoutWorker;

    private ITfsVcQaCheckinWorker _checkInWorker;

    private VersionControlServer _tfsServer;

    private TfsVcCheckoutItem _checkoutItem = new TfsVcCheckoutItem();

    public TfsVcPromotionManager(/*IVersionControlItem tfsItem*/ ITfsVcQaCheckoutWorker checkOutWorker, ITfsVcQaCheckinWorker checkInWorker, VersionControlServer tfsServer)
    {
        if (checkOutWorker == null || tfsServer == null)
        {
            throw new System.ArgumentException("tfsItem or tfsServer objects cannot be null");
        }

        _checkoutWorker = checkOutWorker;

        _checkInWorker = checkInWorker;

        _tfsServer = tfsServer;
    }

    public void CheckoutTfsItem(IVersionControlItem tfsItem)
    {
        if (tfsItem == null)
        {
            throw new System.ArgumentException("TfsItem cannot be null.");
        }

        _checkoutWorker.CheckoutTfsQaItem(_tfsServer);
    }

    public int CheckinTfsItem(IVersionControlItem tfsItem)
    {
        if (tfsItem == null)
        {
            throw new System.ArgumentException("tfsItem cannot be null.");
        }

        return _checkInWorker.CheckinTfsQaItem(tfsItem);
    }
}

The error message:

Result Message: Test method ABCTestProject.TFStests.Check_Interface_CheckOut_Method threw exception: Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Adp.Tools.VersionControl.TfsVersionControl.ITfsVcPromotionManager", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, ABC.Tools.VersionControl.ITfsVcQaCheckoutWorker, is an interface and cannot be constructed. Are you missing a type mapping?

Can anyone tell me why this will not map correctly from the interface?

1
Have you tried registering the constructor arguments? It looks like Unity does not know how to build the arguments your constructor needs. - Gayot Fow

1 Answers

1
votes

It's complaining about not being able to resolve ITfsVcQaCheckoutWorker.

The only unity mapping you've done is for ITfsVcPromotionManager. When you resolve the interface, it creates an instance of TfsVcPromotionManager, whose constructor accepts two more interfaces: ITfsVcQaCheckinWorker and ITfsVcQaCheckoutWorker

You haven't registered those interfaces with unity (aka type mapping), so it doesn't know how to construct a concrete class for them.

I don't know what the names of your other classes are, but you should just be able to register them immediately after the other one:

internal static ITfsVcPromotionManager CreateUnityObjects()
{
    var unityContainer = new UnityContainer();

    unityContainer.RegisterType<ITfsVcPromotionManager, TfsVcPromotionManager>();
    unityContainer.RegisterType<ITfsVcQaCheckinWorker, TfsVcQaCheckinWorker>();
    unityContainer.RegisterType<ITfsVcQaCheckoutWorker, TfsVcQaCheckoutWorker>();

    return unityContainer.Resolve<ITfsVcPromotionManager>();
}