1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using System;
- using System.Linq.Expressions;
- using Luticate2.Utils.Dbo;
- using Microsoft.EntityFrameworkCore;
-
- namespace Luticate2.Utils.DataAccess
- {
- public abstract class LuEfCrudDataAccess<TModel, TDboCreate, TDboRead, TDboUpdate, TDbContext> : LuEfDataAccess<TModel, TDbContext>
- where TModel : class
- where TDboCreate : class
- where TDboRead : class
- where TDboUpdate : class
- where TDbContext : DbContext
- {
- protected LuEfCrudDataAccess(TDbContext db, DbSet<TModel> table) : base(db, table)
- {
- }
-
- protected abstract TModel GetModelFromTCreate(TDboCreate obj);
-
- protected abstract TModel GetModelFromTUpdate(TDboUpdate obj);
-
- protected abstract TDboRead GetDboFromModel(TModel model);
-
- public LuResult<T> Add<T>(TDboCreate obj, Expression<Func<TModel, T>> returnFunc)
- {
- return Execute(() =>
- {
- var model = GetModelFromTCreate(obj);
- Table.Add(model);
- Db.SaveChanges();
- var res = returnFunc.Compile().DynamicInvoke(model);
- return LuResult<T>.Ok((T) res);
- });
- }
-
- public LuResult<string> AddId(TDboCreate obj)
- {
- var param = Expression.Parameter(typeof(TModel), "x");
- var exp = Expression.Property(param, "id");
- var lambda = Expression.Lambda<Func<TModel, string>>(exp, param);
- return Add(obj, lambda);
- }
-
- public LuResult<TDboRead> AddDbo(TDboCreate obj)
- {
- return Add(obj, model => GetDboFromModel(model));
- }
- }
- }
|