CRUD有哪些好的设计模式? [英] What are some good design patterns for CRUD?

查看:193
本文介绍了CRUD有哪些好的设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用许多可以创建,读取,更新和删除的数据实体,我发现自己为它们编写或多或少的相同代码。例如,我有时需要将数据输出为JSON,有时需要以表格格式输出。我发现自己正在编写2种不同类型的视图以将数据导出到其中。而且,在DB中创建这些实体通常只是在SQL语句和输入参数上有所不同。

I am working with a number of data entities which can be created, read, updated and deleted, and I find myself writing more or less the same code for them. For example, I need to sometimes output data as JSON, and sometimes in a table format. I am finding myself writing 2 different types of view to export the data to. Also, the creation of those entities within DB usually differs just by the SQL statements and the input parameters.

我正在考虑创建一种表示不同上下文的策略模式。 。例如,AJAX上下文的read()方法将以JSON形式返回数据。但是,我想知道其他人是否已经事先解决了这个问题,并想知道CRUD操作通常使用哪种设计模式。

I am thinking of creating a strategy pattern to represent different 'contexts'. For example, the read() method of an AJAX context will be to return the data as JSON. However, I wonder if others have deal with this problem beforehand and will like to know what design patterns are usually use for CRUD operations.

编辑:一个注释是,有时复合实体被用来组成一个整体;例如,一个位置可以有很多描述,每种支持的语言都有一个描述。

One note is that sometimes compound entities are used to make up one whole one; for example, a location can have many descriptions, one for each supported language.

推荐答案

CRUD没有单一的模式,有许多重叠/竞争模式。这些CRUD模式的最佳来源之一是:

There isn't one single pattern for CRUD, there are many overlapping/competing patterns. One of the best sources for these CRUD patterns is:

Martin Fowler的企业应用程序体系结构模式

通常,解决此特定问题的最佳方法是使用ORM(假设使用关系数据存储,一个安全的假设):

In general, the best way to solve this particular problem is with an ORM (assuming a relational data store, which seems like a safe assumption in this case):

http://en.wikipedia.org/wiki/Object-relational_mapping

就通过视图公开对象而言,有一些框架可以也可以提供帮助,但是这些框架是特定于您的编码环境的,您未指定。

As far as exposing your objects through views, there are frameworks out there to help with that as well, but those frameworks are specific to your coding environment, which you did not specify.

这篇关于CRUD有哪些好的设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆