1

汎用リポジトリを SimpleIOC に登録するにはどうすればよいですか?

  public interface IRepository<T>
  {

  }

  public class Repository<T> : IRepository<T>
  {

  }

  SimpleIoc.Default.Register<IRepository, Repository>(); //Doesn't work, throws error


 Error  1   Using the generic type 'AdminApp.Repository.IRepository<TModel>' requires 1 type arguments  C:\Application Development\AdminApp\AdminApp.Desktop\ViewModel\ViewModelLocator.cs  55  44  AdminApp.Desktop

私も試しました:

    SimpleIoc.Default.Register<IRepository<>, Repository<>>(); //Doesn't work either
     Error  1   Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement   C:\Application Development\AdminApp\AdminApp.Desktop\ViewModel\ViewModelLocator.cs  55  17  AdminApp.Desktop
4

1 に答える 1

4

GalaSoft.MvvmLight.Ioc.SimpleIoc(ソースコード) がオープン ジェネリック実装をサポートしているとは思えません。閉じた実装を作成し、それぞれを個別に登録する必要があります。

public interface IRepository<T> where T : class { }

public class A { }
public class B { }

public class RepositoryA : IRepository<A> { }
public class RepositoryB : IRepository<B> { }

SimpleIoc.Default.Register<IRepository<A>, RepositoryA>();
SimpleIoc.Default.Register<IRepository<B>, RepositoryB>();

ジェネリックを幅広くサポートするSimpleInjectorなどのより成熟したライブラリへの移行を検討することをお勧めします。

SimpleInjector のコードは次のように単純です。

container.RegisterOpenGeneric(typeof(IRepository<>), typeof(Repository<>));
于 2013-10-10T09:56:03.947 に答える