DataDomain.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using Microsoft.EntityFrameworkCore;
  5. using Quadarax.Foundation.Core.Data.Interface.Domain;
  6. namespace Quadarax.Foundation.Core.Data.Domain
  7. {
  8. public abstract class DataDomain : DbContext, IDomain
  9. {
  10. private MethodInfo? _mtSet;
  11. private MethodInfo? _mtgSet;
  12. protected DataDomain(DbContextOptions options) : base(options)
  13. {
  14. }
  15. public TEntitySet? GetDbSet<TEntitySet>()
  16. {
  17. //TODO: comment because db context is in this scope static
  18. //if (_mtSet == null)
  19. _mtSet = GetType().GetMethod(nameof(DbContext.Set), Type.EmptyTypes);//GetType().GetMethod("Set");
  20. //if (_mtgSet==null)
  21. _mtgSet = _mtSet?.MakeGenericMethod(typeof(TEntitySet).GetGenericArguments().First());
  22. var returnValue = _mtgSet?.Invoke(this, new object[] {});
  23. return (TEntitySet?)returnValue;
  24. }
  25. public IUnitOfWork<TDomainContext> CreateUnitOfWork<TDomainContext>() where TDomainContext : IDomain
  26. {
  27. return new UnitOfWork<TDomainContext>((IDomainContextResolver<TDomainContext>)new DomainContextResolver<DataDomain>(this));
  28. }
  29. public void Commit()
  30. {
  31. this.SaveChanges();
  32. }
  33. }
  34. }