-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AuditDbConverterFactory added (#115)
Co-authored-by: Andrii Kaplanovskyi <[email protected]>
- Loading branch information
1 parent
b744f3e
commit 9f69b3f
Showing
4 changed files
with
73 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...tudio.EntityAuditing/OneBeyond.Studio.EntityAuditing.SqlServer/AuditDbConverterFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using OneBeyond.Studio.Crosscuts.Reflection; | ||
using OneBeyond.Studio.EntityAuditing.Domain; | ||
|
||
namespace OneBeyond.Studio.EntityAuditing.SqlServer; | ||
|
||
internal static class AuditDbConverterFactory | ||
{ | ||
private static readonly MethodInfo _auditConverterConvertMethodInfoAsync = | ||
Reflector.MethodFrom(() => ConvertEntityEventAsync<object>(default, default, default)) | ||
.GetGenericMethodDefinition(); | ||
|
||
private static readonly ConcurrentDictionary<Type, Func<IAuditDbConverter, object, AuditEvent, Task<Entities.AuditEvent>>> _auditConverterFuncs | ||
= new(); | ||
|
||
public static Func<IAuditDbConverter, object, AuditEvent, Task<Entities.AuditEvent>> GetOrCompileConvertFunction(Type entityType) | ||
{ | ||
if (!_auditConverterFuncs.TryGetValue(entityType, out var auditConverterFunc)) | ||
{ | ||
auditConverterFunc = CreateConvertFunc(entityType); | ||
_auditConverterFuncs.AddOrUpdate(entityType, auditConverterFunc, (type, function) => function); | ||
} | ||
return auditConverterFunc; | ||
} | ||
|
||
private static Func<IAuditDbConverter, object, AuditEvent, Task<Entities.AuditEvent>> CreateConvertFunc(Type entityType) | ||
{ | ||
var methodInfo = _auditConverterConvertMethodInfoAsync.MakeGenericMethod(entityType); | ||
|
||
var auditWriterParameter = Expression.Parameter(typeof(IAuditDbConverter), "auditConverter"); | ||
var entityParameter = Expression.Parameter(typeof(object), "entity"); | ||
var eventParameter = Expression.Parameter(typeof(AuditEvent), "event"); | ||
|
||
var methodCall = Expression.Call( | ||
methodInfo, | ||
auditWriterParameter, | ||
entityParameter, | ||
eventParameter); | ||
|
||
var lambda = Expression.Lambda<Func<IAuditDbConverter, object, AuditEvent, Task<Entities.AuditEvent>>>( | ||
methodCall, | ||
auditWriterParameter, | ||
entityParameter, | ||
eventParameter); | ||
|
||
return lambda.Compile(); | ||
} | ||
|
||
private static Task ConvertEntityEventAsync<TEntity>( | ||
IAuditDbConverter auditConverter, | ||
object entity, | ||
AuditEvent @event) | ||
where TEntity : class | ||
=> ((AuditDbConverter<TEntity>)auditConverter).ConvertAsync((TEntity)entity, @event, CancellationToken.None); | ||
} | ||
|