Ninject 与 ria 域服务标准 asp.net web 应用程序

发布于 2024-11-09 21:02:37 字数 794 浏览 9 评论 0原文

我想将 DI (Ninject) 与位于标准 asp.net Web 服务器上的 RIA Web 服务一起使用。

我应该如何连接包含数据存储的存储库?

我添加了一个 Global.asax 文件:

    public class Global : NinjectHttpApplication
{
    protected override Ninject.IKernel CreateKernel()
    {
        IKernel kernel = new StandardKernel();
        kernel.Bind<IPersonRepository>().To<PersonRepository>();
        Application.Add("Kernel", kernel);
        return kernel;
    }

这是我想连接它的地方,...但我被卡住了

[EnableClientAccess]
public class PersonService : DomainService
{
    IPersonRepository _personRepository;        

    public PersonService()
    {    
             _personRepository = ????....kernel.Get<IPersonRepository>();

    }
}

似乎我只是缺少一种获取应用程序对象的方法,然后它就可以工作了,或者?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云朵有点甜 2024-11-16 21:02:37

我本身没有 RIA Web 服务的经验,但在快速浏览文档后,我建议执行以下操作。创建 IDomainServiceFactory 用于 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();
    }

}

使用 DomainService 类。

public void Application_Start(object sender, EventArgs e)
{
    var kernel = CreateKernel();
    DomainService.Factory = new NinjectDomainServiceFactory(kernel);
}

确保您向内核​​注册了域服务和任何依赖项。 IPersonRepository 应该被注入到 PersonService 的构造函数中。

[EnableClientAccess]
public class PersonService : DomainService
{
    private readonly IPersonRepository _personRepository;        

    public PersonService(IPersonRepository personRepository)
    {
        _personRepository = personRepository;
    }
}

希望这会有所帮助,因为我自己还没有尝试过该解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文