[英]Asp.net core generic repository pattern with inheritance not working
I have created a generic repository class and interface which will be subclassed for the concrete repositories. 我创建了一个通用的存储库类和接口,它将被子类化为具体的存储库。
Here is some of my code: 这是我的一些代码:
The generic IRepository interface and Repository class: 通用的IRepository接口和Repository类:
public interface IRepository<T> where T : BaseEntity
{
IEnumerable<T> GetAll();
T Get(long id);
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
}
public class Repostitory<T> : IRepository<T> where T : BaseEntity
{
protected readonly ApplicationDbContext _context;
private DbSet<T> _entities;
private string errorMessage = string.Empty;
public Repostitory(ApplicationDbContext context)
{
this._context = context;
_entities = context.Set<T>();
}
public IEnumerable<T> GetAll()
{
return _entities.AsEnumerable();
}
public T Get(long id)
{
return _entities.SingleOrDefault(s => s.Id == id);
}
public void Insert(T entity)
{
CheckEntityNotNull(entity);
_entities.Add(entity);
_context.SaveChanges();
}
public void Update(T entity)
{
CheckEntityNotNull(entity);
_context.SaveChanges();
}
public void Delete(T entity)
{
CheckEntityNotNull(entity);
_entities.Remove(entity);
_context.SaveChanges();
}
private void CheckEntityNotNull(T entity)
{
if(entity == null)
{
throw new ArgumentNullException("Entity");
}
}
}
Inherited Interface and class: 继承的接口和类:
public interface IEventsRepository : IRepository<Event>
{
}
public class EventsRepository : Repostitory<Event>
{
public EventsRepository(ApplicationDbContext context) : base(context)
{
}
}
At this moment there is no implementation yet in IEventsRepository
and EventsRepository
, but there will be once I got this to work. 正在这时,有又是在没有实现
IEventsRepository
和EventsRepository
,但会有一次我得到了这个工作。
In Startup.cs
I have this in the ConfigureServices
method 在
Startup.cs
我在ConfigureServices
方法中有这个
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddScoped(typeof(IRepository<>), typeof(Repostitory<>));
services.AddScoped(typeof(IEventsRepository), typeof(EventsRepository));
}
And in the controller: 在控制器中:
public class AdminController : Controller
{
private readonly IEventsRepository _eventsRepository;
public AdminController(IEventsRepository eventsRepository)
{
_eventsRepository = eventsRepository;
}
[HttpGet]
public IActionResult EventsIndex()
{
var model = _eventsRepository.GetAll();
return View(model);
}
}
When building the application I do not get any errors, or even when I open the homepage. 在构建应用程序时,我没有收到任何错误,甚至在我打开主页时也是如此。
When I open the EventsIndex
page an internal server error is raised: 当我打开
EventsIndex
页面时,会引发内部服务器错误:
InvalidCastException: Unable to cast object of type 'WebsiteProject.Repositories.EventsRepository`1[WebsiteProject.Models.Event]' to type 'WebsiteProject.Repositories.IEventsRepository`1[WebsiteProject.Models.Event]'.
What am I missing here? 我在这里错过了什么?
I was able to fix it. 我能够解决它。 It was quite simple actually.
实际上很简单。
All I had to do is make EventsRepository
also implement IEventsRepository
. 所有我必须做的是使
EventsRepository
也实现了IEventsRepository
。
So instead of 而不是
public class EventsRepository : Repostitory<Event>
{
public EventsRepository(ApplicationDbContext context) : base(context)
{
}
}
I changed it to: 我改成了:
public class EventsRepository : Repostitory<Event>, IEventsRepository
{
public EventsRepository(ApplicationDbContext context) : base(context)
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.