Files
BeWoPlaner/Service/BusinessLogic/GenericBusinessLogic.cs

64 lines
1.7 KiB
C#

using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.Core;
using BeWo.Service.DCEntityMapper;
using BS.Shared.DataContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeWo.Service.BusinessLogic
{
public abstract class GenericBusinessLogic<TDataContract, TEntity> where TDataContract : BeWoDataContract, new () where TEntity : BeWoEntityBase, new ()
{
public abstract IDCEntityMapper<TEntity, TDataContract> Mapper { get; }
public TDataContract Get(long oid)
{
var entity = getEntity(oid);
var dc = Mapper.MapToNewDC(entity);
return dc;
}
public virtual TDataContract Insert(TDataContract dc)
{
var entity = Mapper.MapToNewEntity(dc);
DAOFactory.GenericDAO.Insert(entity);
var dc2 = Mapper.MapToNewDC(entity);
return dc2;
}
public virtual TDataContract Update(TDataContract dc)
{
var entity = getEntity(dc.Oid.Value);
entity = Mapper.MergeWithEntity(dc, entity);
DAOFactory.GenericDAO.Update(entity);
var dc2 = Mapper.MapToNewDC(entity);
return dc2;
}
public virtual void Deactivate(TDataContract dc)
{
var entity = getEntity(dc.Oid.Value);
ServiceLogic.ConcurrencyCheck(dc.Version, entity);
DAOFactory.GenericDAO.SetActivationType(entity, BS.Shared.ActivationTypeId.Deleted);
}
public virtual bool Delete(TDataContract dc)
{
var entity = getEntity(dc.Oid.Value);
ServiceLogic.ConcurrencyCheck(dc.Version, entity);
DAOFactory.GenericDAO.Delete(entity);
return true;
}
private TEntity getEntity(long oid)
{
var entity = DAOFactory.GenericDAO.LoadByID<TEntity>(oid);
return entity;
}
}
}