Ninject with ria domainservice standard asp.net webapplication
I would like to use DI (Ninject) with my RIA webservices which is located on a standard asp.net webserver.
How should I hook up my repositories containing the datastore?
I've开发者_如何转开发 added a Global.asax file :
public class Global : NinjectHttpApplication
{
protected override Ninject.IKernel CreateKernel()
{
IKernel kernel = new StandardKernel();
kernel.Bind<IPersonRepository>().To<PersonRepository>();
Application.Add("Kernel", kernel);
return kernel;
}
And here's where I would like to hook it up,...but I'm stuck
[EnableClientAccess]
public class PersonService : DomainService
{
IPersonRepository _personRepository;
public PersonService()
{
_personRepository = ????....kernel.Get<IPersonRepository>();
}
}
It seems that I'm only missing a way to get the application object and then it would work, or?
I have no experience of RIA Web Services per se, but after a quick scan of the documentation I would suggest doing the following. Create an implementation of IDomainServiceFactory for Ninject.
public class NinjectDomainServiceFactory : IDomainServiceFactory
{
private readonly IKernel _kernel;
public NinjectDomainServiceFactory(IKernel kernel)
{
_kernel = kernel;
}
public DomainService CreateDomainService(
Type domainServiceType, DomainServiceContext context)
{
var service = _kernel.Get(domainServiceType);
service.Initialize(context);
return service;
}
public void ReleaseDomainService(DomainService domainService)
{
domainService.Dispose();
}
}
Register the custom domain service factory with the DomainService class in Application_Start.
public void Application_Start(object sender, EventArgs e)
{
var kernel = CreateKernel();
DomainService.Factory = new NinjectDomainServiceFactory(kernel);
}
Make sure you register your domain services and any dependencies with the kernel. The IPersonRepository should be injected in the constructor of the PersonService.
[EnableClientAccess]
public class PersonService : DomainService
{
private readonly IPersonRepository _personRepository;
public PersonService(IPersonRepository personRepository)
{
_personRepository = personRepository;
}
}
Hopefully this will be helpful as I haven't tried the solution myself.
精彩评论