CRUD操作模板在java中 [英] CRUD operations template in java
问题描述
我发现4个模式。所有这些人都同意READ操作,所以我跳过它们。我将定义模式:
模式1
void add (E实体);
void update(E实体);
//读操作...
void delete(E entity);
模式2
code> E create(< ListOfEntityProperties>);
void update(E实体);
//读操作...
void delete(E entity);
模式3
code> boolean add(E entity);
布尔更新(E实体);
//读操作...
boolean delete(E entity);
模式4
code> E add(E entity);
void update(E实体);
//读操作...
void delete(E entity);
我不明白每个人的差异和优势/缺点(没有人解释) 。每个模式之间有什么区别?使用什么标准?有人可以证明什么时候使用某种方式(或给我另一个想法)?
提前感谢您的答案
以模式3为例,您可以使用布尔值来知道成功删除或添加的内容。删除的情况是可以的,但对于添加或更新的情况,您可能希望该对象返回一个数据库分配的ID,如图4所示。
如果你想批量插入对象,然后是模式2.
如果您不期望从CRUD操作返回任何内容,则使方法空隙
。
您希望返回的值是重要的部分,模式并不重要,它们完全依赖于底层的API使用。例如,SQL Server插入操作可能会返回插入对象的行ID。在这种情况下,您可以只返回ID,或者作为参数传递的相同对象,但是其ID字段设置。
I'm designing a system where I must perform several CRUD operations among the System entities. Just for make sure I searched in forums to see how it's resolved by someone else.
I found 4 "patterns". All of them agree in the READ operations so I skip them. I'll define the patterns:
Pattern 1
void add(E entity);
void update(E entity);
// Read operations...
void delete(E entity);
Pattern 2
E create(<ListOfEntityProperties>);
void update(E entity);
// Read operations...
void delete(E entity);
Pattern 3
boolean add(E entity);
boolean update(E entity);
// Read operations...
boolean delete(E entity);
Pattern 4
E add(E entity);
void update(E entity);
// Read operations...
void delete(E entity);
I don't understand the difference and advantages/disadvantages of each one (and no one explains it). What are the differences between each "pattern"? What criteria to use? Can someone justify when to use one way or another (or give me another idea)?
Thanks in advance for your answers
Take Pattern 3, for example, you'd use a boolean to know if something successfully was deleted or added. The deleted case is okay, but for the added or updated case, you might want the object back with a database assigned id, which looks like pattern 4.
If you want to batch insert objects, then that's pattern 2.
And if you don't expect to return anything from a CRUD operation, then make the methods void
.
The values that you want returned are the important part, the "patterns" don't matter so much, and they completely depend on the underlying API usage. For example, a SQL Server insert operation may return the row ID of the inserted object. In that case, you could return just the ID, or the same object you passed as a parameter, but with its ID field set.
这篇关于CRUD操作模板在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!