2011-01-30 8 views
3

ObservableCollectionとしてDispatcherNotifiedObservableCollectionを(MyProject.ViewModelsにある)すべてのViewModelに挿入したいと思います。 NinjectでAutofacを使用して特定の名前空間に依存関係を注入する

私はこれを達成することができます:私はニコラスBlumhardtから学んだ

Bind(typeof(ObservableCollection<>)) 
    .To(typeof(DispatcherNotifiedObservableCollection<>)) 
    .When(context => context.ParentContext.Binding 
     .Service.Namespace == "MyProject.ViewModels"); 

Autofac vs Ninject contextual binding?

Autofacは、この機能を提供していませんが、いくつかの回避策が適用され得ること。

ありがとうございます! (私の英語のため申し訳ありません)

編集1:より良い説明のために変更タイトル。

編集2、3:内容とタイトルが変更されました。

答えて

8

ご返信が遅いのは残念です。あなたはIsClosedTypeOf()ためusing Autofac;する必要があります

// Default for other components 
builder.RegisterGeneric(typeof(ObservableCollection<>)); 

// Won't be picked up by default 
builder.RegisterGeneric(typeof(DispatcherNotifiedObservableCollection<>)) 
    .Named("dispatched", typeof(ObservableCollection<>)); 

var viewModelAssembly = typeof(AViewModel).Assembly; 
builder.RegisterAssemblyTypes(viewModelAssembly) 
    .Where(t => t.Name != null && t.Name.EndsWith("ViewModel")) 
    .WithParameter(
     (pi, c) => pi.ParameterType.IsClosedTypeOf(typeof(ObservableCollection<>)), 
     (pi, c) => c.ResolveNamed("dispatched", pi.ParameterType)); 

:Autofacと

あなたの最善の策はViewModel秒を登録するためのルールを使用するとObservableCollection<>の異なる実装を解決するためのパラメータを適用することです。また、使用しているAutofacのバージョンがWithParameter()のこのオーバーロードをサポートしていない場合は、Parameterを受け取り、代わりにResolvedParameterを渡すオーバーロードを使用できます。このことができます

希望、

ニックコードの

+0

ありがとう!私のプロジェクトでうまく動作します。 –

関連する問題