[英]Relational databases and object oriented environment
As every body knows that Object oriented languages provide reusability features. 每个人都知道面向对象语言提供了可重用性功能。
I have a simple three tier application: 我有一个简单的三层应用程序:
I have been struggling to enforce code reusability in this datalayer. 我一直在努力在这个数据层中强制执行代码可重用性。 I am pasting a pseudo pattern in one of my methods in datalayer.
我在datalayer中的一个方法中粘贴伪模式。
create connection object
open connection to database
create a transaction object
begin transaction
create command object
execute it
.
.
.
.
create nth command object
execute it
commit transaction
close connection
In reality this code swells to around 300 to 400 lines of code and it becomes impossible to read this code. 实际上,此代码会膨胀到大约300到400行代码,因此无法读取此代码。
In this series of command executions we are selecting / inserting / updating queries on different tables. 在这一系列的命令执行中,我们在不同的表上选择/插入/更新查询。 If it were not to be in transaction, I would have separated this code to their respective classes.
如果它不是在交易中,我会将此代码分离到各自的类。
There is again a spaghetti pattern which I recently encountered: 我最近遇到了一个意大利面图案:
business layer method1 calls datalayer method to update column1
business layer method2 calls datalayer method to update column2
businees layer method3 calls datalayer method to save the entire result in table by updating it.
This pattern emerged when I was trying to reap the benefits of reusability, these methods are called from different locations so they were reused. 当我试图获得可重用性的好处时出现了这种模式,这些方法从不同的位置调用,因此它们被重用。 However, if were to write simple sql query not keeping in mind of reusability there would have been a single call to database.
但是,如果要编写简单的SQL查询而不记住可重用性,那么就会对数据库进行一次调用。
So, is there any pattern or technique by which reusability can be achieved in data layer? 那么,是否有任何模式或技术可以在数据层中实现可重用性?
Note: 注意:
I agree with the comments to your question; 我同意你对你的问题的评论; you should really avoid re-inventing the wheel here and go with an ORM, if at all possible.
如果可能的话,你应该真的避免在这里重新发明轮子并使用ORM。 Speaking from experience, you're going to end up writing code and solving problems that have long ago been solved and it will probably take you more time in the long run.
从经验来看,你最终会编写代码并解决早已解决的问题,从长远来看,这可能会花费你更多的时间。 However, I understand that sometimes there are constraints that don't permit the use of an ORM.
但是,据我所知,有时存在不允许使用ORM的约束。
Here are some articles that I have found helpful: 以下是一些我发现有用的文章:
This first article is an old one but it explains the different options that you have for data access design patterns. 第一篇文章是旧文章,但它解释了您对数据访问设计模式的不同选择。 It has a few different patterns and only you can really decide which one will be best for you but it sounds like you might want to look at the Repository Pattern:
它有一些不同的模式,只有你可以真正决定哪一个最适合你,但听起来你可能想要查看存储库模式:
http://msdn.microsoft.com/en-us/magazine/dd569757.aspx http://msdn.microsoft.com/en-us/magazine/dd569757.aspx
This next article is the first in a series that talks about how to implement a repository pattern with a data mapper which, based on your example above, will probably help to reduce some of your redundant code. 下一篇文章是系列文章中的第一篇,讨论如何使用数据映射器实现存储库模式,基于上面的示例,这可能有助于减少一些冗余代码。
http://blogsprajeesh.blogspot.com/2010/02/data-access-layer-in-c-using-repository.html http://blogsprajeesh.blogspot.com/2010/02/data-access-layer-in-c-using-repository.html
Finally, depending on how you implement your data access pattern, you may find the template pattern and generics helpful. 最后,根据您实现数据访问模式的方式,您可能会发现模板模式和泛型很有用。 The following article talks about that a little bit and you can glean some helpful information from it:
下面的文章谈到了一点,你可以从中收集一些有用的信息:
http://www.c-sharpcorner.com/UploadFile/rmcochran/elegant_dal05212006130957PM/elegant_dal.aspx http://www.c-sharpcorner.com/UploadFile/rmcochran/elegant_dal05212006130957PM/elegant_dal.aspx
Without knowing more about your project, it's hard to say, exactly, which pattern will best suit your needs. 在不了解您的项目的情况下,很难确切地说,哪种模式最适合您的需求。 However, using a combination of the Unit of Work pattern with repositories and data mappers will probably help you to reuse some code and manage your data access.
但是,将工作单元模式与存储库和数据映射器结合使用可能会帮助您重用某些代码并管理数据访问。
What I'm not seeing is your model layer. 我没有看到的是你的模型层。
You have a business layer, and a DAO layer, but no model. 您有一个业务层和一个DAO层,但没有模型。
business layer method1 calls datalayer method to update column1
business layer method2 calls datalayer method to update column2
businees layer method3 calls datalayer method to save the entire result in table by updating it.
Why isn't this: 为什么不是这个:
business layer updates model/domain object A
business layer updates model/domain object A in a different way
business layer persists model/domain to database through data layer.
This way you get re-use, and avoid repeated loops back and forth to database. 这样您就可以重复使用,并避免重复循环到数据库。
Ultimately it sounds like your business layer knows FAR too much of the database data model. 最终,听起来您的业务层知道FAR过多的数据库数据模型。 You need business objects , not just business methods.
您需要业务对象 ,而不仅仅是业务方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.