2012-07-26 28 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