1

Problem:

I'm using MVC4 and WebAPI. I've installed the Unity.WebAPI package and am stuck.

I don't know what needs to be registered in the Initialise() in bootstrapper.cs? Interface and Class or my Controller?

using System.Web.Http;
using Microsoft.Practices.Unity;
using unitywapi4.Models;

namespace unitywapi4
{
    public static class Bootstrapper
    {
        public static void Initialise()
        {
            var container = BuildUnityContainer();

            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }

        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer();

            // register all your components with the container here
            // e.g. container.RegisterType<ITestService, TestService>();       

            container.RegisterType<IProductRepository, Product>();

            return container;
        }
    }
}

Error:

Cannot be used as type parameter 'TTo' in the generic type or method 'Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType(Microsoft.Practices.Unity.IUnityContainer, params Microsoft.Practices.Unity.InjectionMember[])'.**

There is no implicit reference conversion from 'unitywapi4.Models.Product' to 'unitywapi4.Models.IProductRepository'.

1 Answer 1

1

Apparently...

Mapping types is useful for retrieving instances of different objects that implement the same specified interface or that inherit from the same specified base class. The target type for the mapping must inherit from or implement the base type or interface of the source. You can generate both default and named mappings for a type registration by using the generic overloads of the container methods.

http://msdn.microsoft.com/en-us/library/ff660923%28v=PandP.20%29.aspx#registertype_simple

Sign up to request clarification or add additional context in comments.

Comments

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.