简体   繁体   English

关系数据库和面向对象的环境

[英]Relational databases and object oriented environment

As every body knows that Object oriented languages provide reusability features. 每个人都知道面向对象语言提供了可重用性功能。

I have a simple three tier application: 我有一个简单的三层应用程序:

  1. presentation layer 表示层
  2. business layer is designed to reap the benefits of reusability 业务层旨在获得可重用性的好处
  3. datalayer is a dumb ado.net library(by dumb I meant that it has no business logic in place.) datalayer是一个愚蠢的ado.net库(通过dumb我的意思是它没有业务逻辑。)

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: 注意:

  1. I don't want to use any stored procedures despite the fact that they offer precompilation benefits and etc. , as they tend to tie datalayer more specific to a particular database. 我不想使用任何存储过程,尽管它们提供预编译的好处等,因为它们倾向于将数据层绑定到特定数据库。
  2. I am also currently not considering any ORM solutions here only plain ADO.net. 我目前也没有考虑任何ORM解决方案,只有普通的ADO.net。


Excuses for not considering any ORMs. 不考虑任何ORM的借口。

  1. Learning curve 学习曲线
  2. Avoiding tight coupling to a specific ORM which I think can be removed by restricting the ORM code in datalayer itself. 避免与特定ORM紧密耦合,我认为可以通过限制数据层本身的ORM代码来消除。
  3. I checked the internet some time 6 months ago and there were only two popular or widely used ORM solutions available then. 我在6个月前的某个时间检查了互联网,然后只有两种流行或广泛使用的ORM解决方案。 Entity Framework and NHibernate. 实体框架和NHibernate。 I choose Entity Framework ( for some reasons I will link later link1 , link2 besides that I had a feeling that working with EF would be easy as it is provided by Microsoft) to start learning. 我选择实体框架( 由于某些原因,我将链接以后的 link1link2,除了我有一种感觉,使用EF很容易,因为它由Microsoft提供)开始学习。
  4. I used this Microsoft recommended book in this book there were three techniques as I understood TPT , TPH and TPC ; 我在本书中使用了微软推荐的书 ,有三种技术,我理解TPTTPHTPC ; TPC I never tried. TPC我从未尝试过。
  5. When I checked the SQL generated from the Entity Framework, it was very ugly and was creating some extra columns: Ids, some ugly Case statements etc., it seemed that for a highly transactional system the ORM solution cannot be applied.By highly transactional system I mean 1000 of insertions happening every single minute. 当我检查从实体框架生成的SQL时,它非常难看并且正在创建一些额外的列:Ids,一些丑陋的Case语句等等,似乎对于高度事务性系统,ORM解决方案无法应用。通过高度事务性系统我的意思是每分钟发生1000次插入。 The database continues to swell in size and reaches somewhere near 500 to 600 GBs in some distant future. 在不久的将来,数据库的规模继续扩大,达到500到600 GB左右。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM