1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CY.Infrastructure.Domain;
using System.Transactions;
namespace CY.IBaseDAL
{
    /// <summary>
    /// 集合单元事务处理具体实现
    /// </summary>
    public class UnitOfWorkForSql:IUnitOfWork
    {
        private Dictionary<IAggregateRoot, ICommonDAL> addedEntities;
        private Dictionary<IAggregateRoot, ICommonDAL> changedEntities;
        private Dictionary<IAggregateRoot, ICommonDAL> deletedEntities;
 
        public UnitOfWorkForSql()
        {
            addedEntities = new Dictionary<IAggregateRoot, ICommonDAL>();
            changedEntities = new Dictionary<IAggregateRoot, ICommonDAL>();
            deletedEntities = new Dictionary<IAggregateRoot, ICommonDAL>();
        }
 
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="unitofWorkRepository"></param>
        public void RegisterAmended(IAggregateRoot entity, ICommonDAL unitofWorkRepository)
        {
            if (!changedEntities.ContainsKey(entity))
            {
                changedEntities.Add(entity, unitofWorkRepository);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="unitofWorkRepository"></param>
        public void RegisterNew(IAggregateRoot entity, ICommonDAL unitofWorkRepository)
        {
            if (!addedEntities.ContainsKey(entity))
            {
                addedEntities.Add(entity, unitofWorkRepository);
            };
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="unitofWorkRepository"></param>
        public void RegisterRemoved(IAggregateRoot entity, ICommonDAL unitofWorkRepository)
        {
            if (!deletedEntities.ContainsKey(entity))
            {
                deletedEntities.Add(entity, unitofWorkRepository);
            }
        }
        /// <summary>
        /// 事务提交
        /// </summary>
        public bool Commit()
        {
            bool isSuccess = true;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
            {
                foreach (IAggregateRoot entity in this.addedEntities.Keys)
                {
                     isSuccess = this.addedEntities[entity].InserModel(entity);
                     if(!isSuccess)
                         break;
                }
                if (isSuccess)
                {
                    foreach (IAggregateRoot entity in this.changedEntities.Keys)
                    {
                        isSuccess = this.changedEntities[entity].UpdateModel(entity);
                        if (!isSuccess)
                            break;
                    }
                }
                if (isSuccess)
                {
                    foreach (IAggregateRoot entity in this.deletedEntities.Keys)
                    {
                        isSuccess = this.deletedEntities[entity].DeleteModel(entity);
                        if (!isSuccess)
                            break;
                    }
                }
                if (isSuccess)
                scope.Complete();
            }
            return isSuccess;
        }
    }
}