此时,我的“employee”类有这段代码。但我对“客户”和所有其他人的看法几乎相同。
有没有办法创建与我的类“EmployeeRepository”等效的类,但更像是 MyRepo
public class EmployeeRepository : NHRepository<Employee>, IEmployeeRepository
{
public Employee Get(Guid EmployeeId)
{
return base.Get(EmployeeId);
}
}
public interface IEmployeeRepository : IRepositoryActionActor<Employee>
{
}
public interface IRepositoryActionActor<T>
{
T Get(Guid objId);
}
最佳答案
是的,就像 Spencer Ruport 已经提到的那样,像我一样将代码收集在接口(interface)或抽象基类中:
public interface IPerson
{
void DoSomething( );
}
public abstract class Person : IPerson
{
public virtual void DoSomething( )
{
throw new NotImplementedException( );
}
}
public class Employee : Person
{
public override void DoSomething( )
{
base.DoSomething( );
/* Put additional code here */
}
}
public class Customer : Person { }
public class PersonRepository<T> : System.Collections.Generic.List<T> where T : IPerson, new( )
{
public T Get( Guid id )
{
IPerson person = new T( );
return (T)person;
}
}
关于c# - 类、接口(interface)、泛型......需要简化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1320715/