2011-12-24 38 views
0

我正在使用ASP.NET MVC 3並擁有自定義模型活頁夾。與溫莎城堡相關的自定義模型活頁夾

public class NewsModelBinder : DefaultModelBinder 
{ 
    private readonly INewsRepository newsRepository; 
    private readonly ICategoryRepository categoryRepository; 

    public NewsModelBinder() 
    { 

    } 

    public NewsModelBinder(INewsRepository newsRepository, ICategoryRepository categoryRepository) 
    { 
     this.newsRepository = newsRepository; 
     this.categoryRepository = categoryRepository; 
    } 

    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) 
    { 
     //Some code here 
    } 
} 

我想解決與溫莎城堡的依賴關係。但是,如果我想同時註冊我的活頁夾並且我想讓Windsor給它所需的參數,我可以通過哪種方式來做到這一點?因爲當我註冊粘合劑時,我不能只給它需要的參數。

UPD 0 或者,也許我可以從代碼中的容器中獲取合適的對象?因爲我將這些存儲庫註冊爲單例。

回答

0

我以這種方式解決問題:把我的依賴關係性質和使用性能噴射

public class NewsModelBinder : DefaultModelBinder 
{ 
    public INewsRepository NewsRepository { get; set; } 
    public ICategoryRepository CategoryRepository { get; set; } 

    public NewsModelBinder() 
    {} 

    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) 
    { 
     //Some code here 
    } 
} 

然後在Global.asax中我將自己粘結劑這樣

ModelBinders.Binders.Add(typeof(News), Container.Resolve<NewsModelBinder>()); 

溫莎以這種方式自己解決依賴關係