Witam,
Stworzyłem sobie następujący interfejs:
public interface IRepository<TEntity> where TEntity : class, IEntityObject
{
List<TEntity> GetAllUsers();
TEntity GetEntityById(long userId);
void SaveOrUpdate(TEntity obj);
}
oraz jego implementację:
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, IEntityObject
{
private CarPoolingContext _context;
private IDbSet<TEntity> DbSet
{
get
{
return _context.Set<TEntity>();
}
}
public Repository(DbContext context)
{
_context = (CarPoolingContext)context;
}
public List<TEntity> GetAllUsers()
{
return DbSet.ToList();
}
public TEntity GetEntityById(long userId)
{
return DbSet.Single(x => x.Key == userId);
}
public void SaveOrUpdate(TEntity obj)
{
DbSet.Add(obj);
_context.SaveChanges();
}
}
W sieci znalazłem milion poradników, ale wszystkie wymagają użycia IContainer do rozwiązania zależności, a mi się nie podoba to rozwiązanie, bo nie chce używać IContainer w controllerach w mojej aplikacji ASP.NET MVC.
Czy da się tak zarejestrować ten generyczny typ aby go rozwiązywać jako AsImplementedIntefaces()?
Gdy mam taką rejestrację to się wywala program:
private void RegisterRepository(ContainerBuilder container)
{
container.RegisterGeneric(typeof(Repository<>))
.AsImplementedInterfaces();
}
Natomiast gdy zrobę tak:
private void RegisterRepository(ContainerBuilder container)
{
container.RegisterGeneric(typeof(Repository<>))
.As(typeof(IRepository<>));
}
To nie wiem za bardzo jak użyć to repo w moim Controllerze? Bardzo proszę o wytłumaczenie jak użyć to repozytorium w Controllerze, bo na chwilę obecną w konstruktorze wprowadzam interfejs typu i autofac super sobie z tym radzi i zwraca mi implementację.