226 lines
5.1 KiB
C#
226 lines
5.1 KiB
C#
using ModuleTools.BindingModels;
|
|
using ModuleTools.Enums;
|
|
using ModuleTools.Interfaces;
|
|
using ModuleTools.Models;
|
|
using ModuleTools.ViewModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace ModuleTools.BusinessLogics
|
|
{
|
|
/// <summary>
|
|
/// Базовый класс для логики сущности
|
|
/// </summary>
|
|
/// <typeparam name="G"></typeparam>
|
|
/// <typeparam name="S"></typeparam>
|
|
/// <typeparam name="L"></typeparam>
|
|
/// <typeparam name="E"></typeparam>
|
|
public abstract class BusinessLogicCore<G, S, L, E>
|
|
where G : GetBindingModel
|
|
where S : SetBindingModel
|
|
where L : ListViewModel<E>
|
|
where E : ElementViewModel
|
|
{
|
|
/// <summary>
|
|
/// Перечень ошибок при выполнении операции
|
|
/// </summary>
|
|
public List<(string Title, string Message)> Errors { get; protected set; }
|
|
|
|
protected IEntityService<G, S> Service { get; set; }
|
|
|
|
protected ISecurityManager Security { get; set; }
|
|
|
|
protected readonly AccessOperation _serviceOperation;
|
|
|
|
protected readonly string _entity;
|
|
|
|
public BusinessLogicCore(IEntityService<G, S> service, string entity, AccessOperation serviceOperation)
|
|
{
|
|
Service = service;
|
|
Errors = new List<(string Title, string Message)>();
|
|
Security = UnityContainerConfigurator.Resolve<ISecurityManager>();
|
|
_entity = entity;
|
|
_serviceOperation = serviceOperation;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Проверка доступности операции для пользователя
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
protected bool NoAccess(AccessBindingModel model, AccessType type)
|
|
{
|
|
if (Security.CheckAccess(new SecurityManagerCheckAccessModel(model, _serviceOperation, type, _entity)))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
Errors.Add(("Ошибка безопасности", Security.ErrorMessage));
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение списка записей
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public L GetList(G model)
|
|
{
|
|
Errors.Clear();
|
|
try
|
|
{
|
|
if (NoAccess(model, AccessType.SimpleView) && NoAccess(model, AccessType.FullView))
|
|
{
|
|
return null;
|
|
}
|
|
var result = Service.Read(model);
|
|
if (!result.IsSucceeded)
|
|
{
|
|
Errors.AddRange(Errors);
|
|
return null;
|
|
}
|
|
|
|
return ConvertToL(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Errors.Add(("Ошибка получения", ex.Message));
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение записи
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public E GetElement(G model)
|
|
{
|
|
Errors.Clear();
|
|
try
|
|
{
|
|
if (NoAccess(model, AccessType.SimpleView) && NoAccess(model, AccessType.FullView))
|
|
{
|
|
return null;
|
|
}
|
|
var result = Service.Read(model);
|
|
if (!result.IsSucceeded)
|
|
{
|
|
Errors.AddRange(Errors);
|
|
return null;
|
|
}
|
|
return ConvertToE(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Errors.Add(("Ошибка получения", ex.Message));
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Создание записи
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public E Create(S model)
|
|
{
|
|
Errors.Clear();
|
|
try
|
|
{
|
|
if (NoAccess(model, AccessType.Change))
|
|
{
|
|
return null;
|
|
}
|
|
var result = Service.Create(model);
|
|
if (!result.IsSucceeded)
|
|
{
|
|
Errors.AddRange(Errors);
|
|
return null;
|
|
}
|
|
|
|
return ConvertToE(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Errors.Add(("Ошибка создания", ex.Message));
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Изменение записи
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public E Update(S model)
|
|
{
|
|
Errors.Clear();
|
|
try
|
|
{
|
|
if (NoAccess(model, AccessType.Change))
|
|
{
|
|
return null;
|
|
}
|
|
var result = Service.Update(model);
|
|
if (!result.IsSucceeded)
|
|
{
|
|
Errors.AddRange(Errors);
|
|
return null;
|
|
}
|
|
|
|
return ConvertToE(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Errors.Add(("Ошибка изменения", ex.Message));
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление записи
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public bool Delete(G model)
|
|
{
|
|
Errors.Clear();
|
|
try
|
|
{
|
|
if (NoAccess(model, AccessType.Delete))
|
|
{
|
|
return false;
|
|
}
|
|
var result = Service.Delete(model);
|
|
if (!result.IsSucceeded)
|
|
{
|
|
Errors.AddRange(Errors);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Errors.Add(("Ошибка удаления", ex.Message));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получить список элементов
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
protected abstract L ConvertToL(OperationResultModel model);
|
|
|
|
/// <summary>
|
|
/// Получить элемент
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
protected abstract E ConvertToE(OperationResultModel model);
|
|
}
|
|
} |