谈论接口(interface)隔离原则 我正在评估我经常在服务层中使用的接口(interface)的设计:
public interface ICrudService<D>
{
IList<D> GetAll();
bool Exists(int id);
D GetById(int id);
D NewInstance();
D Create(D dto);
D Update(D dto);
void Delete(int id);
}
我通常使用这个接口(interface)的抽象实现,并从抽象类继承我的具体服务。
但显然我的服务类中并不总是需要所有这些方法,所以我想让这个结构更加灵活。
一个选项可能是以这种方式拆分我的接口(interface)(和我的抽象类):
public interface IGetAllService<D>
{
IList<D> GetAll();
}
public interface IExistsService<D>
{
bool Exists(int id);
}
// etc.
然后在我的具体类中实现所需的方法:
public class ConcreteService : IGetAllService<ConcreteEntity>, IExistsService<ConcreteEntity>
{
// implemented methods
}
但这是好的设计吗?
是否有更好的方法使我的应用程序结构更加灵活和可重用?
最佳答案
看来您是从抽象类派生来继承默认实现的。不要滥用继承来重用代码。继承是为了创造可替代性。为自己创建一些辅助方法。这样您就可以只创建您想要公开的方法。
关于c# - 如何为我的服务层设计更灵活的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31742356/