Files
BeWoPlaner/BeWo/ViewModel/AbstractDCMapperVM.cs
Rene Evertz 2832bb9c5e Merge branch 'master' into feature_rene_18_ai - kein kommentar...
# Conflicts:
#	BeWo/BeWo.csproj
#	BeWo/BeWoApp.xaml.cs
#	BeWo/ServiceProxy/GeneratedAiEnhancedService.cs
#	BeWo/Services/BeWoControlFactory.cs
#	BeWo/Services/BeWoWindowFactory.cs
#	BeWo/Services/BeWoWindowService.cs
#	BeWo/Services/IControlFactory.cs
#	BeWo/Services/IWindowFactory.cs
#	BeWo/Services/IWindowService.cs
#	BeWo/View/BeWoFileView.xaml
#	BeWo/View/Detail/Report/AbwesenheitsView.xaml
#	BeWo/View/Windows/AnimatedBeWoWindow.xaml.cs
#	BeWo/app.config
#	BeWoAllReports.sln
#	BeWoPlanerMobil.sln
#	BeWoPlanerMobil/.vs/BeWoPlanerMobil.csproj.dtbcache.json
#	BeWoTests.sln
#	Dakota/DakotaUtils.cs
#	Dakota/Logic/DakotaInfoCreator.cs
#	Dakota/Models/DakotaFile.cs
#	Data/Access/GenericDAO.cs
#	Data/Access/SearchDAO.cs
#	Data/Data.csproj
#	Host/Web.config
#	Server/Components/ServerUtils/ApiFacade/WebClientFacade.cs
#	Service/BeWoServiceEnums.cs
#	Service/Core/ServiceHelper.cs
#	Service/Core/ServiceValidator.cs
#	Service/Extensions/ServiceEnumExtension.cs
#	Service/Service.csproj
#	Service/ServiceContracts/Enhanced/IAiEnhancedService.cs
#	Service/ServiceContracts/Enhanced/IOperationsEnhancedService.cs
#	Service/ServiceImplementations/EmployeeServiceImp.cs
#	Service/ServiceImplementations/Enhanced/AiEnhancedServiceImp.cs
#	Service/ServiceImplementations/Enhanced/OperationsEnhancedServiceImp.cs
#	Service/ServiceProxy/OpenWebUIFacade.cs
#	Service/ServiceProxy/ServiceFacade.cs
#	Service/ServiceUtils/DistanceCalculator/GoogleDistanceMatrixAPI.cs
#	Shared/BeWoEntityEnums.cs
#	Shared/Core/AppError.cs
#	Shared/Core/Facade/WebClientFacade.cs
#	Shared/Core/WebClientFacade.cs
#	Shared/Shared.csproj
2025-11-28 12:07:35 +01:00

121 lines
2.5 KiB
C#

using DevExpress.Xpf.Accordion;
using System;
using System.Collections.Generic;
namespace BeWo.ViewModel
{
public abstract class AbstractDCMapperVM<DCType> : AbstractBaseVM where DCType : new()
{
private readonly List<string> _DirtyProps = new List<string>();
private bool? _IsNew;
public AbstractDCMapperVM() { }
public AbstractDCMapperVM(DCType pDataContract, bool pIsNew)
{
_IsNew = pIsNew;
DataContract = pDataContract;
InitByDataContract(DataContract);
}
public AbstractDCMapperVM(DCType pDataContract, long? oid) : this(pDataContract, oid is null) { }
public override bool IsDirty
{
get { return _DirtyProps.Count > 0; }
}
public bool IsNew
{
get
{
if (_IsNew == null)
{
throw new NotImplementedException();
}
return _IsNew.Value;
}
set
{
_IsNew = value;
}
}
internal DCType DataContract { get; set; }
public DCType CommitToDataContract()
{
return MapToDataContract(DataContract, true);
}
public DCType CopyToEmptyDataContract()
{
return MapToDataContract(new DCType(), false);
}
public override void SetDirty(bool newDirty)
{
if (!newDirty)
{
_DirtyProps.Clear();
}
}
protected abstract void InitByDataContract(DCType pDataContract);
protected abstract DCType MapToDataContract(DCType pDataContract, bool doCommit);
public virtual void UpdateByDataContract(DCType pDataContract)
{
throw new NotImplementedException();
}
protected void StoreDirtyInformation(bool pDirty, string pPropName)
{
if (_DirtyProps.Contains(pPropName) && !pDirty)
{
_DirtyProps.Remove(pPropName);
}
else if (!_DirtyProps.Contains(pPropName) && pDirty)
{
_DirtyProps.Add(pPropName);
}
}
protected new bool SetProperty<T>(ref T field, T value, string propertyName, Func<T> originalValueProvider = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return false;
field = value;
if (originalValueProvider != null)
{
var isDirty = true;
if(DataContract != null)
{
isDirty = !EqualityComparer<T>.Default.Equals(getSafe(originalValueProvider), value);
}
StoreDirtyInformation(isDirty, propertyName);
}
FirePropertyChanged(propertyName);
return true;
}
private T getSafe<T>(Func<T> originalValueProvider)
{
try
{
return originalValueProvider();
}
catch (Exception e)
{
return default(T);
}
}
}
}