Update MD2

This commit is contained in:
Chris Chen
2024-05-02 15:24:13 -07:00
parent eea11029e9
commit ed89de631b
93 changed files with 3392 additions and 152 deletions
@@ -0,0 +1,216 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Transactions;
using System;
using Church.Net.DAL.EF;
using System.Linq;
using Church.Net.Utility;
using System.Threading.Tasks;
using Church.Net.Entity.Interface;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Church.Net.DAL.EFCoreDBF.Interface;
namespace Church.Net.DAL.EFCoreDBF.Core
{
public class CrudDALCBase1<T> : ICrudDAL<T> where T : class, IEntity, new()
{
private readonly ChurchNetContext dbContext;
public CrudDALCBase1(DatabaseOptions databaseOptions)
{
dbContext = databaseOptions.GetDbContext();
InitKeyProperty();
}
public IQueryable<T> GetDbSet()
{
//var result = (DbSet<T>)typeof(ChurchNetContext).GetMethod("Set").MakeGenericMethod(typeof(T)).Invoke(dbContext, null);
return dbContext.Set<T>().AsNoTracking();
//return result;
}
public virtual T First(Func<T, bool> filter = null)
{
return GetDbSet().Where(filter ?? (s => true)).FirstOrDefault();
}
public virtual T GetById(string Id)
{
return GetQuery(new string[] { Id }).FirstOrDefault();
}
//public virtual T GetByRefndx(string refndx)
//{
// return MapDBModelToEntity(GetFirstDBModelByRefndx<ST>(refndx, this.actionScreen));
//}
public virtual IEnumerable<T> GetAll(Func<T, bool> filter = null)
{
//var dbObjs = GetDbSet().ToArray();
//IEnumerable<T> list = GetDbSet().Where(filter ?? (s => true)).ToList();
return GetDbSet().AsNoTracking().Where(filter ?? (s => true)).ToList();
}
public virtual int Create(T entity)
{
CheckKeyIsEmpty(entity);
ConvertUTCTime(entity);
dbContext.Add(entity);
return dbContext.SaveChanges();
}
public virtual Task<int> CreateAsync(T entity)
{
CheckKeyIsEmpty(entity);
ConvertUTCTime(entity);
dbContext.Add(entity);
//CreateDone(entity, newDbObj);
return dbContext.SaveChangesAsync();
}
public int CreateOrUpdate(T entity)
{
int result = 0;
ConvertUTCTime(entity);
if (CheckExist(entity))
{
result = Update(entity);
}
else
{
result = Create(entity);
}
return result;
}
public virtual int Update(T entity)
{
//if (!this.CheckExist(entity))
//{
// throw new ArgumentNullException("the Id is not exist.");
//}
ConvertUTCTime(entity);
dbContext.Update(entity);
return dbContext.SaveChanges();
}
public virtual void CreateDone(T entity)
{
}
public virtual void UpdateDone(T entity)
{
}
public virtual int UpdateRange(IEnumerable<T> entities)
{
dbContext.UpdateRange(entities);
return dbContext.SaveChanges();
}
public virtual int Delete(T obj)
{
dbContext.Remove(obj);
return dbContext.SaveChanges();
}
public int Delete(Func<T, bool> filter)
{
var list = GetDbSet().Where(filter).ToList();
foreach (var item in list)
{
dbContext.Remove(item);
}
return dbContext.SaveChanges();
}
public virtual bool CheckExist(T obj)
{
//var query = GetDbSet().AsQueryable();
var query = GetAll();
List<PropertyInfo> props = new List<PropertyInfo>();
for (int i = 0; i < keyProps.Count; i++)
{
var prop = keyProps[i];
props.Add(prop);
var value = prop.GetValue(obj)?.ToString();
query = query.Where(e => prop.GetValue(e)?.ToString() == value).ToList();
}
return query.Any();
}
private void ConvertUTCTime(T entity)
{
var props = typeof(T).GetProperties();
foreach (var prop in props)
{
if (prop.PropertyType == typeof(DateTime))
{
//do stuff like prop.SetValue(t, DateTime.Now, null);
DateTime utcTime = ((DateTime)prop.GetValue(entity)).ToUniversalTime();
prop.SetValue(entity, utcTime, null);
}
}
}
private List<PropertyInfo> keyProps;
protected virtual void InitKeyProperty()
{
keyProps = typeof(T).GetProperties().Where(
prop => Attribute.IsDefined(prop, typeof(KeyAttribute))).OrderBy(prop =>
prop.GetCustomAttribute<ColumnAttribute>().Order
)
.ToList();
}
protected IQueryable<T> GetQuery(IEnumerable<string> Ids)
{
var query = GetDbSet().AsQueryable();
for (int i = 0; i < keyProps.Count; i++)
{
query = query.Where(e => keyProps[i].GetValue(e).ToString() == Ids.ElementAt(i));
}
return query;
}
protected virtual void CheckKeyIsEmpty(T entity)
{
if (string.IsNullOrWhiteSpace(entity.Id) || entity.Id.Length <= 3)
{
entity.Id = StringHelper.Get33BaseGuid();
}
}
public void Dispose()
{
throw new NotImplementedException();
}
public IEnumerable<T> GetAllById(IEnumerable<string> Ids)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,254 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Transactions;
using System;
using Church.Net.DAL.EF;
using System.Linq;
using Church.Net.Utility;
using System.Threading.Tasks;
using Church.Net.Entity.Interface;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Church.Net.DAL.EFCoreDBF.Interface;
namespace Church.Net.DAL.EFCoreDBF.Core
{
public class CombinedKeyCrudDALCBase<T> : ICombinedKeyCrudDAL<T> where T : class, ICombinedKeyEntity, new()
{
private readonly DatabaseOptions databaseOptions;
public CombinedKeyCrudDALCBase(DatabaseOptions databaseOptions)
{
this.databaseOptions = databaseOptions;
InitKeyProperty();
}
public IQueryable<T> GetDbSet()
{
//var result = (DbSet<T>)typeof(ChurchNetContext).GetMethod("Set").MakeGenericMethod(typeof(T)).Invoke(dbContext, null);
return databaseOptions.GetDbContext().Set<T>().AsNoTracking();
//return result;
}
public ChurchNetContext GetDbContext()
{
//var result = (DbSet<T>)typeof(ChurchNetContext).GetMethod("Set").MakeGenericMethod(typeof(T)).Invoke(dbContext, null);
return databaseOptions.GetDbContext();//.Set<T>().AsNoTracking();
//return result;
}
public virtual T First(Func<T, bool> filter = null)
{
using (var dbContext = GetDbContext())
{
return dbContext.Set<T>().Where(filter ?? (s => true)).FirstOrDefault();
}
}
public virtual T GetById(IEnumerable<string> Ids)
{
return GetQuery(Ids).FirstOrDefault();
}
//public virtual T GetByRefndx(string refndx)
//{
// return MapDBModelToEntity(GetFirstDBModelByRefndx<ST>(refndx, this.actionScreen));
//}
public virtual IEnumerable<T> GetAll(Func<T, bool> filter = null)
{
//var dbObjs = GetDbSet().ToArray();
//IEnumerable<T> list = GetDbSet().Where(filter ?? (s => true)).ToList();
return GetDbSet().AsNoTracking().Where(filter ?? (s => true)).ToList();
}
public virtual IQueryable<T> GetQuery(Func<T, bool> filter = null)
{
//var dbObjs = GetDbSet().ToArray();
//IEnumerable<T> list = GetDbSet().Where(filter ?? (s => true)).ToList();
return GetDbSet().AsNoTracking().Where(filter ?? (s => true)).AsQueryable();
}
public virtual int Create(T entity)
{
CheckCombinedKeyIsEmpty(entity);
ConvertUTCTime(entity);
using (var dbContext = GetDbContext())
{
dbContext.Add(entity);
return dbContext.SaveChanges();
}
}
public virtual Task<int> CreateAsync(T entity)
{
CheckCombinedKeyIsEmpty(entity);
ConvertUTCTime(entity);
using (var dbContext = GetDbContext())
{
dbContext.Add(entity);
//CreateDone(entity, newDbObj);
return dbContext.SaveChangesAsync();
}
}
public int CreateOrUpdate(T entity)
{
int result = 0;
ConvertUTCTime(entity);
if (CheckExist(entity))
{
result = Update(entity);
}
else
{
result = Create(entity);
}
return result;
}
public virtual int Update(T entity)
{
//if (!this.CheckExist(entity))
//{
// throw new ArgumentNullException("the Id is not exist.");
//}
ConvertUTCTime(entity);
using (var dbContext = GetDbContext())
{
dbContext.Update(entity);
return dbContext.SaveChanges();
}
}
public virtual void CreateDone(T entity)
{
}
public virtual void UpdateDone(T entity)
{
}
public virtual int UpdateRange(IEnumerable<T> entities)
{
using (var dbContext = GetDbContext())
{
dbContext.UpdateRange(entities);
return dbContext.SaveChanges();
}
}
public virtual int Delete(T obj)
{
using (var dbContext = GetDbContext())
{
dbContext.Remove(obj);
return dbContext.SaveChanges();
}
}
public virtual int Delete(IEnumerable<string> combinedKeyIds)
{
var obj = GetById(combinedKeyIds);
return obj == null ? Delete(obj) : 0;
}
public int Delete(Func<T, bool> filter)
{
var list = GetDbSet().Where(filter).ToList();
using (var dbContext = GetDbContext())
{
foreach (var item in list)
{
dbContext.Remove(item);
}
return dbContext.SaveChanges();
}
}
public virtual bool CheckExist(T obj)
{
//var query = GetDbSet().AsQueryable();
var query = GetAll();
List<PropertyInfo> props = new List<PropertyInfo>();
for (int i = 0; i < keyProps.Count; i++)
{
var prop = keyProps[i];
props.Add(prop);
var value = prop.GetValue(obj)?.ToString();
query = query.Where(e => prop.GetValue(e)?.ToString() == value).ToList();
}
return query.Any();
}
private void ConvertUTCTime(T entity)
{
var props = typeof(T).GetProperties();
foreach (var prop in props)
{
if (prop.PropertyType == typeof(DateTime))
{
//do stuff like prop.SetValue(t, DateTime.Now, null);
DateTime utcTime = ((DateTime)prop.GetValue(entity)).ToUniversalTime();
prop.SetValue(entity, utcTime, null);
}
}
}
private List<PropertyInfo> keyProps;
private void InitKeyProperty()
{
keyProps = typeof(T).GetProperties().Where(
prop => Attribute.IsDefined(prop, typeof(KeyAttribute))).OrderBy(prop =>
prop.GetCustomAttribute<ColumnAttribute>().Order
)
.ToList();
}
private IQueryable<T> GetQuery(IEnumerable<string> Ids)
{
using (var dbContext = GetDbContext())
{
var query = dbContext.Set<T>().AsQueryable();
for (int i = 0; i < keyProps.Count; i++)
{
query = query.Where(e => keyProps[i].GetValue(e).ToString() == Ids.ElementAt(i));
}
return query;
}
}
private void CheckCombinedKeyIsEmpty(T entity)
{
for (int i = 0; i < keyProps.Count; i++)
{
if (string.IsNullOrEmpty(keyProps[i].GetValue(entity)?.ToString()))
{
throw new Exception($"Combined Key [{keyProps[i].Name}] not defined!");
}
}
}
}
}
@@ -0,0 +1,286 @@
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Transactions;
using System;
using Church.Net.DAL.EF;
using System.Linq;
using Church.Net.Utility;
using System.Threading.Tasks;
using Church.Net.Entity.Interface;
using Church.Net.DAL.EFCoreDBF.Interface;
namespace Church.Net.DAL.EFCoreDBF.Core
{
public class CrudDALCBase<T> : ICrudDAL<T> where T : class, IEntity, new()
{
private readonly DatabaseOptions databaseOptions;
//private readonly ChurchNetContext dbContext;
public CrudDALCBase(DatabaseOptions databaseOptions)
{
this.databaseOptions = databaseOptions;
//this.dbContext = databaseOptions.GetDbContext();
}
public virtual IQueryable<T> InitQuery(ChurchNetContext dbContext)
{
return dbContext.Set<T>();
}
public ChurchNetContext GetDbContext()
{
//var result = (DbSet<T>)typeof(ChurchNetContext).GetMethod("Set").MakeGenericMethod(typeof(T)).Invoke(dbContext, null);
return databaseOptions.GetDbContext();//.Set<T>().AsNoTracking();
//return result;
}
public virtual T First(Func<T, bool> filter = null)
{
using (var dbContext = GetDbContext())
{
return InitQuery(dbContext).Where(filter ?? (s => true)).FirstOrDefault();
}
}
public virtual T GetById(string Id)
{
using (var dbContext = GetDbContext())
{
return InitQuery(dbContext).FirstOrDefault(e => e.Id == Id);
}
}
//public virtual T GetByRefndx(string refndx)
//{
// return MapDBModelToEntity(GetFirstDBModelByRefndx<ST>(refndx, this.actionScreen));
//}
public virtual IEnumerable<T> GetAll(Func<T, bool> filter = null)
{
using (var dbContext = GetDbContext())
{
return InitQuery(dbContext).Where(filter ?? (s => true)).ToList();
}
//var dbObjs = GetDbContext().ToArray();
//IEnumerable<T> list = GetDbContext().Where(filter ?? (s => true)).ToList();
}
public virtual IEnumerable<T> GetAllById(IEnumerable<string> Ids)
{
using (var dbContext = GetDbContext())
{
var RowIds = Ids.Select(i => i.ToString());
//return GetDbContext().Where(e => RowIds.Any(id => id == e.Id));
//var list = new List<T>();
return InitQuery(dbContext).Where(e => RowIds.Any(id => id == e.Id)).ToArray();
}
}
private bool needGenId(T entity)
{
return string.IsNullOrWhiteSpace(entity.Id) || entity.Id.Length <= 3;
}
public virtual int Create(T entity)
{
if (needGenId(entity))
{
entity.Id = StringHelper.Get33BaseGuid();
}
ConvertUTCTime(entity);
using (var dbContext = GetDbContext())
{
dbContext.Add(entity);
return dbContext.SaveChanges();
}
}
public virtual Task<int> CreateAsync(T entity)
{
int result = 0;
ConvertUTCTime(entity);
if (needGenId(entity))
{
entity.Id = StringHelper.Get33BaseGuid();
}
T newDbObj = new T();
if (newDbObj != null)
{
//AfterCreatMapping(entity, newDbObj);
//newDbObj.RowId = entity.Id.ToString();
//result = Add(newDbObj) ? 1 : 0;
}
using (var dbContext = GetDbContext())
{
dbContext.Add(entity);
//CreateDone(entity, newDbObj);
return dbContext.SaveChangesAsync();
}
}
public virtual string CreateReturnId(T entity)
{
ConvertUTCTime(entity);
if (Create(entity) > 0)
{
return entity.Id;
}
else
{
return null;
};
}
public int CreateOrUpdate(T entity)
{
ConvertUTCTime(entity);
using (var dbContext = GetDbContext())
{
if (CheckExist(entity))
{
dbContext.Update(entity);
}
else
{
if (needGenId(entity))
{
entity.Id = StringHelper.Get33BaseGuid();
}
dbContext.Add(entity);
}
return dbContext.SaveChanges();
}
}
public virtual int Update(T entity)
{
using (var dbContext = GetDbContext())
{
//var dbObj = this.InitQuery(dbContext).Any(e => e.Id == entity.Id);
if (!InitQuery(dbContext).Any(e => e.Id == entity.Id))
{
throw new ArgumentNullException("the Id is not exist.");
}
ConvertUTCTime(entity);
dbContext.Update(entity);
return dbContext.SaveChanges();
}
}
public virtual void CreateDone(T entity)
{
}
public virtual void UpdateDone(T entity)
{
}
public virtual int UpdateRange(IEnumerable<T> entities)
{
foreach (var entity in entities)
{
ConvertUTCTime(entity);
}
using (var dbContext = GetDbContext())
{
dbContext.UpdateRange(entities);
return dbContext.SaveChanges();
}
}
public virtual int Delete(T obj)
{
using (var dbContext = GetDbContext())
{
dbContext.Remove(InitQuery(dbContext).FirstOrDefault(e => e.Id == obj.Id.ToString()));
return dbContext.SaveChanges();
}
}
public int Delete(Func<T, bool> filter)
{
using (var dbContext = GetDbContext())
{
var list = InitQuery(dbContext).Where(filter).ToList();
if (list.Count > 0)
{
dbContext.RemoveRange(list);
return dbContext.SaveChanges();
}
return 0;
}
}
public virtual bool CheckExist(T obj)
{
using (var dbContext = GetDbContext())
{
return InitQuery(dbContext).Any(e => e.Id == obj.Id);
}
}
private void ConvertUTCTime(T entity)
{
var props = typeof(T).GetProperties();
foreach (var prop in props)
{
if (prop.PropertyType == typeof(DateTime))
{
//do stuff like prop.SetValue(t, DateTime.Now, null);
DateTime localTime = (DateTime)prop.GetValue(entity);
if (localTime.Kind != DateTimeKind.Utc)
{
localTime = new DateTime(localTime.Year, localTime.Month, localTime.Day,
localTime.Hour, localTime.Minute, localTime.Second,
localTime.Millisecond, DateTimeKind.Local);
}
var offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
var OutputTime = DateTime.SpecifyKind(localTime - offset, DateTimeKind.Utc);
DateTime utcTime = localTime.ToUniversalTime();
prop.SetValue(entity, utcTime, null);
}
else if (prop.PropertyType == typeof(DateTime?))
{
DateTime? localTime = (DateTime?)prop.GetValue(entity);
if (localTime.HasValue)
{
if (localTime.Value.Kind != DateTimeKind.Utc)
{
//localTime = DateTime.SpecifyKind(localTime.Value, DateTimeKind.Local);
localTime = new DateTime(localTime.Value.Year, localTime.Value.Month, localTime.Value.Day,
localTime.Value.Hour, localTime.Value.Minute, localTime.Value.Second,
localTime.Value.Millisecond, DateTimeKind.Local);
}
DateTime? utcTime = localTime.Value.ToUniversalTime();
prop.SetValue(entity, utcTime, null);
}
}
}
}
public IQueryable<T> GetDbSet()
{
return databaseOptions.GetDbContext().Set<T>().AsNoTracking();
}
}
}
@@ -0,0 +1,16 @@
using Church.Net.DAL.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Church.Net.DAL.EFCoreDBF.Core
{
public class DatabaseOptions
{
public string ConnectionString { get; set; }
public ChurchNetContext GetDbContext() => new ChurchNetContext(ConnectionString);
}
}