init
This commit is contained in:
25
Common/DTS.CommonCore/Base/Classes/BasePropertyChanged.cs
Normal file
25
Common/DTS.CommonCore/Base/Classes/BasePropertyChanged.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
// ReSharper disable CheckNamespace
|
||||
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public abstract class BasePropertyChanged : IBasePropertyChanged
|
||||
{
|
||||
public virtual event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
public bool SetProperty<T>(ref T storage, T value, String propertyName = null)
|
||||
{
|
||||
if (Equals(storage, value)) return false;
|
||||
|
||||
storage = value;
|
||||
OnPropertyChanged(propertyName);
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void OnPropertyChanged(string propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace DTS.Common.Base.Classes
|
||||
{
|
||||
/// <summary>
|
||||
/// this class overloads the description attribute to allow us to use an enum
|
||||
/// to set the description and to lookup the description in the string resources
|
||||
/// to be translated the property should have a string resource in the form of PropertyName_Description
|
||||
/// otherwise ##DescriptionNotFound## will be returned as the translated string
|
||||
/// </summary>
|
||||
public class DescriptionResourceAttribute : DescriptionAttribute
|
||||
{
|
||||
private string _resourceId;
|
||||
public DescriptionResourceAttribute(string resourceId)
|
||||
{
|
||||
_resourceId = resourceId;
|
||||
}
|
||||
public override string Description => Strings.Strings.ResourceManager.GetString(_resourceId) ?? @"##DescriptionNotFound##" + _resourceId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace DTS.Common.Base.Classes
|
||||
{
|
||||
/// <summary>
|
||||
/// this class overloads the description attribute to allow us to use an enum
|
||||
/// to set the description and to lookup the description in the string resources
|
||||
/// to be translated the property should have a string resource in the form of PropertyName_Description
|
||||
/// otherwise ##DescriptionNotFound## will be returned as the translated string
|
||||
/// </summary>
|
||||
public class DisplayResourceAttribute : DisplayNameAttribute
|
||||
{
|
||||
private string _resourceId;
|
||||
public DisplayResourceAttribute(string resourceId)
|
||||
{
|
||||
_resourceId = resourceId;
|
||||
}
|
||||
public override string DisplayName => Strings.Strings.ResourceManager.GetString(_resourceId) ?? @"##ResourceNotFound##" + _resourceId;
|
||||
}
|
||||
}
|
||||
642
Common/DTS.CommonCore/Base/Classes/DynamicTypeDescriptor.cs
Normal file
642
Common/DTS.CommonCore/Base/Classes/DynamicTypeDescriptor.cs
Normal file
@@ -0,0 +1,642 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing.Design;
|
||||
|
||||
namespace DTS.Common.Base.Classes
|
||||
{
|
||||
/// <summary>
|
||||
/// https://stackoverflow.com/questions/16422844/propertygrid-browsable-not-found-for-entity-framework-created-property-how-to-f
|
||||
/// </summary>
|
||||
public sealed class DynamicTypeDescriptor : ICustomTypeDescriptor, INotifyPropertyChanged
|
||||
{
|
||||
private Type _type;
|
||||
private AttributeCollection _attributes;
|
||||
private TypeConverter _typeConverter;
|
||||
private Dictionary<Type, object> _editors;
|
||||
private EventDescriptor _defaultEvent;
|
||||
private PropertyDescriptor _defaultProperty;
|
||||
private EventDescriptorCollection _events;
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
private DynamicTypeDescriptor()
|
||||
{
|
||||
}
|
||||
|
||||
public DynamicTypeDescriptor(Type type)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
|
||||
_type = type;
|
||||
_typeConverter = TypeDescriptor.GetConverter(type);
|
||||
_defaultEvent = TypeDescriptor.GetDefaultEvent(type);
|
||||
_defaultProperty = TypeDescriptor.GetDefaultProperty(type);
|
||||
_events = TypeDescriptor.GetEvents(type);
|
||||
|
||||
List<PropertyDescriptor> normalProperties = new List<PropertyDescriptor>();
|
||||
OriginalProperties = TypeDescriptor.GetProperties(type);
|
||||
foreach (PropertyDescriptor property in OriginalProperties)
|
||||
{
|
||||
if (!property.IsBrowsable)
|
||||
continue;
|
||||
|
||||
normalProperties.Add(property);
|
||||
|
||||
}
|
||||
Properties = new PropertyDescriptorCollection(normalProperties.ToArray());
|
||||
|
||||
_attributes = TypeDescriptor.GetAttributes(type);
|
||||
|
||||
_editors = new Dictionary<Type, object>();
|
||||
object editor = TypeDescriptor.GetEditor(type, typeof(UITypeEditor));
|
||||
if (editor != null)
|
||||
{
|
||||
_editors.Add(typeof(UITypeEditor), editor);
|
||||
}
|
||||
editor = TypeDescriptor.GetEditor(type, typeof(ComponentEditor));
|
||||
if (editor != null)
|
||||
{
|
||||
_editors.Add(typeof(ComponentEditor), editor);
|
||||
}
|
||||
editor = TypeDescriptor.GetEditor(type, typeof(InstanceCreationEditor));
|
||||
if (editor != null)
|
||||
{
|
||||
_editors.Add(typeof(InstanceCreationEditor), editor);
|
||||
}
|
||||
}
|
||||
|
||||
public T GetPropertyValue<T>(string name, T defaultValue)
|
||||
{
|
||||
if (name == null)
|
||||
throw new ArgumentNullException("name");
|
||||
|
||||
foreach (PropertyDescriptor pd in Properties)
|
||||
{
|
||||
if (pd.Name == name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return (T)Convert.ChangeType(pd.GetValue(Component), typeof(T));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public void SetPropertyValue(string name, object value)
|
||||
{
|
||||
if (name == null)
|
||||
throw new ArgumentNullException("name");
|
||||
|
||||
foreach (PropertyDescriptor pd in Properties)
|
||||
{
|
||||
if (pd.Name == name)
|
||||
{
|
||||
pd.SetValue(Component, value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void OnValueChanged(PropertyDescriptor prop)
|
||||
{
|
||||
PropertyChangedEventHandler handler = PropertyChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, new PropertyChangedEventArgs(prop.Name));
|
||||
}
|
||||
}
|
||||
|
||||
internal static T GetAttribute<T>(AttributeCollection attributes) where T : Attribute
|
||||
{
|
||||
if (attributes == null)
|
||||
return null;
|
||||
|
||||
foreach (Attribute att in attributes)
|
||||
{
|
||||
if (typeof(T).IsAssignableFrom(att.GetType()))
|
||||
return (T)att;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public sealed class DynamicProperty : PropertyDescriptor, INotifyPropertyChanged
|
||||
{
|
||||
private readonly Type _type;
|
||||
private readonly bool _hasDefaultValue;
|
||||
private readonly object _defaultValue;
|
||||
private readonly PropertyDescriptor _existing;
|
||||
private readonly DynamicTypeDescriptor _descriptor;
|
||||
private Dictionary<Type, object> _editors;
|
||||
private bool? _readOnly;
|
||||
private bool? _browsable;
|
||||
private string _displayName;
|
||||
private string _description;
|
||||
private string _category;
|
||||
private List<Attribute> _attributes = new List<Attribute>();
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
internal DynamicProperty(DynamicTypeDescriptor descriptor, Type type, object value, string name, Attribute[] attrs)
|
||||
: base(name, attrs)
|
||||
{
|
||||
_descriptor = descriptor;
|
||||
_type = type;
|
||||
Value = value;
|
||||
DefaultValueAttribute def = DynamicTypeDescriptor.GetAttribute<DefaultValueAttribute>(Attributes);
|
||||
if (def == null)
|
||||
{
|
||||
_hasDefaultValue = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
_hasDefaultValue = true;
|
||||
_defaultValue = def.Value;
|
||||
}
|
||||
if (attrs != null)
|
||||
{
|
||||
foreach (Attribute att in attrs)
|
||||
{
|
||||
_attributes.Add(att);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static Attribute[] GetAttributes(PropertyDescriptor existing)
|
||||
{
|
||||
List<Attribute> atts = new List<Attribute>();
|
||||
foreach (Attribute a in existing.Attributes)
|
||||
{
|
||||
atts.Add(a);
|
||||
}
|
||||
return atts.ToArray();
|
||||
}
|
||||
|
||||
internal DynamicProperty(DynamicTypeDescriptor descriptor, PropertyDescriptor existing, object component)
|
||||
: this(descriptor, existing.PropertyType, existing.GetValue(component), existing.Name, GetAttributes(existing))
|
||||
{
|
||||
_existing = existing;
|
||||
}
|
||||
|
||||
public void RemoveAttributesOfType<T>() where T : Attribute
|
||||
{
|
||||
List<Attribute> remove = new List<Attribute>();
|
||||
foreach (Attribute att in _attributes)
|
||||
{
|
||||
if (typeof(T).IsAssignableFrom(att.GetType()))
|
||||
{
|
||||
remove.Add(att);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Attribute att in remove)
|
||||
{
|
||||
_attributes.Remove(att);
|
||||
}
|
||||
}
|
||||
|
||||
public IList<Attribute> AttributesList
|
||||
{
|
||||
get
|
||||
{
|
||||
return _attributes;
|
||||
}
|
||||
}
|
||||
|
||||
public override AttributeCollection Attributes
|
||||
{
|
||||
get
|
||||
{
|
||||
return new AttributeCollection(_attributes.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public object Value { get; set; }
|
||||
|
||||
public override bool CanResetValue(object component)
|
||||
{
|
||||
if (_existing != null)
|
||||
return _existing.CanResetValue(component);
|
||||
|
||||
return _hasDefaultValue;
|
||||
}
|
||||
|
||||
public override Type ComponentType
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_existing != null)
|
||||
return _existing.ComponentType;
|
||||
|
||||
return typeof(object);
|
||||
}
|
||||
}
|
||||
|
||||
public override object GetValue(object component)
|
||||
{
|
||||
if (_existing != null)
|
||||
return _existing.GetValue(component);
|
||||
|
||||
return Value;
|
||||
}
|
||||
|
||||
public override string Category
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_category != null)
|
||||
return _category;
|
||||
|
||||
return base.Category;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetCategory(string category)
|
||||
{
|
||||
_category = category;
|
||||
}
|
||||
|
||||
public override string Description
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_description != null)
|
||||
return _description;
|
||||
|
||||
return base.Description;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDescription(string description)
|
||||
{
|
||||
_description = description;
|
||||
}
|
||||
|
||||
public override string DisplayName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_displayName != null)
|
||||
return _displayName;
|
||||
|
||||
if (_existing != null)
|
||||
return _existing.DisplayName;
|
||||
|
||||
return base.DisplayName;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetDisplayName(string displayName)
|
||||
{
|
||||
_displayName = displayName;
|
||||
}
|
||||
|
||||
public override bool IsBrowsable
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_browsable.HasValue)
|
||||
return _browsable.Value;
|
||||
|
||||
return base.IsBrowsable;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetBrowsable(bool browsable)
|
||||
{
|
||||
_browsable = browsable;
|
||||
}
|
||||
|
||||
public override bool IsReadOnly
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_readOnly.HasValue)
|
||||
return _readOnly.Value;
|
||||
|
||||
if (_existing != null)
|
||||
return _existing.IsReadOnly;
|
||||
|
||||
ReadOnlyAttribute att = DynamicTypeDescriptor.GetAttribute<ReadOnlyAttribute>(Attributes);
|
||||
if (att == null)
|
||||
return false;
|
||||
|
||||
return att.IsReadOnly;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetIsReadOnly(bool readOnly)
|
||||
{
|
||||
_readOnly = readOnly;
|
||||
}
|
||||
|
||||
public override Type PropertyType
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_existing != null)
|
||||
return _existing.PropertyType;
|
||||
|
||||
return _type;
|
||||
}
|
||||
}
|
||||
|
||||
public override void ResetValue(object component)
|
||||
{
|
||||
if (_existing != null)
|
||||
{
|
||||
_existing.ResetValue(component);
|
||||
PropertyChangedEventHandler handler = PropertyChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, new PropertyChangedEventArgs(Name));
|
||||
}
|
||||
_descriptor.OnValueChanged(this);
|
||||
return;
|
||||
}
|
||||
|
||||
if (CanResetValue(component))
|
||||
{
|
||||
Value = _defaultValue;
|
||||
_descriptor.OnValueChanged(this);
|
||||
}
|
||||
}
|
||||
|
||||
public override void SetValue(object component, object value)
|
||||
{
|
||||
if (_existing != null)
|
||||
{
|
||||
_existing.SetValue(component, value);
|
||||
PropertyChangedEventHandler handler = PropertyChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
handler(this, new PropertyChangedEventArgs(Name));
|
||||
}
|
||||
_descriptor.OnValueChanged(this);
|
||||
return;
|
||||
}
|
||||
|
||||
Value = value;
|
||||
_descriptor.OnValueChanged(this);
|
||||
}
|
||||
|
||||
public override bool ShouldSerializeValue(object component)
|
||||
{
|
||||
if (_existing != null)
|
||||
return _existing.ShouldSerializeValue(component);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override object GetEditor(Type editorBaseType)
|
||||
{
|
||||
if (editorBaseType == null)
|
||||
throw new ArgumentNullException("editorBaseType");
|
||||
|
||||
if (_editors != null)
|
||||
{
|
||||
object type;
|
||||
if ((_editors.TryGetValue(editorBaseType, out type)) && (type != null))
|
||||
return type;
|
||||
}
|
||||
return base.GetEditor(editorBaseType);
|
||||
}
|
||||
|
||||
public void SetEditor(Type editorBaseType, object obj)
|
||||
{
|
||||
if (editorBaseType == null)
|
||||
throw new ArgumentNullException("editorBaseType");
|
||||
|
||||
if (_editors == null)
|
||||
{
|
||||
if (obj == null)
|
||||
return;
|
||||
|
||||
_editors = new Dictionary<Type, object>();
|
||||
}
|
||||
if (obj == null)
|
||||
{
|
||||
_editors.Remove(editorBaseType);
|
||||
}
|
||||
else
|
||||
{
|
||||
_editors[editorBaseType] = obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public PropertyDescriptor AddProperty(Type type, string name, object value, string displayName, string description, string category, bool hasDefaultValue, object defaultValue, bool readOnly)
|
||||
{
|
||||
return AddProperty(type, name, value, displayName, description, category, hasDefaultValue, defaultValue, readOnly, null);
|
||||
}
|
||||
|
||||
public PropertyDescriptor AddProperty(
|
||||
Type type,
|
||||
string name,
|
||||
object value,
|
||||
string displayName,
|
||||
string description,
|
||||
string category,
|
||||
bool hasDefaultValue,
|
||||
object defaultValue,
|
||||
bool readOnly,
|
||||
Type uiTypeEditor)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
|
||||
if (name == null)
|
||||
throw new ArgumentNullException("name");
|
||||
|
||||
List<Attribute> atts = new List<Attribute>();
|
||||
if (!string.IsNullOrEmpty(displayName))
|
||||
{
|
||||
atts.Add(new DisplayNameAttribute(displayName));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(description))
|
||||
{
|
||||
atts.Add(new DescriptionAttribute(description));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(category))
|
||||
{
|
||||
atts.Add(new CategoryAttribute(category));
|
||||
}
|
||||
|
||||
if (hasDefaultValue)
|
||||
{
|
||||
atts.Add(new DefaultValueAttribute(defaultValue));
|
||||
}
|
||||
|
||||
if (uiTypeEditor != null)
|
||||
{
|
||||
atts.Add(new EditorAttribute(uiTypeEditor, typeof(UITypeEditor)));
|
||||
}
|
||||
|
||||
if (readOnly)
|
||||
{
|
||||
atts.Add(new ReadOnlyAttribute(true));
|
||||
}
|
||||
|
||||
DynamicProperty property = new DynamicProperty(this, type, value, name, atts.ToArray());
|
||||
AddProperty(property);
|
||||
return property;
|
||||
}
|
||||
|
||||
public void RemoveProperty(string name)
|
||||
{
|
||||
if (name == null)
|
||||
throw new ArgumentNullException("name");
|
||||
|
||||
List<PropertyDescriptor> remove = new List<PropertyDescriptor>();
|
||||
foreach (PropertyDescriptor pd in Properties)
|
||||
{
|
||||
if (pd.Name == name)
|
||||
{
|
||||
remove.Add(pd);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (PropertyDescriptor pd in remove)
|
||||
{
|
||||
Properties.Remove(pd);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddProperty(PropertyDescriptor property)
|
||||
{
|
||||
if (property == null)
|
||||
throw new ArgumentNullException("property");
|
||||
|
||||
Properties.Add(property);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return base.ToString() + " (" + Component + ")";
|
||||
}
|
||||
|
||||
public PropertyDescriptorCollection OriginalProperties { get; private set; }
|
||||
public PropertyDescriptorCollection Properties { get; private set; }
|
||||
|
||||
public DynamicTypeDescriptor FromComponent(object component)
|
||||
{
|
||||
if (component == null)
|
||||
throw new ArgumentNullException("component");
|
||||
|
||||
if (!_type.IsAssignableFrom(component.GetType()))
|
||||
throw new ArgumentException(null, "component");
|
||||
|
||||
DynamicTypeDescriptor desc = new DynamicTypeDescriptor();
|
||||
desc._type = _type;
|
||||
desc.Component = component;
|
||||
|
||||
// shallow copy on purpose
|
||||
desc._typeConverter = _typeConverter;
|
||||
desc._editors = _editors;
|
||||
desc._defaultEvent = _defaultEvent;
|
||||
desc._defaultProperty = _defaultProperty;
|
||||
desc._attributes = _attributes;
|
||||
desc._events = _events;
|
||||
desc.OriginalProperties = OriginalProperties;
|
||||
|
||||
// track values
|
||||
List<PropertyDescriptor> properties = new List<PropertyDescriptor>();
|
||||
foreach (PropertyDescriptor pd in Properties)
|
||||
{
|
||||
DynamicProperty ap = new DynamicProperty(desc, pd, component);
|
||||
properties.Add(ap);
|
||||
}
|
||||
|
||||
desc.Properties = new PropertyDescriptorCollection(properties.ToArray());
|
||||
return desc;
|
||||
}
|
||||
|
||||
public object Component { get; private set; }
|
||||
public string ClassName { get; set; }
|
||||
public string ComponentName { get; set; }
|
||||
|
||||
AttributeCollection ICustomTypeDescriptor.GetAttributes()
|
||||
{
|
||||
return _attributes;
|
||||
}
|
||||
|
||||
string ICustomTypeDescriptor.GetClassName()
|
||||
{
|
||||
if (ClassName != null)
|
||||
return ClassName;
|
||||
|
||||
if (Component != null)
|
||||
return Component.GetType().Name;
|
||||
|
||||
if (_type != null)
|
||||
return _type.Name;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
string ICustomTypeDescriptor.GetComponentName()
|
||||
{
|
||||
if (ComponentName != null)
|
||||
return ComponentName;
|
||||
|
||||
return Component != null ? Component.ToString() : null;
|
||||
}
|
||||
|
||||
TypeConverter ICustomTypeDescriptor.GetConverter()
|
||||
{
|
||||
return _typeConverter;
|
||||
}
|
||||
|
||||
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
|
||||
{
|
||||
return _defaultEvent;
|
||||
}
|
||||
|
||||
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
|
||||
{
|
||||
return _defaultProperty;
|
||||
}
|
||||
|
||||
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
|
||||
{
|
||||
object editor;
|
||||
if (_editors.TryGetValue(editorBaseType, out editor))
|
||||
return editor;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
|
||||
{
|
||||
return _events;
|
||||
}
|
||||
|
||||
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
|
||||
{
|
||||
return _events;
|
||||
}
|
||||
|
||||
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
|
||||
{
|
||||
return Properties;
|
||||
}
|
||||
|
||||
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
|
||||
{
|
||||
return Properties;
|
||||
}
|
||||
|
||||
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
|
||||
{
|
||||
return Component;
|
||||
}
|
||||
}
|
||||
}
|
||||
5
Common/DTS.CommonCore/Base/Interface/IBaseClass.cs
Normal file
5
Common/DTS.CommonCore/Base/Interface/IBaseClass.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public interface IBaseClass : IBasePropertyChanged { }
|
||||
}
|
||||
13
Common/DTS.CommonCore/Base/Interface/IBaseModel.cs
Normal file
13
Common/DTS.CommonCore/Base/Interface/IBaseModel.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public interface IBaseModel : IBasePropertyChanged
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Gets the IsSaved status.
|
||||
/// </summary>
|
||||
bool IsSaved { get; }
|
||||
|
||||
}
|
||||
}
|
||||
11
Common/DTS.CommonCore/Base/Interface/IBasePropertyChanged.cs
Normal file
11
Common/DTS.CommonCore/Base/Interface/IBasePropertyChanged.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.ComponentModel;
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public interface IBasePropertyChanged : INotifyPropertyChanged
|
||||
{
|
||||
//void OnPropertyChanged(string propertyName = null);
|
||||
//bool SetProperty<T>(ref T storage, T value, String propertyName = null);
|
||||
void OnPropertyChanged(string propertyName);
|
||||
}
|
||||
}
|
||||
11
Common/DTS.CommonCore/Base/Interface/IBaseView.cs
Normal file
11
Common/DTS.CommonCore/Base/Interface/IBaseView.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public interface IBaseView
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the data context.
|
||||
/// </summary>
|
||||
object DataContext { get; set; }
|
||||
}
|
||||
}
|
||||
72
Common/DTS.CommonCore/Base/Interface/IBaseViewModel.cs
Normal file
72
Common/DTS.CommonCore/Base/Interface/IBaseViewModel.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public interface IBaseViewModel : IBasePropertyChanged
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets, sets the IsMenuIncluded status.
|
||||
/// </summary>
|
||||
bool IsMenuIncluded { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets, sets the IsNavigationIncluded status.
|
||||
/// </summary>
|
||||
bool IsNavigationIncluded { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the IsBusy status.
|
||||
/// </summary>
|
||||
bool IsBusy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The Activated method.
|
||||
/// </summary>
|
||||
void Activated();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the IsDirty status.
|
||||
/// </summary>
|
||||
bool IsDirty { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The Cleanup method.
|
||||
/// </summary>
|
||||
void Cleanup();
|
||||
|
||||
/// <summary>
|
||||
/// Starts the cleanup process asynchronously.
|
||||
/// </summary>
|
||||
Task CleanupAsync();
|
||||
|
||||
/// <summary>
|
||||
/// The Initialize method.
|
||||
/// </summary>
|
||||
void Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// The Initialize method.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to initialize the viewmodel.</param>
|
||||
void Initialize(object parameter);
|
||||
|
||||
/// <summary>
|
||||
/// The Initialize method.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to initialize the viewmodel.</param>
|
||||
/// <param name="model">Another parameter to initialize the viewmodel.</param>
|
||||
void Initialize(object parameter, object model);
|
||||
|
||||
/// <summary>
|
||||
/// Starts the initialization process asynchronously.
|
||||
/// </summary>
|
||||
Task InitializeAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Starts the initialization process asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to initialize the viewmodel.</param>
|
||||
Task InitializeAsync(object parameter);
|
||||
}
|
||||
}
|
||||
11
Common/DTS.CommonCore/Base/Interface/IBaseWindow.cs
Normal file
11
Common/DTS.CommonCore/Base/Interface/IBaseWindow.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public interface IBaseWindow
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the data context.
|
||||
/// </summary>
|
||||
object DataContext { get; set; }
|
||||
}
|
||||
}
|
||||
66
Common/DTS.CommonCore/Base/Interface/IBaseWindowModel.cs
Normal file
66
Common/DTS.CommonCore/Base/Interface/IBaseWindowModel.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System.ComponentModel;
|
||||
using System.Threading.Tasks;
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
|
||||
public interface IBaseWindowModel : INotifyPropertyChanged
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets, sets the IsMenuIncluded status.
|
||||
/// </summary>
|
||||
bool IsMenuIncluded { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets, sets the IsNavigationIncluded status.
|
||||
/// </summary>
|
||||
bool IsNavigationIncluded { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the IsBusy status.
|
||||
/// </summary>
|
||||
bool IsBusy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The Activated method.
|
||||
/// </summary>
|
||||
void Activated();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the IsDirty status.
|
||||
/// </summary>
|
||||
bool IsDirty { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The Cleanup method.
|
||||
/// </summary>
|
||||
void Cleanup();
|
||||
|
||||
/// <summary>
|
||||
/// Starts the cleanup process asynchronously.
|
||||
/// </summary>
|
||||
Task CleanupAsync();
|
||||
|
||||
/// <summary>
|
||||
/// The Initialize method.
|
||||
/// </summary>
|
||||
void Initialize();
|
||||
|
||||
/// <summary>
|
||||
/// The Initialize method.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to initialize the viewmodel.</param>
|
||||
void Initialize(object parameter);
|
||||
|
||||
/// <summary>
|
||||
/// Starts the initialization process asynchronously.
|
||||
/// </summary>
|
||||
Task InitializeAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Starts the initialization process asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to initialize the viewmodel.</param>
|
||||
Task InitializeAsync(object parameter);
|
||||
}
|
||||
}
|
||||
15
Common/DTS.CommonCore/Base/Interface/IHeaderInfoProvider.cs
Normal file
15
Common/DTS.CommonCore/Base/Interface/IHeaderInfoProvider.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides an easy way to recognize a class that exposes a HeaderInfo that can be used to bind to a header from XAML.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The HeaderInfo type.</typeparam>
|
||||
public interface IHeaderInfoProvider<T>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the header info of type T.
|
||||
/// </summary>
|
||||
T HeaderInfo { get; }
|
||||
}
|
||||
}
|
||||
10
Common/DTS.CommonCore/Base/Interface/IViewModel.cs
Normal file
10
Common/DTS.CommonCore/Base/Interface/IViewModel.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public interface IViewModel
|
||||
{
|
||||
// Summary:
|
||||
// Gets or sets the Model property of the viewmodel object.
|
||||
object Model { get; set; }
|
||||
}
|
||||
}
|
||||
33
Common/DTS.CommonCore/Base/Model/BaseModel.cs
Normal file
33
Common/DTS.CommonCore/Base/Model/BaseModel.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
// ReSharper disable CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class used to create Model objects
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">Type of the Model object.</typeparam>
|
||||
public abstract class BaseModel<TModel> : BasePropertyChanged, IBaseModel
|
||||
where TModel : class
|
||||
{
|
||||
//DependencyObject,
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Model object.
|
||||
/// </summary>
|
||||
public TModel Model { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create new instance of base class.
|
||||
/// </summary>
|
||||
// ReSharper disable PublicConstructorInAbstractClass
|
||||
// ReSharper disable once EmptyConstructor
|
||||
public BaseModel()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ReSharper disable once UnusedAutoPropertyAccessor.Local
|
||||
public bool IsSaved { get; private set; }
|
||||
}
|
||||
}
|
||||
49
Common/DTS.CommonCore/Base/View/BaseView.cs
Normal file
49
Common/DTS.CommonCore/Base/View/BaseView.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Windows.Controls;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the base class for views that define the appearance of data in a UserControl control.
|
||||
/// </summary>
|
||||
///
|
||||
public class BaseView : UserControl, IBaseView
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the bound object data data has been changed (View is dirty).
|
||||
/// </summary>
|
||||
public bool IsDirty
|
||||
{
|
||||
get
|
||||
{
|
||||
if (DataContext != null)
|
||||
{
|
||||
if (DataContext is IBaseViewModel baseViewModel)
|
||||
{
|
||||
return baseViewModel.IsDirty;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a header info which uses by the TabControl to display the TabItem's header.
|
||||
/// </summary>
|
||||
public string HeaderInfo
|
||||
{
|
||||
get
|
||||
{
|
||||
if (DataContext != null)
|
||||
{
|
||||
if (DataContext is IHeaderInfoProvider<string> headerInfoProvider)
|
||||
{
|
||||
return headerInfoProvider.HeaderInfo;
|
||||
}
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
41
Common/DTS.CommonCore/Base/View/BaseWindow.cs
Normal file
41
Common/DTS.CommonCore/Base/View/BaseWindow.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Windows;
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public class BaseWindow : Window, IBaseWindow
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the bound object data data has been changed (View is dirty).
|
||||
/// </summary>
|
||||
public bool IsDirty
|
||||
{
|
||||
get
|
||||
{
|
||||
if (DataContext != null)
|
||||
{
|
||||
if (DataContext is IBaseWindowModel baseWindowModel)
|
||||
return baseWindowModel.IsDirty;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a header info which uses by the TabControl to display the TabItem's header.
|
||||
/// </summary>
|
||||
public string HeaderInfo
|
||||
{
|
||||
get
|
||||
{
|
||||
if (DataContext != null)
|
||||
{
|
||||
if (DataContext is IHeaderInfoProvider<string> iHeaderInfoProvider)
|
||||
return iHeaderInfoProvider.HeaderInfo;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
154
Common/DTS.CommonCore/Base/ViewModel/BaseViewModel.cs
Normal file
154
Common/DTS.CommonCore/Base/ViewModel/BaseViewModel.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using Microsoft.Practices.Prism.Events;
|
||||
using Microsoft.Practices.Prism.Commands;
|
||||
using Microsoft.Practices.Prism.Regions;
|
||||
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
|
||||
using Microsoft.Practices.Unity;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Threading;
|
||||
using DTS.Common.Events;
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
public abstract class BaseViewModel<TModel> : BasePropertyChanged, IBaseViewModel
|
||||
where TModel : class
|
||||
{
|
||||
|
||||
protected IEventAggregator Aggregator { get; private set; }
|
||||
protected IUnityContainer Container { get; private set; }
|
||||
protected IRegionManager _regionManager { get; private set; }
|
||||
public TModel Model { get; set; }
|
||||
|
||||
protected BaseViewModel()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected BaseViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer unityContainer)
|
||||
{
|
||||
Aggregator = eventAggregator;
|
||||
Container = unityContainer;
|
||||
_regionManager = regionManager;
|
||||
CreateCommands();
|
||||
}
|
||||
|
||||
|
||||
protected virtual void CreateCommands()
|
||||
{
|
||||
ConfirmationRequest = new InteractionRequest<Confirmation>();
|
||||
CloseCommand = new DelegateCommand<object>(CloseMethod);
|
||||
}
|
||||
|
||||
#region Commands
|
||||
|
||||
/// <summary>
|
||||
/// The interaction request to display the confirmation.
|
||||
/// </summary>
|
||||
public InteractionRequest<Confirmation> ConfirmationRequest { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The delegate command to close object.
|
||||
/// </summary>
|
||||
public DelegateCommand<object> CloseCommand { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// This is an execute method for the <see cref="CloseCommand">CloseCommand</see>.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to pass to the <see cref="CloseCommand">CloseCommand</see>.</param>
|
||||
protected virtual void CloseMethod(object parameter)
|
||||
{
|
||||
CleanupAtClose();
|
||||
}
|
||||
|
||||
private void CleanupAtClose()
|
||||
{
|
||||
Cleanup();
|
||||
}
|
||||
|
||||
#endregion Commands
|
||||
|
||||
#region Bases Methods
|
||||
|
||||
/// <summary>
|
||||
/// Executes after the viewmodel activated.
|
||||
/// </summary>
|
||||
public virtual void Activated()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
|
||||
/// </summary>
|
||||
public virtual void Initialize()
|
||||
{
|
||||
Aggregator.GetEvent<ShowStatus>()
|
||||
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to be used initialize viewmodel.</param>
|
||||
public virtual void Initialize(object parameter)
|
||||
{
|
||||
Aggregator.GetEvent<ShowStatus>()
|
||||
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
|
||||
}
|
||||
|
||||
public virtual void Initialize(object parameter, object model)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
|
||||
/// </summary>
|
||||
public virtual async Task InitializeAsync()
|
||||
{
|
||||
await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
|
||||
{
|
||||
Aggregator.GetEvent<ShowStatus>()
|
||||
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Publishes the <see cref="ShowStatus">ShowStatus</see> during the viewmodel initialization.
|
||||
/// </summary>
|
||||
/// <param name="parameter">The parameter to be used initialize viewmodel.</param>
|
||||
public virtual async Task InitializeAsync(object parameter)
|
||||
{
|
||||
await Dispatcher.CurrentDispatcher.InvokeAsync(() =>
|
||||
{
|
||||
Aggregator.GetEvent<ShowStatus>()
|
||||
.Publish(new StatusInfo(StatusInfo.StatusState.Busy, Strings.Strings.Loading));
|
||||
});
|
||||
}
|
||||
|
||||
#endregion Bases Methods
|
||||
|
||||
public new event PropertyChangedEventHandler PropertyChanged;
|
||||
public bool IsMenuIncluded { get; set; }
|
||||
public bool IsNavigationIncluded { get; set; }
|
||||
public int Percentage { get; set; }
|
||||
public string IsBusyMessage { get; set; }
|
||||
public bool IsBusy { get; set; }
|
||||
|
||||
|
||||
public bool IsDirty { get; set; }
|
||||
/// <summary>
|
||||
/// Sets the Model to null.
|
||||
/// </summary>
|
||||
public virtual void Cleanup()
|
||||
{
|
||||
Model = default(TModel);
|
||||
}
|
||||
|
||||
public Task CleanupAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
91
Common/DTS.CommonCore/Base/ViewModel/ViewModelBase.cs
Normal file
91
Common/DTS.CommonCore/Base/ViewModel/ViewModelBase.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace DTS.Common.Base
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Base class used to create ViewModel objects that implement their own commands/verbs/actions.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the Model object.</typeparam>
|
||||
public abstract class ViewModelBase<T> : DependencyObject, INotifyPropertyChanged, IViewModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the Model object.
|
||||
/// </summary>
|
||||
public object Model { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Create new instance of base class used to create ViewModel objects that implement their own commands/verbs/actions.
|
||||
/// </summary>
|
||||
public ViewModelBase()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this object is executing an asynchronous process.
|
||||
/// </summary>
|
||||
public bool IsBusy { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the Model has been changed.
|
||||
/// </summary>
|
||||
public virtual bool IsDirty { get; protected set; }
|
||||
|
||||
|
||||
// Summary:
|
||||
// Event raised when an error occurs during processing.
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public virtual event EventHandler<ErrorEventArgs> ErrorOccurred;
|
||||
|
||||
/// <summary>
|
||||
/// Event raised when a property changes.
|
||||
/// </summary>
|
||||
public virtual event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Override this method to implement async initialization of the model object.
|
||||
/// The result of this method is used to set the Model property of the viewmodel.
|
||||
/// </summary>
|
||||
/// <returns>A Task that creates the model object.</returns>
|
||||
[DebuggerStepThrough]
|
||||
protected abstract Task<T> InitializeAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Creates or retrieves a new instance of the Model by invoking a static factory method.
|
||||
/// </summary>
|
||||
/// <param name="factoryMethod">Static factory method function.</param>
|
||||
protected abstract void DoRefresh(Func<T> factoryMethod);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Raises ErrorOccurred event when an error occurs during processing.
|
||||
/// </summary>
|
||||
/// <param name="error"></param>
|
||||
protected abstract void OnError(Exception error);
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the Model changes, allowing event handlers to be unhooked from the old object and hooked on the new object.
|
||||
/// </summary>
|
||||
/// <param name="oldValue">Previous Model reference.</param>
|
||||
/// <param name="newValue">New Model reference.</param>
|
||||
protected abstract void OnModelChanged(T oldValue, T newValue);
|
||||
|
||||
/// <summary>
|
||||
/// Raise the PropertyChanged event.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">Name of the changed property.</param>
|
||||
protected abstract void OnPropertyChanged(string propertyName);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user