2

I have a web API controller, and I am using Unity.WebAPI for resolving dependency. Following is my Bootstrapper.cs code,

public static class Bootstrapper
{
    /// <summary>
    /// Public method to initialise UnityContainer.
    /// </summary>
    public static void Initialise()
    {
        var container = BuildUnityContainer();
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        // register dependency resolver for WebAPI RC
        GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
    }

    /// <summary>
    /// Register built in types.
    /// </summary>
    /// <returns></returns>
    private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();
        container.RegisterType<IUserService, UserService>().RegisterType<UnitOfWork>(new HierarchicalLifetimeManager());

        return container;
    }
}

This resolves my UserService to my UsersController , my controller code is,

private readonly IUserService _userService;




/// &lt;summary&gt;
/// Public constructor to initialize user service instance
/// &lt;/summary&gt;
/// &lt;param name=&quot;userService&quot;&gt;&lt;/param&gt;
/// &lt;param name=&quot;tokenService&quot;&gt; &lt;/param&gt;
public UsersController(IUserService userService)
{
    _userService = userService;

}

Now suppose My UserService also depends on any other class say Employee and needs a new Employee() instance wheneven UserService instance is needed. How can I resolve the dependency of this new Employee() class in my UserService. Also note that Employee class does not Implement any Interface.

Please answer, or let me know if any further clarification is needed. I hope I am clear in asking the question.

2 Answers 2

2

Your dependency injector can (should) only inject behaviour classes (services, handlers, managers, etc), it cannot (should not) inject data holder classes (entities, poco's, etc). So you have to change your design a bit so the Employee instance can be provided to your IUserService.

For instance, your IUserService can depends on an IEmployeeProvider which can be injected. And your employee provider can load/retrieve the employee-to-inject from wherever you have stored it (database, session). It all depends on where your application decides which employee to use.

Normally your composition root is created at startup, and cannot (should not) change afterwards.

Your UsersController is created directly by the DependencyResolver, and at that moment your employee is not yet known.

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

2 Comments

Do not get confuse by the name Employee class, it may be any class XYZ.All I want is that when my UserService is instantiated I want that XYZ class on which the UserService depends also gets instantiated.Where and How to write that code?
You have to register it in your container just like you do with the IUserService registration.
0

You just register the type mappings you need in your BuildUnityContainer method.

See the unity documentation on how to use factory methods or register instances.

2 Comments

Please suggest what code exactly has to be written and where?
See this for registering type mappings, and here on how to register instances

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.