数据库和UI之间的类
我有一个类处理从数据库写入和读取数据。这个类的正确名称是什么?
I have a class that handles writing and reading data from my database. What is a proper name to call this class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几个约定。假设一个 Person 模型,您可以使用:
...
它也取决于您正在使用的技术。我的意思是,谁知道您所使用的语言存在哪些约定。让我们知道什么语言和什么数据访问框架,答案可能会有所不同。
我过去常在后面加上“道”,因为它简短而清晰。但后来我更多地转向了 Martin Fowler 的词汇和模式,所以现在我使用存储库。啰嗦了一点,但我生性啰嗦,所以很适合我的风格。最后,这就是关键。这是风格上的,据我所知,没有全面的标准。最重要的是,你要选择明确的东西并持续使用它。如果您稍后决定切换到其他内容,请怜悯任何可能跟随您并重命名所有内容的程序员,以便所有数据访问组件的命名保持一致。
编辑:在重读本文时,我意识到我假设您将有多个这样的类,每个模型实体都有一个。谁知道你的设置是什么。如果您不打算这样做,而只是为所有数据访问的单个入口点寻找一个标准名称,则可以使用:
通常,假设您将拥有多个这些围绕着您的每个“表”/模型实体。这不仅仅是命名约定,它可能是标准编码约定。这样,当您更改或添加与“persons”表交互方式的某些方面时,您不必修改包含访问“addresses”表的代码的类。查看 Martin Fowler 的企业应用程序架构模式 (PofEAA),了解更多
PofEAA 模式目录(查看数据源架构模式
和
<一个href="http://www.evansvillednug.com/LinkClick.aspx?fileticket=4LwCx2tkyBA=&tabid=76&mid=399" rel="nofollow">快速领域驱动设计(免费 pdf)第 3 章
There are a couple of conventions. Assuming a Person model, you could use:
...
It is also dependent on the technology you are using. I mean, who knows what conventions exist for the language you are using. Let us know what language and what data access framework and the answer may vary.
I used to append "Dao" because it's short and clear. But then I moved over more to Martin Fowler's vocabulary and patterns, so now I use Repository. A little more long winded, but I'm long winded by nature, so it fits my style. In the end, that's the key. It's stylistic and there is no across the board standard that I'm aware of. What's most important is that you pick something that is clear and you use it consistently. If you decide, later on, to switch to something else, have mercy on any programmers that may follow you and rename everything so that all your data access components are consistently named.
Edit: in rereading this, I realized I am assuming you are going to have multiple such classes, one for each of your model entities. Who knows what your setup is. If you aren't going to do it like that, and you're just looking for a standard name for a single point of entry to all data access, you could use:
Typically, the assumption is that you are going to have several of these around, one for each of your "tables"/model entities. More than a naming convention, that is probably a standard coding convention. This way, when you change or add some aspect of how you interact with your "persons" table, you don't have to modify a class in which you have code to access the "addresses" table. Check out Martin Fowler's Patterns of Enterprise Application Architecture (PofEAA), for more
PofEAA catalog of patterns (check out Data Source Architectural Patterns
and
Domain Driven Design Quickly (free pdf) esp. Ch. 3
根据该类表示的实体,它可能是例如
Person
。然后,您设计一个传递到 GUI 的PersonViewModel
。因此,您从数据库获取的Person
被映射到PersonViewModel
,该模型被传递到 UI 层以某种形式显示。视图模型只是从数据库获取的域模型的表示,并且仅包含需要在给定 UI 上显示的必要信息。Depending on the entity this class represents it could be for example
Person
. Then you design aPersonViewModel
which is passed to the GUI. So thePerson
you got from the database is mapped to aPersonViewModel
which is passed to the UI layer for being shown under some form. The view model is just a representation of the domain model you fetched from the database and containing only the necessary information that you need to display on the given UI.