N层架构中如何维护事务
我正在 N 层架构中开发应用程序。众所周知,我们需要在插入/更新/删除操作时实现事务。请告诉我如何在 N 层架构的 c#.net 中使用事务。 我的架构是这样的 Applicationform->middle_Layre->Factory->DataAccessLayre->StoredProcedure->Table 在应用程序表单中,我创建中间层对象并在中间层的插入/更新/删除函数中传递数据。 我正在工厂类中创建 sqlcommand 对象,并填充从中间层获取的数据,并将该对象 os sqlcommand 传递给 DAL。
I am developing application in N-Tier Architecture. as we all know that we need to implement transactions while insert/update/delete operation. please tell me how to use transaction in c#.net in N-Tier architecture.
my architecture is like this
Applicationform->middle_Layre->Factory->DataAccessLayre->StoredProcedure->Table
in application form i create object of middleLayer and pass data in Insert/update/delete function of middle layer.
i am creating object of sqlcommand in factoryclass and fill the data which i gets from middle layer and pass that object os sqlcommand to DAL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是您可以遵循的软件层的代表性模式:
在
此模型中,事务发生在存储库中,其中安排了“工作单元”。通常,这是通过从 DAL 请求数据、对其执行工作并保存更改来实现的。 DAL 通常会围绕您的工作单元封装事务。
数据库、DAL、存储库和 BLL 共同构成了 MVC(模型-视图-控制器)架构中的模型。所有业务逻辑和数据操作都发生在模型中。控制器充当模型和视图模型/UI 之间的中间人,它们共同形成视图。
存储库是您设置“工作单元”的地方。
*<子>可选
Here is a representative pattern of software layers you can follow:
Where
In this model, the transaction takes place in the Repository, where a "unit of work" is arranged. Typically, this happens by requesting data from the DAL, performing work on it, and saving changes. The DAL will generally wrap a transaction around your unit of work.
The Database, DAL, Repository and BLL collectively form what is known as the Model in MVC (Model-view-controller) architecture. All business logic and data manipulation takes place in the Model. The controller acts as a go-between between the model and the View Model/UI, which collectively form the view.
The Repository is where you would set up your "Unit of Work."
*Optional
只要您的代码在同一台计算机上运行,层之间没有任何 WCF 或 Web 服务调用,您就可以使用 TransactionScope。
http://msdn.microsoft.com/en-us/ library/system.transactions.transactionscope.aspx
只需将您希望在事务中发生的调用放在事务范围内即可。
As long as your code is running on the same machine, without any WCF or Web Service calls between the layers, you could use TransactionScope.
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx
Just place the calls that you want to occur with a transaction, inside the transaction scope.
另一种选择是将逻辑放在存储过程中。这样,您的 DAL 对数据库进行一次调用,在失败时回滚。
Another option is to place the logic in a stored procedure. This way your DAL makes a single call to the db that rolls back on failure.