0

suppose i have a interface and class like this

public interface ILienholderBusinessService
    {
        string GetCoverage(IList<PolicyVinRequest> InputList);
    }

public class LienholderBusinessService : ILienholderBusinessService
    {
        public ILienholderPolicySearchDataService LienholderPolicySearchDataService { get; set; }

        public LienholderBusinessService(ILienholderPolicySearchDataService  LienholderPolicySearchDataService )
        {
            this.LienholderPolicySearchDataService =LienholderPolicySearchDataService ;
        }
        public string GetCoverage(IList<PolicyVinRequest> InputList)
        {
            return "my string";

        }
    }

I have a controller like this

 public class InsuranceVerificationPortalController : Controller
    {
        public ILienholderBusinessService LienholderBusinessService { get; set; }
        public InsuranceVerificationPortalController(ILienholderBusinessService LienholderBusinessService)
        {
            this.LienholderBusinessService = LienholderBusinessService;
        }
}

now when i try to implement unity in my MVC, i get error like

are you missing any type

How can i implement DI using Unity on a class which has a constructor

1 Answer 1

1

You'll have to register mappings for all interfaces that you need to resolve concrete types for. From the posted code this would be ILienholderBusinessService and ILienholderPolicySearchDataService:

container.RegisterType<ILienholderBusinessService, LienholderBusinessService>();
container.RegisterType<ILienholderPolicySearchDataService, LienholderPolicySearchDataService>();

It could be that the implementation of ILienholderPolicySearchDataService also depends on interfaces/abstract classes (e.g. IRepository<T>). If that is the case then mappings will need to be created for those as well.

By default, Unity will select the constructor with the most number of parameters so if you need to choose another constructor you can use an InjectionConstructor. e.g.

container.RegisterType<ILienholderPolicySearchDataService, LienholderPolicySearchDataService>(
    new InjectionConstructor(typeof(IRepository<Policy>)));
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.