0

I have a problem that I don't know how to solve. I have a container which contains different interfaces for different services that I expose with a WebApi. The problem is that I need to get that dependencies inside my controller and I must avoid the use of static. I read about this

http://beletsky.net/2011/10/inside-aspnet-mvc-idependencyresolver.html

http://www.asp.net/web-api/overview/advanced/dependency-injection

In the asp.net I read that I can implement my own IDependencyResolver. Is this madness? because I searched a lot and I only found examples using Unity. If I don't want to use that dependency injector? What it's the best way to achieve this?.

public class MyController: ApiController
{
    private InterfaceService m_interfaceService; //This is the dependency I need

    public MyController()
    {

    }

    [HttpGet]
    [Route("myServices/")]
    public List<IServiceCategory> GetServiceObjectsList()
    {
        return m_interfaceServices.GetObjectsList();
    }

}
5
  • 1
    Don't use unity if you don't like it, but don't write your own DI container. Commented Jun 9, 2016 at 14:26
  • So what can I do? The container which has the different interfaces, is "some kind" of DI made by my team. That's the reason why I can't use a DI like Unity Commented Jun 9, 2016 at 14:29
  • @RobertMoskal Why do you not suggest writting your own DI container? Commented Jun 9, 2016 at 15:02
  • Concentrate on solving your domain problems! Commented Jun 9, 2016 at 15:03
  • Why would you write a DI-Container when a bunch of good ones already exist? I have the same discussions over and over. "Let's write our own scheduling app". "NO, let's use Quartz.Net". "Let's write our own messaging-service-bus".. "NO, let's use RabbitMQ or Azure-Message-Bus". Same conversations, over and over. Commented Jun 9, 2016 at 17:28

3 Answers 3

1

So you have an pre-existing container/dependency mechanism! You should ask your team why it was a good idea to make something like that instead of using all the good ones out in the .net world.

Nonetheless, from the docs:

http://www.asp.net/web-api/overview/advanced/dependency-injection

Although you could write a complete IDependencyResolver implementation from scratch, the interface is really designed to act as bridge between Web API and existing IoC containers.

The Unity example on that page shows what must be done to bridge the gap between Unity DI framework and the web mvc. You just need to do the same thing with your home-rolled one. It's matter of implementing just a few methods. Go for it!

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

2 Comments

Thanks for your response. The main problem about this maybe is that I won't have all the configuration part that this DI provides us.
Nonetheless you have to try! Good luck!
1

Here is an implementation that meets your requirements:

namespace AdvancedDI.Controllers
{
    public class ProductController : ApiController
    {
        public IFactory iFactory { get; set; }

        protected override void Initialize(HttpControllerContext controllerContext)
        {
            DIAPP.GetContainer(this);
            base.Initialize(controllerContext);
        }

        public IHttpActionResult Get()
        {
            var response = iFactory.DoWork();
            return Ok(response);
        }

        protected override void Dispose(bool disposing)
        {
            DIAPP.Dispose(this);
            base.Dispose(disposing);
        }
    }
}

This is one kind of property based injection. It is possible with UnityContainerExtensions. The Initialize method will get called before Get.

Step 1. DIAPP.GetContainer(this) carries the entire productController context.

Step 2. GetContainer receives the IFactory property information from this.

Step 3. Next, you have a chance to receive the unity IBuilderContext for this IFactory.

2 Comments

Please explain how this code works and what it does.
This is one kind of property based injection. It can be possible with UnityContainerExtensions. The Initialize method will call before Get. step 1: DIAPP.GetContainer(this) carries entire productController context. step 2: GetContainer receives 'IFactory' property information from this. step 3: next you are having a chance to receives unity IBuilderContext for this IFactory.(Please refer more information about (UnityContainerExtensions)
0

I've used both Ninject and AutoFac for dependency injection. This is not madness, it's common practice.

3 Comments

I mean if I implement IDependencyResolver. Is that a bad idea? Because our container is in some way a "dependency injector" and we can not include a 3rd one.
Don't build a dependency resolver, use a prebuilt one (ie the ones I listed).
Don't use a IDependencyResolver at all. use IHttpControllerActivator. See blog.ploeh.dk/2012/10/03/…

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.