在标准应用程序中注入自定义代码 [英] Inject custom code in standard application

查看:108
本文介绍了在标准应用程序中注入自定义代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们公司有一个标准的应用程序. 现在,我想在应用程序中注入一些自定义程序集.

We have a standard application in our company. Now I want to inject some custom assemblies in the application.

通常,如果您有ninject内核或unity容器,则可以像下面这样实现:

Normally if you have a ninject kernel or unity container, you can get the implementation like below:

IKernel kernel = new StandardKernel();
DealerService myServ = new DealerService(kernel.Get<IDealerController>());

DealerService:

DealerService:

public partial class DealerService : ServiceBase
{
    private readonly IDealerController Controller;

    public DealerService(IDealerController controller)
    {
        Controller = controller;

        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        Process();
    }

    protected override void OnStop()
    {
    }

    public void Process()
    {
        Controller.GetDealerDetails(5);
    }
}

DealerController接口

DealerController interface

public interface IDealerController
{
    Dealer GetDealerDetails(int dealerId);
}

经销商控制器的实现

public class DealerController : IDealerController
{
    public Dealer GetDealerDetails(int dealerId)
    {     
        throw new NotImplementedException("This is a custom error thrown from the controller implementation");
    }
}

现在,我不想让标准应用程序知道有关自定义程序集的信息. Dealerservice是一个工作流活动,因此可以通过工作流基础动态找到.问题是注入的DealerController为空... 如何在我的标准应用程序中插入IDealerController的实现而没有kernel.Get<IDealerController>()行.

Now the thing is that I don't want that the standard application knows about the custom assemblies. The dealerservice is a workflow activity so that is found dynamically by workflow foundation. The problem is that the injected DealerController is empty... How can I inject the implementation of the IDealerController in my standardapplication without the line kernel.Get<IDealerController>().

如果将Ninject与ninject MVC 3 nuget包一起使用,则将实现插入到mvc控制器的构造函数中,而无需调用kernel.Get<IDealerController>().

If you use Ninject with the ninject MVC 3 nuget package, the implementation is injected in the constructor of the mvc controller without calling the kernel.Get<IDealerController>().

添加了具有程序集名称Ninject.Extensions.Controllers.Module的模块,以便通过命名约定找到该模块. Ninject 文档中对此进行了描述.这在我的MVC应用程序中有效.

Added the module with as assembly name Ninject.Extensions.Controllers.Module so the Module is found by naming convention. This is described in the Ninject documentation. This works in my MVC application.

public class ControllersModule : NinjectModule
{
    public override void Load()
    {            
        this.Bind<IDealerController>().To<DealerController>();            
    }
}

推荐答案

如果要将IDealerController实现注入到DealerService中,还必须让Ninject管理DealerService的实例化. /p>

If you want to inject the IDealerController implementation into DealerService, you will also have to let Ninject to manage the instantiation of the DealerService.

IKernel kernel = new StandardKernel();
kernel.Bind<IDealerController>.To<DefaultDealerController>(); //.InSingletonScope();
kernel.Bind<DealerService>().ToSelf(); //.InSingletonScope();
DealerService myServ = kernel.Get<DealerService>();

要动态加载Ninject配置,可以使用模块方法和kernel.Load("*.dll")方法.如果某个接口有多种实现,则还应在

To dynamically load Ninject configuration you can use modules approach and kernel.Load("*.dll") method. If you will have multiple implementations of some interface, you should also specify which one should be used with the help of the conditional binding.

kernel.Bind<IDealerController>
      .To<SpecialDealerController>()
      .When(x=> SpecialConditionIsMet());

这篇关于在标准应用程序中注入自定义代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆