使用与EntityFramework的事务从业务逻辑层

时间:2012-05-08 06:19:25

标签: c# entity-framework entity-framework-4 transactionscope

请先看看这个: Good Coding Practices

所以,这是我的设计。

  1. 网站 2.商业逻辑层 3.DALFacade(我们使用dalfacade隐藏数据访问,因为我们使用2个不同的存储,sql和db2) 4.DAL
  2. 在DAL中,我们使用工作单元模式和存储库模式。 1.这里最大的问题是:如果下面的代码对于从业务逻辑创建的事务运行正常。?

    Page:
    
    public partial class NewBonusRequest : System.Web.UI.Page
    {
    
        #region Constructor and Instantiation of Business Logic
            /// <summary>
            /// Property that holds the Business Logic type to call methods
            /// </summary>
            public IRequestBL RequestBL { get; private set; }
    
            /// <summary>
            /// The default constructor will use the default implementation of the business logic interface
            /// </summary>
            public Request()
                : this(new RequestBL())
            {
            }
    
            /// <summary>
            /// The constructor accepts a IEcoBonusRequestFacade type
            /// </summary>
            /// <param name="ecoBonusRequestBL">IEcoBonusRequestFacade type</param>
            public NewRequest(IRequestBL RequestBL)
            {
                RequestBL = RequestBL;
            } 
        #endregion
    
    
        protected void PageLoad(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
    
            }
        }
    
    
        #region Control Events
            protected void BtnSubmitRequestClick(object sender, EventArgs e)
            {
                var request= new Request
                                           {
                                               IsOnHold = true
                                               //All other properties go here.
                                           };
    
                RequestBL.Save(request);
            }
    
    
    
        Business Logic Code.
    
        public interface IRequestBL
        {
            void Save(Request request);
        }
    
        /// <summary>
        /// Class in charge of the business logic for EcoBonusRequest
        /// </summary>
        public class RequestBL : IRequestBL
        {
            /// <summary>
    
    
            /// <summary>
            /// Saves a new ecobonus request into database and evaluate business rules here
            /// </summary>
            /// <param name="ecoBonusWorkflow">EcoBonusWorkflow entity</param>
            public void Save(Request Request)
            {
                using (var scope = new TransactionScope())
                {
                    Request.Save(request);
                    // Call to other DALCFacade methods that insert data in different tables
                    // OtherObject.Save(otherobject)
                    scope.Complete();
                }
            }
        }
    

1 个答案:

答案 0 :(得分:1)

是在同一个线程中,如果存在,EF将正确考虑事务范围。如果已经在一个交易中,EF将不会创建新交易。

但是,您必须小心,因为如果您在没有事务的情况下查询数据库,那么您将获得脏读。因为如果不存在,EF将不会读取事务中的任何内容,但如果在保存更改时它不存在则会创建新事务。

在您的代码中,您只保存事务中的更改,但在阅读时应该小心,并且您应该在较小的单位中将查询封装在范围内。