226 lines
5.2 KiB
C#
226 lines
5.2 KiB
C#
using ToolsModule.BindingModels;
|
||
using ToolsModule.Enums;
|
||
using ToolsModule.Interfaces;
|
||
using ToolsModule.ViewModels;
|
||
using System;
|
||
|
||
namespace ToolsModule.BusinessLogics
|
||
{
|
||
/// <summary>
|
||
/// Базовый класс для логики сущности
|
||
/// </summary>
|
||
/// <typeparam name="G"></typeparam>
|
||
/// <typeparam name="S"></typeparam>
|
||
/// <typeparam name="L"></typeparam>
|
||
/// <typeparam name="E"></typeparam>
|
||
public class GenericBusinessLogic<G, S, L, E> : CoreBusinessLogic, IGenericEntityLogic<G, S, L, E>
|
||
where G : GetBindingModel
|
||
where S : SetBindingModel
|
||
where L : ListViewModel<E>
|
||
where E : ElementViewModel
|
||
{
|
||
/// <summary>
|
||
/// Сервис с хранилищем данных
|
||
/// </summary>
|
||
protected IGenericEntityService<G, S> Service { get; set; }
|
||
|
||
/// <summary>
|
||
/// Возможен ли просмотр без авторизации
|
||
/// </summary>
|
||
protected bool _allowSimpleView = true;
|
||
|
||
public GenericBusinessLogic(IGenericEntityService<G, S> service, string entity, AccessOperation serviceOperation)
|
||
{
|
||
Service = service;
|
||
_entity = entity;
|
||
_serviceOperation = serviceOperation;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение списка записей
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public L GetList(G model)
|
||
{
|
||
Errors.Clear();
|
||
try
|
||
{
|
||
model.HaveRight = !NoAccess(model, AccessType.View);
|
||
if (model.HaveRight && !_allowSimpleView)
|
||
{
|
||
throw new MethodAccessException("Нет прав на получение списка");
|
||
}
|
||
var result = Service.Read(model);
|
||
if (!result.IsSucceeded)
|
||
{
|
||
Errors.AddRange(result.Errors);
|
||
return null;
|
||
}
|
||
|
||
return result.Result as L;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Errors.Add(("Ошибка получения", ex.Message));
|
||
Errors.Add(("Ошибка получения (Stack)", ex.StackTrace));
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение записи
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public E GetElement(G model)
|
||
{
|
||
Errors.Clear();
|
||
try
|
||
{
|
||
model.HaveRight = !NoAccess(model, AccessType.View);
|
||
if (model.HaveRight && !_allowSimpleView)
|
||
{
|
||
throw new MethodAccessException("Нет прав на получение списка");
|
||
}
|
||
var result = Service.Read(model);
|
||
if (!result.IsSucceeded)
|
||
{
|
||
Errors.AddRange(result.Errors);
|
||
return null;
|
||
}
|
||
return result.Result as E;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Errors.Add(("Ошибка получения", ex.Message));
|
||
Errors.Add(("Ошибка получения (Stack)", ex.StackTrace));
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Создание записи
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public virtual E Create(S model)
|
||
{
|
||
Errors.Clear();
|
||
try
|
||
{
|
||
if (NoAccess(model, AccessType.Change))
|
||
{
|
||
return null;
|
||
}
|
||
var result = Service.Create(model);
|
||
if (!result.IsSucceeded)
|
||
{
|
||
Errors.AddRange(result.Errors);
|
||
return null;
|
||
}
|
||
|
||
return result.Result as E;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Errors.Add(("Ошибка создания", ex.Message));
|
||
Errors.Add(("Ошибка создания (Stack)", ex.StackTrace));
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Изменение записи
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public virtual E Update(S model)
|
||
{
|
||
Errors.Clear();
|
||
try
|
||
{
|
||
if (NoAccess(model, AccessType.Change))
|
||
{
|
||
return null;
|
||
}
|
||
var result = Service.Update(model);
|
||
if (!result.IsSucceeded)
|
||
{
|
||
Errors.AddRange(result.Errors);
|
||
return null;
|
||
}
|
||
|
||
return result.Result as E;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Errors.Add(("Ошибка изменения", ex.Message));
|
||
Errors.Add(("Ошибка изменения (Stack)", ex.StackTrace));
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление записи
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public virtual bool Delete(G model)
|
||
{
|
||
Errors.Clear();
|
||
try
|
||
{
|
||
if (NoAccess(model, AccessType.Delete))
|
||
{
|
||
return false;
|
||
}
|
||
var result = Service.Delete(model);
|
||
if (!result.IsSucceeded)
|
||
{
|
||
Errors.AddRange(result.Errors);
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Errors.Add(("Ошибка удаления", ex.Message));
|
||
Errors.Add(("Ошибка удаления (Stack)", ex.StackTrace));
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Восстанолвение записи
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <returns></returns>
|
||
public virtual E Restore(G model)
|
||
{
|
||
Errors.Clear();
|
||
try
|
||
{
|
||
if (NoAccess(model, AccessType.Change))
|
||
{
|
||
return null;
|
||
}
|
||
var result = Service.Restore(model);
|
||
if (!result.IsSucceeded)
|
||
{
|
||
Errors.AddRange(result.Errors);
|
||
return null;
|
||
}
|
||
|
||
return result.Result as E;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Errors.Add(("Ошибка восстановления", ex.Message));
|
||
Errors.Add(("Ошибка восстановления (Stack)", ex.StackTrace));
|
||
}
|
||
return null;
|
||
}
|
||
}
|
||
} |