· 5 years ago · Jul 19, 2020, 06:20 PM
1using Deventure.BusinessLogic.Workflow;
2using Deventure.DataLayer.Repositories;
3using System;
4using System.Collections.Generic;
5
6namespace UpWorky.BusinessLogic.ModelCore
7{
8 internal class RepoContextBase : IDisposable
9 {
10 private RepoUnitOfWork mUnitOfWork;
11
12 private bool mTracking;
13
14 private IDictionary<Guid, BaseDataRepository> ActiveRepositories;
15
16 public RepoUnitOfWork UnitOfWork => mUnitOfWork;
17
18 public bool IsTracking => mTracking;
19
20 public T GetRepo<T>()
21 where T : BaseDataRepository, new()
22 {
23 if (!ActiveRepositories.ContainsKey(typeof(T).GUID))
24 {
25 T newRepo = null;
26 if (IsTracking)
27 {
28 newRepo = mUnitOfWork.TrackingRepository<T>();
29 }
30 else
31 {
32 newRepo = mUnitOfWork.Repository<T>();
33 }
34
35 ActiveRepositories.Add(typeof(T).GUID, newRepo);
36
37 return newRepo;
38 }
39
40 return (T)ActiveRepositories[typeof(T).GUID];
41 }
42
43 public void Dispose()
44 {
45 if (mUnitOfWork != null)
46 {
47 foreach (var key in ActiveRepositories.Keys)
48 {
49 ActiveRepositories[key].Dispose();
50 }
51
52 ActiveRepositories.Clear();
53
54 mUnitOfWork.Dispose();
55 mUnitOfWork = null;
56 }
57 }
58
59 public RepoContextBase(bool tracking = true)
60 {
61 mUnitOfWork = RepoUnitOfWork.New();
62 mTracking = tracking;
63 ActiveRepositories = new Dictionary<Guid, BaseDataRepository>();
64 }
65 }
66
67 public class RepoContext : IDisposable
68 {
69 #region MEMBER DATA
70
71 private int Level = 0;
72
73 private RepoContextBase mInstance;
74
75 private RepoContextBase Instance => mInstance;
76
77 private bool mEnableChildren = false;
78
79 /// <summary>
80 /// If true child contexts RollbackTransaction() / CommitTransaction() calls will be valid.
81 /// </summary>
82 private bool IsEnableChildren => mEnableChildren;
83
84 public RepoUnitOfWork UnitOfWork => mInstance.UnitOfWork;
85
86 #endregion
87
88 #region PUBLIC API
89
90 /// <summary>
91 /// -> Gets or creates a new T repository <br/>
92 /// -> Once created, the repo instance will "exist" until the [RepoContext] instance is Disposed<br/>
93 /// </summary>
94 /// <typeparam name="T">Repository type</typeparam>
95 /// <returns></returns>
96 public T GetRepo<T>()
97 where T : BaseDataRepository, new()
98 {
99 return mInstance.GetRepo<T>();
100 }
101
102 public bool IsTracking => mInstance.IsTracking;
103
104 /// <summary>
105 /// This will do the commit logic only of Level is 0 (if this is the original context)
106 /// </summary>
107 public void CommitTransaction()
108 {
109 if (Level == 0 || mEnableChildren)
110 {
111 mInstance.UnitOfWork.CommitTransaction();
112 }
113 }
114
115 public bool IsTransactionFinalized => mInstance.UnitOfWork.IsTransactionFinalized;
116
117 /// <summary>
118 /// This will do the rollback logic only if Level is 0 (if this is the original context)
119 /// </summary>
120 public void RollbackTransaction()
121 {
122 if (Level == 0 || mEnableChildren)
123 {
124 mInstance.UnitOfWork.RollbackTransaction();
125 }
126 }
127
128 /// <summary>
129 /// -> Use this to pass a "safe" copy into a function <br/>
130 /// -> eg. MyMethodThatUsesAUnitOfWorkAndMayCreateNewRepos(context.NewChildContext() , ...)
131 /// </summary>
132 /// <returns></returns>
133 public RepoContext NewChildContext()
134 {
135 return new RepoContext(this);
136 }
137
138 public static RepoContext NewContext(bool enableChildren = false)
139 {
140 return new RepoContext(enableChildren);
141 }
142
143 #endregion
144
145 #region PRIVATE API
146
147 private RepoContext(bool enableChildren)
148 {
149 mEnableChildren = enableChildren;
150
151 mInstance = new RepoContextBase();
152 }
153
154 private RepoContext(RepoContext parent)
155 {
156 //New level
157 Level = parent.Level + 1;
158
159 //Same instance
160 mInstance = parent.mInstance;
161
162 //Same enable children
163 mEnableChildren = parent.mEnableChildren;
164 }
165
166 #endregion
167
168 #region IDISPOSABLE
169
170 public void Dispose()
171 {
172 if (mInstance != null)
173 {
174 mInstance.Dispose();
175 mInstance = null;
176 }
177 }
178
179 #endregion
180 }
181}