123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using System;
- using System.Collections.Generic;
- using Luticate2.Utils.Dbo.Basic;
- using Luticate2.Utils.Dbo.PaginatedRequest;
- using Luticate2.Utils.Dbo.Result;
- using Luticate2.Utils.Interfaces;
- using Luticate2.Utils.Utils;
-
- namespace Luticate2.Utils.Business
- {
- public abstract class LuCrudBusiness<TDataAccess, TDboCreate, TDboRead, TDboUpdate, TId> :
- LuBusiness,
- ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
- where TDataAccess : ILuCrudInterface<TDboCreate, TDboRead, TDboUpdate, TId>
- where TDboCreate : class
- where TDboRead : class
- where TDboUpdate : class
- {
- protected readonly TDataAccess DataAccess;
-
- protected readonly IServiceProvider ServiceProvider;
-
- private readonly IList<Type> _listeners = new List<Type>();
-
- protected LuCrudBusiness(TDataAccess dataAccess, IServiceProvider serviceProvider)
- {
- DataAccess = dataAccess;
- ServiceProvider = serviceProvider;
- }
-
- public void AddListener<T>()
- where T : ILuCrudNotifications
- {
- _listeners.Add(typeof(T));
- }
-
- protected virtual LuResult<TDboCreate> CheckAdd(TDboCreate obj)
- {
- return LuResult<TDboCreate>.Ok(obj);
- }
-
- protected virtual LuResult<TDboUpdate> CheckEdit(TDboRead dbo, TDboUpdate update)
- {
- return LuResult<TDboUpdate>.Ok(update);
- }
-
- protected virtual LuResult<TDboUpdate> GetAndCheckEdit(TId id, TDboUpdate update)
- {
- var res = GetSingleById(id);
- if (!res)
- {
- return res.To<TDboUpdate>();
- }
- return CheckEdit(res.Data, update);
- }
-
-
- public TId GetDboId(TDboRead obj)
- {
- return DataAccess.GetDboId(obj);
- }
-
- public virtual LuResult<T> Add<T>(TDboCreate obj, Func<TDboRead, T> returnFunc)
- {
- var res = CheckAdd(obj);
- if (!res)
- {
- return res.To<T>();
- }
- var newEntity = DataAccess.AddDbo(res.Data);
- if (newEntity)
- {
- NotifyCreate(newEntity.Data);
- return LuResult<T>.Ok(returnFunc(newEntity.Data));
- }
- return newEntity.To<T>();
- }
-
-
-
-
- public virtual LuResult<TDboRead> GetSingleById(TId id)
- {
- return DataAccess.GetSingleById(id);
- }
-
-
- public virtual LuResult<LuPaginatedDbo<TDboRead>> GetMultiple(LuPaginatedRequestDbo request)
- {
- return DataAccess.GetMultiple(request);
- }
-
-
-
-
- public virtual LuResult<T> EditSingleById<T>(TId id, TDboUpdate update, Func<TDboRead, T> returnFunc)
- {
- var originalEntity = GetSingleById(id);
- if (!originalEntity)
- {
- return originalEntity.To<T>();
- }
-
- var obj = CheckEdit(originalEntity.Data, update);
- if (!obj)
- {
- return obj.To<T>();
- }
- var editedEntity = DataAccess.EditSingleByIdDbo(id, obj.Data);
- if (editedEntity)
- {
- NotifyUpdate(originalEntity.Data, editedEntity.Data);
- return LuResult<T>.Ok(returnFunc(editedEntity.Data));
- }
- return editedEntity.To<T>();
- }
-
-
-
-
- public virtual LuResult<T> DeleteSingleById<T>(TId id, Func<TDboRead, T> returnFunc)
- {
- var originalEntity = GetSingleById(id);
- if (!originalEntity)
- {
- return originalEntity.To<T>();
- }
- var res = DataAccess.DeleteSingleById(id, returnFunc);
- if (res)
- {
- NotifyDelete(originalEntity.Data);
- }
- return res;
- }
-
- private void ForeachListener(Action<ILuCrudNotifications> action)
- {
- foreach (var listener in _listeners)
- {
- var instance = ServiceProvider.GetService(listener) as ILuCrudNotifications;
- action(instance);
- }
- }
-
- private void NotifyCreate(object newEntity)
- {
- ForeachListener(notifications => notifications.NotifyCreate(GetType(), newEntity));
- }
-
- private void NotifyUpdate(object oldEntity, object newEntity)
- {
- ForeachListener(notifications => notifications.NotifyUpdate(GetType(), oldEntity, newEntity));
- }
-
- private void NotifyDelete( object oldEntity)
- {
- ForeachListener(notifications => notifications.NotifyDelete(GetType(), oldEntity));
- }
- }
- }
|