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.