[英]IRepository with Inherited Classes
In keeping with the Repository pattern of data input, I've a question in regards to using inherited classes. 与数据输入的存储库模式保持一致,关于使用继承的类,我有一个问题。 For instance, suppose I would have the class...
例如,假设我要上课...
class Employee
IEmployeeRepository
{
Add(Employee employee);
}
This works fine, nothing wrong with it so far... but now let's say I continue on.. 效果很好,到目前为止没有任何问题...但是现在让我继续。
class Manager : Employee
Okay, now let's assume that I never need to enter a manager different than an Employee? 好吧,现在让我们假设我不需要再输入不同于雇员的经理了吗? What's the best approach here?
最好的方法是什么? Would a scenario such as ..
是否会出现诸如..
IEmployeeRepository
{
Add<T>(T employee) where T : Employee
}
Be the best approach, or do I need to abstract a different repository for each type? 是最好的方法,还是我需要为每种类型抽象一个不同的存储库?
If they are all employees, I would probably create an interface and constrain it to that. 如果他们都是雇员,我可能会创建一个界面并将其限制为该界面。 For example:
例如:
IEmployeeRepository
{
Add<T>(T employee) where T : IEmployee
}
If things are being handled differently, then you should create another Repository for the new type. 如果处理方式不同,则应为新类型创建另一个存储库。
If the two never have different logic (and they really shouldn't considering a Manager is still an Employee in your case), then you can re-use what you already have. 如果两者从未有过不同的逻辑(他们真的不应该考虑经理在您的情况下仍然是雇员),那么您可以重用已经拥有的东西。 You shouldn't even need to change the code (as long as Manager inherits from the Employee class):
您甚至不需要更改代码(只要Manager从Employee类继承):
public interface IEmployeeRepository
{
Add(Employee employee);
}
Should work just fine. 应该工作正常。
let's assume that I never need to enter a manager different than an Employee
假设我再也不需要输入与员工不同的经理了
So you do not have an abstraction. 因此,您没有抽象。 You only have an
Employee
and you need a repository for that. 您只有一名
Employee
并且需要一个存储库。
do I need to abstract a different repository for each type?
我需要为每种类型抽象一个不同的存储库吗?
Yes if you have an abstraction of a few subclasses. 是的,如果您有一些子类的抽象。 The point is you normally do not need a repository for abstract classes only for subclasses.
关键是您通常不需要仅用于子类的抽象类的存储库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.