2012-07-26 64 views
2

我使用本教程中我的解决方案来创建插件架构,我也用ninject首次加载类和控制器实例:插件架构与ninject - 从插件组装到主MVC项目

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,版本= 1.0.0.0文化=中性公钥=空”

And want to use pluginpaymentmethod like i do in controller example 

pluginpaymentmethod.PostProcessPayment(postProcessPaymentRequest); 

但它抛出错误资源未发现pluginpaymentmethod不loade d。我该如何解决这个问题,或者你能否推荐任何类似实现的教程?谢谢。

+0

只是一个个人的观点,但我认为你应该简化你的过程,并把所有事情都回滚到你的基本IPaymentMethod并实现它的功能,然后构建它。我看了一下codeproject的文章,看起来有人用ninject有问题,作者成功地获得了团结。所有我说的,当谈到支付网关等时,你需要充分理解发生了什么,并且有一个强大的框架。我不相信这一点。我的意见只。 – 2012-07-26 15:43:20

回答

2

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

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

检查,这是发生在App_Start文件夹下你的NinjectWebCommon.cs类。一个更为复杂的情况可能是IPaymentMethod具有以同样的方式进行注册,该Ninject IKernel势必:

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

这将可能是一个棘手的问题,以锻炼身体。

+0

感谢您的评论,很显然,我对ninject和插件体系结构的流程有一个非常模糊的概念,但认为拥有每种付款方式都是合理的。我会考虑较简单的变体。 – Kariasoft 2012-07-26 20:47:55