asp.net-mvc-3 - 具有 ninject 的插件架构 - 将类和 Controller 实例从插件程序集加载到主 MVC 项目

标签 asp.net-mvc-3 plugins ninject

我使用本教程在我的解决方案中创建插件架构,并且我也是第一次使用 ninject:

http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=358360&av=526320&msg=4308834#xx4308834xx

现在,在 MVC 应用程序中,当用户正在结帐过程中时,我会获取他选择的付款方式,并且需要检索所选付款方式的插件。我已经成功地以这种方式检索插件 Controller ,尽管我不知道它是否安全或可接受的做法:

Type type = Type.GetType(paymentMethod.PaymentMethodPluginType);  

 //get plugin controller

var paymentController = ServiceLocator.Current.GetInstance(type) as BasePaymentController;

//get validations from plugin

    var warnings = paymentController.ValidatePaymentForm(form);

        //get payment info from plugin

        var paymentInfo = paymentController.GetPaymentInfo(form);
        //…

我还需要访问一个插件类来处理付款。 我有一个接口(interface) IPaymentMethod

  public partial interface IPaymentMethod 
  {
   void  PostProcessPayment (PostProcessPaymentRequest postprocessPaymentRequest);        

  }

以及像这样的插件 PaymentProcessor

public class PluginPaymentProcessor :IPaymentMethod
    {        
        public void PostProcessPayment (PostProcessPaymentRequest postprocessPaymentRequest)
        {
            ///
        }

Now in MVC project I try to access PostProcessPayment method this way 

IPaymentMethod pluginpaymentmethod = ServiceLocator.Current.GetInstance<IPaymentMethod>(paymentMethod.PaymentProcessor);

这里 paymentMethod.PaymentProcessor 是“MyApp.Plugins.MyPlugin.PluginPaymentProcessor, MyApp.Plugins.MyPlugin,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”

   And want to use pluginpaymentmethod like i do in controller example

pluginpaymentmethod.PostProcessPayment(postProcessPaymentRequest);

但它抛出错误,资源未找到并且插件支付方法未加载。我该如何修复它,或者您可以建议任何具有类似实现的教程吗?谢谢。

最佳答案

假设您有一个名为 MyPlugin 的具体类,它具有 IPaymentMethod 接口(interface),那么您的 ninject 绑定(bind)应该类似于:

private static void RegisterServices(IKernel kernel){
    kernel.Bind<IPaymentMethod>().To<MyPlugin>().InRequestScope();
}

检查它是否位于 App_Start 文件夹下的 NinjectWebCommon.cs 类中。一个更棘手的场景可能是 IPaymentMethod 必须以与绑定(bind) Ninject IKernel 相同的方式注册:

kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);

这可能是一个更棘手的问题。

关于asp.net-mvc-3 - 具有 ninject 的插件架构 - 将类和 Controller 实例从插件程序集加载到主 MVC 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11672794/

相关文章:

grails - Grails xwiki插件中的编译错误

entity-framework - 在 MVC 5 项目上使用 ASP.NET 标识,但 httpcontext User.ProviderName 为 "AspNetSqlRoleProvider"

c# - 如何在_LogOnPartial.cshtml或_Layout.cshtml文件中使用RoleEnvironment.CurrentRoleInstance.Id?

c# - .NET 中的动态编译插件

xml - 如何下载xml文件中的xml内容

php - 恢复此修订按钮无法恢复内容

c# - 将构造函数参数传递给根对象的依赖项,作为 Ninject 中单个解析的一部分

c# - 需要帮助了解 Ninject 如何将 Nhibernate SessionFactory 实例放入 UnitOfWork 中?

asp.net-mvc-3 - 通过 MVC 3 中的依赖注入(inject)学习控制反转

c# - 包含 HTML 显示原始文本的数据库字段