Unity: Implicit ResolvedParameter for unnamed registrations

前端 未结 2 1859
心在旅途
心在旅途 2020-12-09 06:36

The UserService constructor has two parameters, a IUnitOfWork and a IUserRepository:

public UserService(IUnitOfWork un         


        
2条回答
  •  死守一世寂寞
    2020-12-09 07:21

    Would you be willing to decorate your constructor with the DependencyAttribute from Unity? This solution is straight forward, built-in, and lets you pick and chose named dependency. But it does 'dirty' your constructor with Unity goo.

    public UserService(
        [Dependency("someContext")]IUnitOfWork unitofWork, 
        IUserRepository userRepository) 
    { ... }
    

    Another solution would be to write a custom BuilderStrategy and UnityContainerExtension. This could be done with a bit more work.

提交回复
热议问题