插件体系结构与ninject - 从插件程序集到主MVC项目的加载类和控制器实例

时间:2012-07-26 15:31:09

标签: asp.net-mvc-3 plugins ninject

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

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

现在,在用户处于结账过程中的MVC应用程序中,我获得了他选择的付款方式,需要检索所选付款方式的插件。我成功地以这种方式检索插件控制器,虽然我不知道它是安全的还是可接受的练习:

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);
        //…

我还需要访问一个插件类来处理付款。 我有一个接口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);

但它会抛出错误,找不到资源并且未加载pluginpaymentmethod。我该如何修复它,或者你可以建议任何类似实现的教程吗?谢谢。

1 个答案:

答案 0 :(得分:2)

假设你有一个名为MyPlugin的具体类,它有IPaymentMethod接口,那么你的ninject绑定看起来应该有点像:

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

检查NinjectWebCommon.cs文件夹下的App_Start课程中是否存在此问题。更棘手的情况可能是IPaymentMethod必须以与Ninject IKernel绑定相同的方式注册:

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

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