1

I have a service class :

public class MyService : IService
{
    private IRepoA repoA;
    private IRepoB repoB;

    public MyService(IRepoA repoA,IRepoB repoB)
    {
        this.repoA=repoA;
        this.repoB=repoB;
    }
}

my Controller depends on the MyService classL

public MyController : DefaultController
{
    IService myService;
    public MyController(IService service)
    {
        myService=service;
    }
 }

how do I inject the MyService class into the controller ? should i do this ? :

 Bind<IPostService>()
                .To<PostService>();

            Bind<IPostsRepository>()
                .To<SqlPostsRepository>()
                .WithConstructorArgument("connectionString", 
 ConfigurationManager.ConnectionStrings[0].ConnectionString);


            Bind<IUserRepository>()
                .To<FakeUserRepository>();

2 Answers 2

3

Look at Ninject.Extensions.Mvc. Read the read me and documentation. It has everything you need. From the looks of it, all you'll need is to properly setup your HttpApplication to inherit from NinjectHttpApplication and add the proper config code (all located in the readme/documenation)

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

Comments

2

You'll want to create your own controller factory that injects the dependencies for you and register that factory in Global.asax.

If you're using structure map, that could look like this:

public class IoCControllerFactory : DefaultControllerFactory   
{  
    protected override IController GetControllerInstance(Type controllerType)  
    {
        return (Controller)ObjectFactory.GetInstance(controllerType);  
    }  
}  

1 Comment

Regardless of Ioc container you should be doing this to then find the rest of the dependencies down the chain. :)

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.