How can I register generic Repository with 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
I Also tried:
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
I don't believe that GalaSoft.MvvmLight.Ioc.SimpleIoc
(source code) supports open generic implementations. You would need to create closed implementations and register each of them separately:
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>();
I suggest you consider moving to a more mature library such as SimpleInjector which has extensive support for generics.
The code for SimpleInjector would be as simple as:
container.RegisterOpenGeneric(typeof(IRepository<>), typeof(Repository<>));