C# - 在业务层交易(SQLServer的2005+,甲骨文)的用法 - 很好的例子 [英] C# - Usage of transactions in business layer (SQLServer 2005+ , Oracle) - good examples

查看:126
本文介绍了C# - 在业务层交易(SQLServer的2005+,甲骨文)的用法 - 很好的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立使用3层架构的服务,我真的很担心如何在交易的方式处理业务。

I am gonna build a service using 3-tier architecture and I am really worried about how to handle operations in a transacted way.

我知道我有两个选择: IDbTransaction 的TransactionScope ...但我没有真正决定为哪一个去,虽然我做了很多研究。

I know I have 2 options: IDbTransaction and TransactionScope... but I am not really decided for which one to go, although I did a lot of research.

我会去的TransactionScope,但我不想涉及DTC ...加上我需要SQLServer2005的和Oracle的支持。 (我知道我需要只有一次打开的连接)

I would go for TransactionScope but I don't want to involve DTC... plus I need support for SQLServer2005 and Oracle. (I am aware that I need to have only one connection opened at a time)

我想看到他们的使用很好的例子/模式两种情况下...良好的链接会做就好了。

I would like to see good examples/patterns of their usage for both cases... Good links would do just fine.

喜欢的东西怎么BL类和DAL类会是什么样子......又是如何交易/连接创建,并在它们之间进行。

Something like how a BL class and DAL class would look like... but also how transaction/connections are created and carried between them.

EDIT1:
我找somekind的实现这一点(但是这两个选项):

I am looking for somekind of implementation of this (but for both options):

using(var scope = new TransactionScope())
{
    // transactional methods
    datalayer.InsertFoo();
    datalayer.InsertBar();
    scope.Complete();
}



EDIT2:
由于丹尼斯提供我一个非常不错的选择......我还是等着别人给我看一个模型的一个很好的例子与业务层,并使用的TransactionScope 数据层之间的交互

感谢您。

推荐答案

我在这种情况下使用目前多个存储库和多重的UnitOfWork方法。比方说,你有CustomerRepository和InvoiceRepository。如果你需要这样做:

What I use in this case currently is multiple repositories and multiple UnitOfWork approach. Lets say that you have CustomerRepository and InvoiceRepository. If you need to do this:

customerRepository.Add(customer);
invoiceRepository.Add(bill);

和有这两个作为交易,然后我做的是对资源库的创建我给他们同样的的UnitOfWork,如:

and have these two as a transaction, then what I do is on repository creation I give them the same UnitOfWork, like:

IUnitOfWork uow = UnitOfWork.Start();
ICustomerRepository customerRepository = new CustomerRepository(uow);
IInvoiceRepository invoiceRepository = new InvoiceRepository(uow);



这样的语句以上是现在:

so that statements above are now:

customerRepository.Add(customer);
invoiceRepository.Add(bill);
uow.Commit();



所有的魔法之下,依赖于你所使用的数据技术(无论是ORM像NHibernate的,或者。原ADO.NET - 这是不是在大多数情况下推荐使用)

All magic is beneath, dependent on what you use as data technology (either ORM like NHibernate, or maybe raw ADO.NET - and this is not recommended in most cases).

有关存储库模式和UnitOfWorks一个很好的例子,通过的this教程,但要注意它,你不能有多个UnitOfWorks活动的(和一些应用程序需要,实际上,所以没有真正的问题)。
此外,该教程使用NHibernate的,所以如果你不熟悉ORM的概念,我建议你进入它(如果你的时间表允许的话)。

For a good example on repository pattern and UnitOfWorks, go through this tutorial, but note that in it you can't have multiple UnitOfWorks active (and few applications need that actually, so no real problem there). Also, the tutorial uses NHibernate, so if you are not familiar with ORM concept, I suggest you get into it (if your timetable allows it).

一件事:你也有更先进的模式在这里,每一样交谈,这样的会议,但是这是在我在我的头上包着,现在,如果你想看看uNhAddIns项目(NHibernate的还先进的东西)

one more thing: you also have more advanced patterns here, like session per conversation and such, but this is advanced stuff in which I'm having my head wrapped in right now, if you want take a look at uNhAddIns projects (for NHibernate also)

这篇关于C# - 在业务层交易(SQLServer的2005+,甲骨文)的用法 - 很好的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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