2016-06-01 12 views
1

単純なインジェクタを使用してクラスコンストラクタにパラメータを動的に渡す際に問題があります。Simple Injectorを使用してコンストラクタに引数を渡す

私は次のコード構造を持っています。

コントローラの例:

public class HomeController : Controller 
{ 
    private readonly ICheckService _checkService; 

    public HomeController(ICheckService checkService) 
    { 
     _checkService= checkService; 
    } 

    // GET: Home 
    public ActionResult Index() 
    { 
     var list = _checkService.GetAll(); 
     return View(list); 
    } 
} 

サービス層(この層に、私はどのように私は簡単な注射器を使ってこれを実現しますICheckRepository<T>を実施しているCheckRepository<T>のための2つのコンストラクタのパラメータを渡す必要があり、私が試したが、溶液を得ていません。? 。周りの一例としては、達成するためには、本当に感謝される)

public interface ICheckService 
{ 
     List<CheckType> GetAll(); 
} 

public class CheckService : ICheckService 
{ 
    private readonly ICheckRepository<CheckType> _checkRepository; 

    public CheckService(ICheckRepository<CheckType> checkRepository) 
    { 
     _checkRepository= checkRepository; 
    } 

    public List<T> GetAll() 
    { 
     return _checkRepository.GetAll().ToList(); 
    } 
} 

リポジトリ層:

public abstract class RepositoryBase<T> where T : class 
{ 
    public string Types { get; set; } 
    public string Segment { get; set; } 

    public RepositoryBase(string type) 
    { 
     Types = type; 
    } 

    public RepositoryBase(string type, string segment) 
    { 
     Types = type; 
     Segment = segment; 
    } 
} 

public interface ICheckRepository<T> where T : class 
{ 
    IEnumerable<T> GetAll(); 
} 

public class CheckRepository<T> : RepositoryBase<T>, ICheckRepository<T> where T : class 
{ 
    public CheckRepository(string types, string segment) 
     : base(types, segment) 
    { 

    } 

    public IEnumerable<T> GetAll() 
    { 
     var list = new List<T>(); 
     using (DbAccess dbAccess = new DbAccess(ConnectionString, DatabaseType.SqlServer)) 
     { 
      return dbAccess.ExecuteReader<T>(StoredProc, CommandType.StoredProcedure).ToList(); 
     } 
    } 
} 

私の単純なインジェクタ初期化子クラス:

public static void InitializeInjector() 
{ 
    var container = new Container(); 

    InitializeContainer(container); 

    container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); 
    container.RegisterMvcIntegratedFilterProvider(); 
    container.Verify(); 

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container)); 
} 

private static void InitializeContainer(Container container) 
{ 
    container.Register(typeof(IFilterRepository<>), typeof(FilterRepository<>)); 
    //Here is where I am struggling to bind dynamic constructor parameter registering 

} 

誰もが上記のコードのためのすべてのソリューションを持っていますか?

もう一度おねがいします。

+0

この2つのパラメータにはどのような値を入れたいですか?すべてのリポジトリに独自の接続文字列またはストアドプロシージャがあるか、またはこれらの設定定数はすべてのリポジトリと同じですか?コンテナを使用せずにこれらのリポジトリを作成する方法の例を使って質問を更新できますか? – Steven

+0

その文字列はちょうど文字列であり、正確に何を入力するかは関係ありませんが、これらの文字列パラメータはリポジトリごとに異なる場合があります。私はあなたの最後の質問を取得していなかったので、答えは探しています。上のコードは私が立ち往生したところまで説明しました。 –

+0

それはあなたが探している答えではありません。これをSimple Injectorに登録する方法を探しています。しかし、私たちが手助けをするためには、Simple Injectorを持っていなかった場合、それらのリポジトリを手作業で新しくしたことを示す必要があります。 '新しいCheckService(新しいFilterRepository (whatgoedhere?))'のようなものです。作成したい別のリポジトリの例を表示してください。これは、あなたが達成しようとしていることの知識を私たちに与え、あなたの質問に対する正しい答えを定式化することを可能にします。 – Steven

答えて

2

パラメータは、特定の固定されている場合は閉じて、ジェネリック型を、次のように登録を行う必要があります

c.Register<ICheckRepo<Customer>>(() => new CheckRepository<Customer>(constr, "cust_sp")); 
c.Register<ICheckRepo<Order>>(() => new CheckRepository<Order>(constr, "order_sp")); 
c.Register<ICheckRepo<Product>>(() => new CheckRepository<Product>(constr, "prod_sp")); 
// more registrations here 

リポジトリが設定値との依存関係をミックスする場合には、あなたはまた、混合文脈登録を使用することができますオープンジェネリック型の登録に:hereを説明するように

// Registrations 
// One registration for the open generic type 
c.Register(typeof(ICheckRepository<>), typeof(CheckRepository<>)); 

// One registration for the connection string (assuming you only have one) 
container.RegisterConditional(typeof(string), CreateStringConstant(constr), 
    c => c.Consumer.Target.Name == "connectionString"); 

// Conditional registrations for each closed ICheckRepository<T> 
RegisterStoredProcForCheckRepository<Customer>("cuts_sp"); 
RegisterStoredProcForCheckRepository<Order>("order_sp"); 
RegisterStoredProcForCheckRepository<Product>("prod_sp"); 
// more registrations here 

// Helper methods 
Registration CreateStringConstant(string value) => 
    Lifestyle.Singleton.CreateRegistration(typeof(string),() => value, container); 

void RegisterStoredProcForCheckRepository<TEntity>(string spName) { 
    container.RegisterConditional(typeof(string), CreateStringConstant(container, spName), 
     c => c.Consumer.Target.Name == "segment" 
      && c.Contumer.ImplementationType == typeof(CheckRepository<TEntity>)); 
} 

接続文字列またはストアドプロシージャは、リクエストごとに変化する場合には、あなたは、デザインを変更する必要があります。

関連する問題