Files
BeWoPlaner/BeWo/ViewModel/AbstractDCMapperVM.cs

140 lines
3.2 KiB
C#
Raw Permalink Normal View History

2025-11-24 12:20:26 +01:00
using DevExpress.Xpf.Accordion;
using System;
2016-06-27 01:45:38 +02:00
using System.Collections.Generic;
namespace BeWo.ViewModel
{
public abstract class AbstractDCMapperVM<DCType> : AbstractBaseVM where DCType : new()
{
2026-01-27 16:58:04 +01:00
protected readonly List<string> _DirtyProps = new List<string>();
2016-06-27 01:45:38 +02:00
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) { }
2016-06-27 01:45:38 +02:00
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;
}
2016-06-27 01:45:38 +02:00
}
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);
// Methode dient dem aktualiseren eines existierendes VM mit einem neuen DC (z.B. Serverupdate)
2025-05-26 11:36:48 +02:00
public virtual void UpdateByDataContract(DCType pDataContract)
2025-05-13 11:10:24 +02:00
{
throw new NotImplementedException();
}
2016-06-27 01:45:38 +02:00
protected void StoreDirtyInformation(bool pDirty, string pPropName)
{
if (_DirtyProps.Contains(pPropName) && !pDirty)
{
_DirtyProps.Remove(pPropName);
}
else if (!_DirtyProps.Contains(pPropName) && pDirty)
{
_DirtyProps.Add(pPropName);
}
}
2026-04-08 11:55:09 +02:00
protected bool SetProperty<T>(ref T field, T value, string propertyName, params string[] morePropertyNames)
=> SetProperty(ref field, value, propertyName, null, morePropertyNames);
protected bool SetProperty<T>(ref T field, T value, string propertyName, Func<T> originalValueProvider = null, params string[] morePropertyNames)
{
var success = SetProperty<T>(ref field, value, propertyName, originalValueProvider);
if (success)
{
foreach (var morePropertyName in morePropertyNames)
{
FirePropertyChanged(morePropertyName);
}
}
return success;
}
2025-07-07 11:52:34 +02:00
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);
}
}
2016-06-27 01:45:38 +02:00
}
}