Skip to content

Commit c6fe00e

Browse files
committed
finally all aspects ok for triple key repo
1 parent e151525 commit c6fe00e

1 file changed

Lines changed: 205 additions & 49 deletions

File tree

SharpRepository.Repository/CompoundKeyRepositoryBase.cs

Lines changed: 205 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,12 @@ public override void Update(T entity)
903903
{
904904
if (entity == null) throw new ArgumentNullException("entity");
905905

906+
if (!RunAspect(attribute => attribute.OnUpdateExecuting(entity, _repositoryActionContext)))
907+
return;
908+
906909
UpdateItem(entity);
910+
911+
RunAspect(attribute => attribute.OnUpdateExecuted(entity, _repositoryActionContext));
907912
if (BatchMode) return;
908913

909914
Save();
@@ -914,9 +919,14 @@ public override void Update(T entity)
914919

915920
private void Save()
916921
{
922+
if (!RunAspect(attribute => attribute.OnSaveExecuting(_repositoryActionContext)))
923+
return;
924+
917925
SaveChanges();
918926

919927
_queryManager.OnSaveExecuted();
928+
929+
RunAspect(attribute => attribute.OnSaveExecuted(_repositoryActionContext));
920930
}
921931

922932
protected virtual bool GetPrimaryKey(T entity, out TKey key, out TKey2 key2)
@@ -985,6 +995,8 @@ public abstract partial class CompoundKeyRepositoryBase<T, TKey, TKey2, TKey3>
985995
// the query manager uses the caching strategy to determine if it should check the cache or run the query
986996
private CompoundKeyQueryManager<T, TKey, TKey2, TKey3> _queryManager;
987997

998+
private readonly RepositoryActionContext<T, TKey,TKey2,TKey3> _repositoryActionContext;
999+
9881000
public override bool CacheUsed
9891001
{
9901002
get { return _queryManager.CacheUsed; }
@@ -1010,6 +1022,9 @@ protected CompoundKeyRepositoryBase(ICompoundKeyCachingStrategy<T, TKey, TKey2,
10101022
}
10111023

10121024
CachingStrategy = cachingStrategy ?? new NoCachingStrategy<T, TKey, TKey2, TKey3>();
1025+
1026+
_repositoryActionContext = new RepositoryActionContext<T, TKey, TKey2, TKey3>(this);
1027+
RunAspect(aspect => aspect.OnInitialized(_repositoryActionContext));
10131028
}
10141029

10151030
public ICompoundKeyCachingStrategy<T, TKey, TKey2, TKey3> CachingStrategy
@@ -1067,47 +1082,116 @@ public override IEnumerable<T> GetAll(IQueryOptions<T> queryOptions, IFetchStrat
10671082

10681083
public override IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions, IFetchStrategy<T> fetchStrategy)
10691084
{
1070-
if (selector == null) throw new ArgumentNullException("selector");
1085+
try
1086+
{
1087+
if (selector == null) throw new ArgumentNullException("selector");
10711088

1072-
return _queryManager.ExecuteGetAll(
1073-
() => GetAllQuery(queryOptions, fetchStrategy).Select(selector).ToList(),
1074-
selector,
1075-
queryOptions
1076-
);
1089+
var context = new CompoundTripleKeyRepositoryQueryMultipleContext<T, TKey, TKey2, TKey3, TResult>(this, null, queryOptions, selector);
1090+
if (!RunAspect(attribute => attribute.OnGetAllExecuting(context)))
1091+
return context.Results;
1092+
1093+
// if the aspect altered the specificaiton then we need to run a FindAll with that specification
1094+
IEnumerable<TResult> results;
1095+
1096+
if (context.Specification == null)
1097+
{
1098+
results = _queryManager.ExecuteGetAll(
1099+
() => GetAllQuery(context.QueryOptions, fetchStrategy).Select(context.Selector).ToList(),
1100+
context.Selector,
1101+
context.QueryOptions
1102+
);
1103+
}
1104+
else
1105+
{
1106+
context.Specification.FetchStrategy = fetchStrategy;
1107+
1108+
results = _queryManager.ExecuteFindAll(
1109+
() => FindAllQuery(context.Specification, context.QueryOptions).Select(context.Selector).ToList(),
1110+
context.Specification,
1111+
context.Selector,
1112+
context.QueryOptions
1113+
);
1114+
}
1115+
1116+
context.Results = results;
1117+
RunAspect(attribute => attribute.OnGetAllExecuted(context));
1118+
1119+
return context.Results;
1120+
}
1121+
catch (Exception ex)
1122+
{
1123+
Error(ex);
1124+
throw;
1125+
}
10771126
}
10781127

10791128
protected abstract T GetQuery(TKey key, TKey2 key2, TKey3 key3);
10801129

10811130
public T Get(TKey key, TKey2 key2, TKey3 key3)
10821131
{
1083-
return _queryManager.ExecuteGet(
1084-
() => GetQuery(key, key2, key3),
1085-
null,
1086-
key,
1087-
key2,
1088-
key3
1089-
);
1132+
try
1133+
{
1134+
var context = new CompoundTripleKeyRepositoryGetContext<T, TKey, TKey2, TKey3>(this, key, key2, key3);
1135+
if (!RunAspect(attribute => attribute.OnGetExecuting(context)))
1136+
return context.Result;
1137+
1138+
var result = _queryManager.ExecuteGet(
1139+
() => GetQuery(context.Id, context.Id2, context.Id3),
1140+
context.Selector,
1141+
context.Id,
1142+
context.Id2,
1143+
context.Id3
1144+
);
1145+
1146+
context.Result = result;
1147+
RunAspect(attribute => attribute.OnGetExecuted(context));
1148+
1149+
return context.Result;
1150+
}
1151+
catch (Exception ex)
1152+
{
1153+
Error(ex);
1154+
throw;
1155+
}
10901156
}
10911157

10921158
public TResult Get<TResult>(TKey key, TKey2 key2, TKey3 key3, Expression<Func<T, TResult>> selector)
10931159
{
1094-
if (selector == null) throw new ArgumentNullException("selector");
1160+
try
1161+
{
1162+
if (selector == null) throw new ArgumentNullException("selector");
10951163

1096-
return _queryManager.ExecuteGet(
1097-
() =>
1098-
{
1099-
var result = GetQuery(key, key2, key3);
1100-
if (result == null)
1101-
return default(TResult);
1164+
var context = new CompoundTripleKeyRepositoryGetContext<T, TKey, TKey2, TKey3, TResult>(this, key, key2, key3, selector);
1165+
if (!RunAspect(attribute => attribute.OnGetExecuting(context)))
1166+
return context.Result;
11021167

1103-
var results = new[] { result };
1104-
return results.AsQueryable().Select(selector).First();
1105-
},
1106-
selector,
1107-
key,
1108-
key2,
1109-
key3
1110-
);
1168+
// get the full entity, possibly from cache
1169+
var item = _queryManager.ExecuteGet(
1170+
() =>
1171+
{
1172+
var result = GetQuery(key, key2, key3);
1173+
if (result == null)
1174+
return default(TResult);
1175+
1176+
var results = new[] { result };
1177+
return results.AsQueryable().Select(selector).First();
1178+
},
1179+
selector,
1180+
context.Id,
1181+
context.Id2,
1182+
context.Id3
1183+
);
1184+
1185+
context.Result = item;
1186+
RunAspect(attribute => attribute.OnGetExecuted(context));
1187+
1188+
return context.Result;
1189+
}
1190+
catch (Exception ex)
1191+
{
1192+
Error(ex);
1193+
throw;
1194+
}
11111195
}
11121196

11131197
public bool Exists(TKey key, TKey2 key2, TKey3 key3)
@@ -1147,38 +1231,91 @@ public bool TryGet<TResult>(TKey key, TKey2 key2, TKey3 key3, Expression<Func<T,
11471231

11481232
public override IEnumerable<T> FindAll(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null)
11491233
{
1150-
if (criteria == null) throw new ArgumentNullException("criteria");
1234+
if (criteria == null) return GetAll(queryOptions);
11511235

1152-
return _queryManager.ExecuteFindAll(
1153-
() => FindAllQuery(criteria, queryOptions).ToList(),
1154-
criteria,
1155-
null,
1156-
queryOptions
1157-
);
1236+
try
1237+
{
1238+
var context = new CompoundTripleKeyRepositoryQueryMultipleContext<T, TKey, TKey2, TKey3>(this, criteria, queryOptions);
1239+
if (!RunAspect(attribute => attribute.OnFindAllExecuting(context)))
1240+
return context.Results;
1241+
1242+
var results = _queryManager.ExecuteFindAll(
1243+
() => FindAllQuery(context.Specification, context.QueryOptions).ToList(),
1244+
context.Specification,
1245+
null,
1246+
context.QueryOptions
1247+
);
1248+
1249+
context.Results = results;
1250+
RunAspect(attribute => attribute.OnFindAllExecuted(context));
1251+
1252+
return context.Results;
1253+
}
1254+
catch (Exception ex)
1255+
{
1256+
Error(ex);
1257+
throw;
1258+
}
11581259
}
11591260

11601261
public override IEnumerable<TResult> FindAll<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
11611262
{
1162-
if (criteria == null) throw new ArgumentNullException("criteria");
1263+
if (criteria == null) return GetAll(selector, queryOptions);
11631264

1164-
return _queryManager.ExecuteFindAll(
1165-
() => FindAllQuery(criteria, queryOptions).Select(selector).ToList(),
1166-
criteria,
1167-
selector,
1168-
queryOptions
1169-
);
1265+
try
1266+
{
1267+
if (selector == null) throw new ArgumentNullException("selector");
1268+
1269+
var context = new CompoundTripleKeyRepositoryQueryMultipleContext<T, TKey, TKey2, TKey3, TResult>(this, criteria, queryOptions, selector);
1270+
if (!RunAspect(attribute => attribute.OnFindAllExecuting(context)))
1271+
return context.Results;
1272+
1273+
var results = _queryManager.ExecuteFindAll(
1274+
() => FindAllQuery(context.Specification, context.QueryOptions).Select(context.Selector).ToList(),
1275+
context.Specification,
1276+
context.Selector,
1277+
context.QueryOptions
1278+
);
1279+
1280+
context.Results = results;
1281+
RunAspect(attribute => attribute.OnFindAllExecuted(context));
1282+
1283+
return context.Results;
1284+
}
1285+
catch (Exception ex)
1286+
{
1287+
Error(ex);
1288+
throw;
1289+
}
11701290
}
11711291

11721292
public override T Find(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null)
11731293
{
1174-
if (criteria == null) throw new ArgumentNullException("criteria");
1294+
try
1295+
{
1296+
if (criteria == null) throw new ArgumentNullException("criteria");
11751297

1176-
return _queryManager.ExecuteFind(
1177-
() => FindQuery(criteria, queryOptions),
1178-
criteria,
1179-
null,
1180-
null
1181-
);
1298+
var context = new CompoundTripleKeyRepositoryQuerySingleContext<T, TKey, TKey2, TKey3>(this, criteria, queryOptions);
1299+
if (!RunAspect(attribute => attribute.OnFindExecuting(context)))
1300+
return context.Result;
1301+
1302+
var item = _queryManager.ExecuteFind(
1303+
() => FindQuery(context.Specification, context.QueryOptions),
1304+
context.Specification,
1305+
null,
1306+
null
1307+
);
1308+
1309+
context.Result = item;
1310+
RunAspect(attribute => attribute.OnFindExecuted(context));
1311+
1312+
return context.Result;
1313+
}
1314+
catch (Exception ex)
1315+
{
1316+
Error(ex);
1317+
throw;
1318+
}
11821319
}
11831320

11841321
public override TResult Find<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null)
@@ -1224,7 +1361,12 @@ public override void Add(T entity)
12241361
{
12251362
if (entity == null) throw new ArgumentNullException("entity");
12261363

1364+
if (!RunAspect(attribute => attribute.OnAddExecuting(entity, _repositoryActionContext)))
1365+
return;
1366+
12271367
AddItem(entity);
1368+
1369+
RunAspect(attribute => attribute.OnAddExecuted(entity, _repositoryActionContext));
12281370
if (BatchMode) return;
12291371

12301372
Save();
@@ -1237,7 +1379,12 @@ public override void Delete(T entity)
12371379
{
12381380
if (entity == null) throw new ArgumentNullException("entity");
12391381

1382+
if (!RunAspect(attribute => attribute.OnDeleteExecuting(entity, _repositoryActionContext)))
1383+
return;
1384+
12401385
DeleteItem(entity);
1386+
1387+
RunAspect(attribute => attribute.OnDeleteExecuted(entity, _repositoryActionContext));
12411388
if (BatchMode) return;
12421389

12431390
Save();
@@ -1259,7 +1406,12 @@ public override void Update(T entity)
12591406
{
12601407
if (entity == null) throw new ArgumentNullException("entity");
12611408

1409+
if (!RunAspect(attribute => attribute.OnUpdateExecuting(entity, _repositoryActionContext)))
1410+
return;
1411+
12621412
UpdateItem(entity);
1413+
1414+
RunAspect(attribute => attribute.OnUpdateExecuted(entity, _repositoryActionContext));
12631415
if (BatchMode) return;
12641416

12651417
Save();
@@ -1270,9 +1422,13 @@ public override void Update(T entity)
12701422

12711423
private void Save()
12721424
{
1425+
if (!RunAspect(attribute => attribute.OnSaveExecuting(_repositoryActionContext)))
1426+
return;
1427+
12731428
SaveChanges();
12741429

12751430
_queryManager.OnSaveExecuted();
1431+
RunAspect(attribute => attribute.OnSaveExecuted(_repositoryActionContext));
12761432
}
12771433

12781434
protected virtual bool GetPrimaryKey(T entity, out TKey key, out TKey2 key2, out TKey3 key3)

0 commit comments

Comments
 (0)