0

I'm currently injecting dependencies into controllers using a IoC container (Castle). This is possible because you need to create a custom controller factory which enables the dependency injection.

What are other examples of dependency injection? At which point in an MVC application would you use it, and where does a 'factory' come into play?

2
  • If you use Castle, I would suggest that code.google.com/p/sutekishop, personally, I feel it's a very good example to follow regarding how to use IOC Commented Feb 12, 2011 at 19:54
  • I'm looking at Sutekishop but cannot download the latest source code? Commented Feb 13, 2011 at 13:41

1 Answer 1

3

I am using Ninject. At my project:

  • Service layer objects are injected into controllers (using constructor).
  • Repositories are injected into service layer objects (using constructor).
  • ObjectContext is injected into repositories (using constructor).
  • web.config setting are encapsulated into a class, which implements IAppSettings interface, which is then injected into service layer.
  • NinjectActionInvoker is injected as IActionInvoker. It takes care of injecting services into ActionFilters.
  • I have my own implementation of IPrincipal interface, which is injected into service layer, instead of referring to HttpContext.Current.User.

Example using Ninject:

public class UserService : GenericService<User>, IUserService
{
    public ISettingService SettingService { get; set; }
    public ICTEmailSender CTEmailSender { get; set; }
    public ICTSettings CTSettings { get; set; }
    public ICTPrincipal User { get; set; }
}

Ninject rules:

Bind<ICTPrincipal>().ToMethod(c => (ICTPrincipal)HttpContext.Current.User).OnlyIf(a => HttpContext.Current.User is ICTPrincipal);
Bind<ICTEmailSender>().To<CTEmailSender>();
Bind<ICTSettings>().To<CTSettings>();

Not only service is injected into controller, but parts of service are injected into it. It makes service more testable. I am sure it can be easily ported into Castle.

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

15 Comments

In other words: Dependency Injection all the way ;-)
Pretty interesting, any example that we can follow
Yes this should port easily. I was a bit off track focusing on a factory that needed to be applied, however this is not the case. You can also inject methods and other objects without a factory.
Is it typical for different components (like: Controllers, Services, Repositories) to be 'installed' using separate configurations (e.g. IWindsorInstaller in Windsor)? I'm deducting from documentation that this is indeed the case. However it's also allowing for only one 'binding' per dependency. What if two components depend on the same object, where (in which installer) would you configure or install this binding?
@Ropstah: I am not an expert in Windsor, but from what I see in documentation, you should have it in installer related to this object's class. If this object is Repository, you should place it in RepositoryInstaller.
|

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.