如何设计一个具有类似操作的解决方案,例如CRUD,但并不适用于其所有子类



我试图设计一个提供CRUD操作的解决方案。我应该为几个实体提供接口。其中每个类都映射到单个实体。每个类为每个实体提供CRUD操作。然而,有一些实体只提供某些操作。例如只读和更新,创建和只读等。我应该如何设计,使我能够更好地扩展,并为其他开发人员创建一个良好的界面?

my current design:

5 Interface
IEntity, IReadItem, IDelete, ICreate, IUpdate.
Example IEntity -> Customer class
IReadList -> Read List Operations on Customer
IDelete-> Delete operation on Customer
ICreate -> Create Item operation on Customer
IUpdate -> Update Operation on customer
EntityFactory -> Instantiate the IEntity, and 4 operation concrete class.
 -> getEntity(EntityType(enum)) : return Concrete IEntity Class
 -> getReadList(ReadListType(enum),entity) : return concrete GetItemOperation Class
 -> getDelete(DeleteType(enum),entity) : return concrete DeleteItemOperation Class
 -> getCreate(CreateType(enum),entity) : return concrete CreateItemOperation Class
Entity -> Responislbe of caching own information and connection
Read,Create,Delete,Update -> Responsible of it own details and operation
Each entity have all or subset of the type 4 operation, CRUD.

有没有更好的设计方法?或者对上面的布局有什么评论吗?

我的建议是选择一个众所周知的现有设计模式来处理关系数据库。查看您的代码很难确定您要使用哪一种。一旦你确定了合适的模式,你可能会发现找到解决方案更容易。

您可以在这里阅读一些持久性选项。Martin Fowler还在数据源架构模式下列出了该页面的几个。

当然,你也可以选择使用现有的ORM,如实体框架或nHibernate。

问了上面的实际设计目标…如果没有更具体的目标,我会说保持简单:

IEntity应该有Update和Delete方法,工厂应该有Read/List和Create方法。不支持该操作的实体/工厂应该抛出异常或返回一个有意义的值。就是这样。

相关内容

最新更新