服务层中的@Transactional。NET MVC

时间:2020-12-13 16:34:38

Having worked with Spring MVC for JAVA, I've gotten used to having @Transactional behaviour being applied in the service layer which works fabulously well with Hibernate DAOs. Now that I'm faced with a similar challenge in a project which is based on LINQ to SQL ORM, I see that all transactions are taking places within DAOs ( i.e. using (DC dc = new DC() { dc.submitChanges()}

在使用JAVA的Spring MVC之后,我已经习惯了在服务层中应用@Transactional行为,它在Hibernate DAOs中工作得非常好。现在,我在一个基于LINQ到SQL ORM的项目中遇到了类似的挑战,我看到所有事务都发生在dao中(例如,使用(DC = new DC() {DC . submitchanges ()}))

How do you remove transaction logic from DAOs and bring it to the service layer in ASP.NET MVC LINQ2SQL project?

如何从DAOs中删除事务逻辑并将其带到ASP中的服务层。净MVC LINQ2SQL项目吗?

Thank you.

谢谢你!

1 个解决方案

#1


2  

It worked fabulously because Spring MVC had Hibernate's Session and SessionFactory registered and dealt with creating and committing transaction. You can do it in .NET as well but you must do it yourselves. There is no prepared integration of all these tools like Spring MVC provides to Java. You generally need to inject DataContext instance into service layer instead of creating it manually.

它的工作非常棒,因为Spring MVC已经注册了Hibernate的会话和SessionFactory,并处理了创建和提交事务。你也可以在。net中做,但是你必须自己做。没有准备好所有这些工具的集成,比如Spring MVC为Java提供的。通常需要将DataContext实例注入服务层,而不是手工创建它。

What you call service layer? For me the service layer is a model. In such case the stuff is mostly outside of ASP.NET MVC - there is a way how to create attribute for controller's action in ASP.NET MVC (custom filters) but that is not a service layer. If you want custom transactional attribute for service layer it mostly means Aspect oriented programming (AOP).

什么叫服务层?对我来说,服务层是一个模型。在这种情况下,内容主要是ASP之外的。NET MVC——有一种方法可以为ASP中的控制器动作创建属性。NET MVC(自定义过滤器),但这不是服务层。如果您想要为服务层定制事务属性,那么它主要意味着面向方面的编程(AOP)。

What are your choices? Select good IoC container which supports AOP - fore example Windsor Castle, Spring.NET, Unity 2.0 or use PostSharp for AOP. The easy way to make transactional AOP attribute is just creating TransactionScope before the annotated method is executed and Complete the scope after the annotated method is executed. But that is not what you want because you will still have to create DataContext instance and call SubmitChanges. You need AOP attribute which will create DataContext instance store it somewhere and execute annotated method. The method will be able to load stored context instance and use it. Once the method finishes the AOP attribute will call SubmitChanges. The only problem is where to store the context instance and how to retrieve it - I believe Spring MVC uses Spring internally for this and provides the whole infrastructure but in .NET you will have to write it - in case of Web app. you will have to store the context in HttpContext.Items.

你的选择是什么?选择好的IoC容器,它支持AOP -例如Windsor Castle, Spring。NET、Unity 2.0或使用PostSharp处理AOP。创建事务AOP属性的简单方法是在执行带注释的方法之前创建TransactionScope,并在执行带注释的方法之后完成该范围。但这不是您想要的,因为您仍然需要创建DataContext实例并调用SubmitChanges。您需要AOP属性,它将创建DataContext实例并将其存储在某处并执行带注释的方法。该方法将能够加载存储的上下文实例并使用它。一旦方法完成,AOP属性将调用SubmitChanges。唯一的问题是在哪里存储上下文实例以及如何检索它——我相信Spring MVC使用弹簧内部并提供整个基础设施但是你将不得不把它写在。net——的Web应用,你需要存储的上下文httpcontext . items。

#1


2  

It worked fabulously because Spring MVC had Hibernate's Session and SessionFactory registered and dealt with creating and committing transaction. You can do it in .NET as well but you must do it yourselves. There is no prepared integration of all these tools like Spring MVC provides to Java. You generally need to inject DataContext instance into service layer instead of creating it manually.

它的工作非常棒,因为Spring MVC已经注册了Hibernate的会话和SessionFactory,并处理了创建和提交事务。你也可以在。net中做,但是你必须自己做。没有准备好所有这些工具的集成,比如Spring MVC为Java提供的。通常需要将DataContext实例注入服务层,而不是手工创建它。

What you call service layer? For me the service layer is a model. In such case the stuff is mostly outside of ASP.NET MVC - there is a way how to create attribute for controller's action in ASP.NET MVC (custom filters) but that is not a service layer. If you want custom transactional attribute for service layer it mostly means Aspect oriented programming (AOP).

什么叫服务层?对我来说,服务层是一个模型。在这种情况下,内容主要是ASP之外的。NET MVC——有一种方法可以为ASP中的控制器动作创建属性。NET MVC(自定义过滤器),但这不是服务层。如果您想要为服务层定制事务属性,那么它主要意味着面向方面的编程(AOP)。

What are your choices? Select good IoC container which supports AOP - fore example Windsor Castle, Spring.NET, Unity 2.0 or use PostSharp for AOP. The easy way to make transactional AOP attribute is just creating TransactionScope before the annotated method is executed and Complete the scope after the annotated method is executed. But that is not what you want because you will still have to create DataContext instance and call SubmitChanges. You need AOP attribute which will create DataContext instance store it somewhere and execute annotated method. The method will be able to load stored context instance and use it. Once the method finishes the AOP attribute will call SubmitChanges. The only problem is where to store the context instance and how to retrieve it - I believe Spring MVC uses Spring internally for this and provides the whole infrastructure but in .NET you will have to write it - in case of Web app. you will have to store the context in HttpContext.Items.

你的选择是什么?选择好的IoC容器,它支持AOP -例如Windsor Castle, Spring。NET、Unity 2.0或使用PostSharp处理AOP。创建事务AOP属性的简单方法是在执行带注释的方法之前创建TransactionScope,并在执行带注释的方法之后完成该范围。但这不是您想要的,因为您仍然需要创建DataContext实例并调用SubmitChanges。您需要AOP属性,它将创建DataContext实例并将其存储在某处并执行带注释的方法。该方法将能够加载存储的上下文实例并使用它。一旦方法完成,AOP属性将调用SubmitChanges。唯一的问题是在哪里存储上下文实例以及如何检索它——我相信Spring MVC使用弹簧内部并提供整个基础设施但是你将不得不把它写在。net——的Web应用,你需要存储的上下文httpcontext . items。