2016-06-20 10 views
0

WCFオートファクトコンテナ(IContainer)は、WCF以外のプロジェクトクラスでどのように使用できますか?WCF Autofac Containerをn-tierアーキテクチャの別のプロジェクトレベルで使用する

Autofac to Serviceプロジェクト(下記のサンプル)を追加することは唯一の解決方法であり、これを実現する他の優雅な方法です。

サービスをパラメータとして渡すことができない

助けてください。

WCFサービスプロジェクト(Autofac WCFを使用します)

public class DiscountWCFService 
{ 
    public IDiscountService service {get; set;} 
} 

サービスプロジェクト

public class DiscountService : IDiscountService 
{ 
    public IDiscountRepository repository {get; set;} 
} 


public static class DiscountExtension 
{ 
    public static IDiscountProcessor GetProcessor(this Discount discount) 
    { 
     if(discount.Type=1) return TypeAProcessor(); 
     if(discount.Type=2) return TypeBProcessor(); 

     return null; 
    } 
} 

public class TypeAProcessor : IDiscountProcessor 
{ 
    // Is it possible to use Autofac? 
    // E.G. IService fooService = AutofacContainer.Resolve<IFooService> 
} 

public class TypeBProcessor : IDiscountProcessor 
{ 
} 

エンティティプロジェクト

public class Discount 
{ 
} 

答えて

0

Autofacでkeyed registrationを使用すると、私の日が節約されました。私の場合のための

試料溶液が

builder.RegisterType<TypeAProcessor>().As<IDiscountProcessor>().Keyed<IDataService>(Type.One); 

builder.RegisterType<TypeBProcessor>().As<IDiscountProcessor>().Keyed<IDataService>(Type.Two);  

builder.Register<Func<Discount, IDiscountProcessor>>(c => 
{ 
    var componentContext = c.Resolve<IComponentContext>(); 
    return (discount) => 
    { 
     var processor = componentContext.ResolveKeyed<IDiscountProcessor>(discount.Type); 
     return dataService; 
    }; 
}); 
ある
関連する問題