Does anyone know of any design patterns for interfacing with relational databases? For instance, is it better to have SQL inline in your methods, or instantiate a SQL object where you pass data in and it builds the SQL statements? Do you have a static method to return the connection string and each method just gets that string and connects to the DB, performs its action, then disconnects as needed or do you have other structures that are in charge of connecting, executing, disconnecting, etc?
有没有人知道任何与关系数据库接口的设计模式?例如,最好在方法中使用SQL内联,还是实例化传入数据的SQL对象并构建SQL语句?您是否有一个静态方法来返回连接字符串,每个方法只获取该字符串并连接到数据库,执行其操作,然后根据需要断开连接,或者您是否有其他负责连接,执行,断开连接等的结构?
In otherwords, assuming the database already exists, what is the best way for OO applications to interact with it?
换句话说,假设数据库已经存在,OO应用程序与之交互的最佳方式是什么?
Thanks for any help.
谢谢你的帮助。
7 个解决方案
#1
I recommend the book Patterns of Enterprise Application Architecture by Martin Fowler for a thorough review of the most common answers to these questions.
我推荐Martin Fowler撰写的“企业应用程序架构模式”一书,以全面回顾这些问题的最常见答案。
#3
I personally would hate to work with a database without an ORM. NHibernate is preferable but iBatis is also an option for existing databases (not to say that NH can't handle existing databases).
我个人不愿意在没有ORM的情况下使用数据库。 NHibernate是首选,但iBatis也是现有数据库的选项(并不是说NH无法处理现有数据库)。
#4
In general, the best way for OO applications to interface with a relational database is through an ORM; while this isn't a design pattern per se, it's a type of tool that has a specific usage pattern, so it's similar enough. Object Relational Mapping (ORM) tools provide a mapping between a database and a set of objects in memory; usually, these tools provide means for managing things such as sessions, connections, and transactions. A good example of an ORM that works fantastically well would be Hibernate (NHibernate on .NET).
通常,OO应用程序与关系数据库交互的最佳方式是通过ORM;虽然这本身不是设计模式,但它是一种具有特定使用模式的工具,因此它足够相似。对象关系映射(ORM)工具提供数据库与内存中一组对象之间的映射;通常,这些工具提供了管理会话,连接和事务等方法的方法。一个非常好用的ORM的好例子是Hibernate(.NET上的NHibernate)。
#5
In my experience it is best to have no SQL statements at all (most ORMs will allow that), and it is best not to have any knowledge of connection details (connection string, etc). Even better if you can have the exact same piece of code working with any major db vendor.
根据我的经验,最好不要有任何SQL语句(大多数ORM都允许),最好不要了解连接细节(连接字符串等)。如果您可以使用完全相同的代码与任何主要数据库供应商合作,那就更好了。
#6
POEAA has a wealth of knowledge on the issue if you intend to roll your own.
如果您打算自己动手,POEAA对此问题有丰富的知识。
#7
Everything you will identify by googling for DAL describes a design pattern. Seems like standing in the middle of the forest and asking to see a tree. There are dozens if not thousands.
通过谷歌搜索DAL识别的所有内容都描述了一种设计模式。好像站在森林中间,想要看到一棵树。有几十个,如果不是几千个。
Here's a quote from a book I'm reading to start with for looking for resources.
这是我正在阅读的一本书的引用,用于寻找资源。
... it is impossible to discuss ORM without talking about patterns and best practices for building persistence layers. Then again, it is also impossible to discuss ORM patterns without calling out the gurus in the industry, namely Martin Fowler, Eric Evans, Jimmy Nilsson, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, the last four of whom are known in the industry as the Gang of Four (GoF). The purposes of this chapter are to explain and expand some of the patterns created by these gurus and to provide concrete examples using language that every developer can understand.
......如果不讨论构建持久层的模式和最佳实践,就不可能讨论ORM。再说一次,如果不召集业内专家,即Martin Fowler,Eric Evans,Jimmy Nilsson,Erich Gamma,Richard Helm,Ralph Johnson和John Vlissides,他们也不可能讨论ORM模式,其中最后四位是众所周知的在行业中作为*(GoF)。本章的目的是解释和扩展这些大师创建的一些模式,并使用每个开发人员都能理解的语言提供具体的例子。
#1
I recommend the book Patterns of Enterprise Application Architecture by Martin Fowler for a thorough review of the most common answers to these questions.
我推荐Martin Fowler撰写的“企业应用程序架构模式”一书,以全面回顾这些问题的最常见答案。
#2
#3
I personally would hate to work with a database without an ORM. NHibernate is preferable but iBatis is also an option for existing databases (not to say that NH can't handle existing databases).
我个人不愿意在没有ORM的情况下使用数据库。 NHibernate是首选,但iBatis也是现有数据库的选项(并不是说NH无法处理现有数据库)。
#4
In general, the best way for OO applications to interface with a relational database is through an ORM; while this isn't a design pattern per se, it's a type of tool that has a specific usage pattern, so it's similar enough. Object Relational Mapping (ORM) tools provide a mapping between a database and a set of objects in memory; usually, these tools provide means for managing things such as sessions, connections, and transactions. A good example of an ORM that works fantastically well would be Hibernate (NHibernate on .NET).
通常,OO应用程序与关系数据库交互的最佳方式是通过ORM;虽然这本身不是设计模式,但它是一种具有特定使用模式的工具,因此它足够相似。对象关系映射(ORM)工具提供数据库与内存中一组对象之间的映射;通常,这些工具提供了管理会话,连接和事务等方法的方法。一个非常好用的ORM的好例子是Hibernate(.NET上的NHibernate)。
#5
In my experience it is best to have no SQL statements at all (most ORMs will allow that), and it is best not to have any knowledge of connection details (connection string, etc). Even better if you can have the exact same piece of code working with any major db vendor.
根据我的经验,最好不要有任何SQL语句(大多数ORM都允许),最好不要了解连接细节(连接字符串等)。如果您可以使用完全相同的代码与任何主要数据库供应商合作,那就更好了。
#6
POEAA has a wealth of knowledge on the issue if you intend to roll your own.
如果您打算自己动手,POEAA对此问题有丰富的知识。
#7
Everything you will identify by googling for DAL describes a design pattern. Seems like standing in the middle of the forest and asking to see a tree. There are dozens if not thousands.
通过谷歌搜索DAL识别的所有内容都描述了一种设计模式。好像站在森林中间,想要看到一棵树。有几十个,如果不是几千个。
Here's a quote from a book I'm reading to start with for looking for resources.
这是我正在阅读的一本书的引用,用于寻找资源。
... it is impossible to discuss ORM without talking about patterns and best practices for building persistence layers. Then again, it is also impossible to discuss ORM patterns without calling out the gurus in the industry, namely Martin Fowler, Eric Evans, Jimmy Nilsson, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, the last four of whom are known in the industry as the Gang of Four (GoF). The purposes of this chapter are to explain and expand some of the patterns created by these gurus and to provide concrete examples using language that every developer can understand.
......如果不讨论构建持久层的模式和最佳实践,就不可能讨论ORM。再说一次,如果不召集业内专家,即Martin Fowler,Eric Evans,Jimmy Nilsson,Erich Gamma,Richard Helm,Ralph Johnson和John Vlissides,他们也不可能讨论ORM模式,其中最后四位是众所周知的在行业中作为*(GoF)。本章的目的是解释和扩展这些大师创建的一些模式,并使用每个开发人员都能理解的语言提供具体的例子。