Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial versions of base and frammework files.

  • Loading branch information...
commit 35732a2e2ea54190a42526e7e1543b2487591045 1 parent e99c8b5
@miseeger authored
View
26 Base/Contracts/IDataObject.cs
@@ -0,0 +1,26 @@
+/* ************************************************************************* *
+ * MashedVVM *
+ * *
+ * Created with SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/) *
+ * By : Michael Seeger (www.codedriven.net) *
+ * Date: 13.05.2012 *
+ * *
+ * This code is licensed under the Creative Commons Attribution 3.0 License *
+ * (http://creativecommons.org/licenses/by/3.0/de/). *
+ * ************************************************************************* */
+
+using System.Collections.Generic;
+using System.ComponentModel;
+using MashedVVM.Base.Enum;
+
+namespace MashedVVM.Base.Contracts
+{
+
+ public interface IDataObject : IEditableObject, IDataErrorInfo
+ {
+ IEnumerable<string> ObjectStatusIgnoringProperties { get; set; }
+ DataObjectStatus ObjectStatus { get; set; }
+ void Validate();
+ }
+
+}
View
20 Base/Contracts/IView.cs
@@ -0,0 +1,20 @@
+/* ************************************************************************* *
+ * MashedVVM *
+ * *
+ * Created with SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/) *
+ * By : Michael Seeger (www.codedriven.net) *
+ * Date: 15.05.2012 *
+ * *
+ * This code is licensed under the Creative Commons Attribution 3.0 License *
+ * (http://creativecommons.org/licenses/by/3.0/de/). *
+ * ************************************************************************* */
+
+namespace MashedVVM.Base.Contracts
+{
+
+ public interface IView
+ {
+ IViewModel ViewModel { get; set; }
+ }
+
+}
View
24 Base/Contracts/IViewModel.cs
@@ -0,0 +1,24 @@
+/* ************************************************************************* *
+ * MashedVVM *
+ * *
+ * Created with SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/) *
+ * By : Michael Seeger (www.codedriven.net) *
+ * Date: 15.05.2012 *
+ * *
+ * This code is licensed under the Creative Commons Attribution 3.0 License *
+ * (http://creativecommons.org/licenses/by/3.0/de/). *
+ * ************************************************************************* */
+
+using System.ComponentModel;
+
+namespace MashedVVM.Base.Contracts
+{
+
+ public interface IViewModel : INotifyPropertyChanged
+ {
+ IView View { get; set; }
+ bool IsBusy { get; set; }
+ bool InDesign { get; }
+ void Initialize();
+ }
+}
View
130 Base/DataObject.cs
@@ -0,0 +1,130 @@
+/* ************************************************************************* *
+ * MashedVVM *
+ * *
+ * Created with SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/) *
+ * By : Michael Seeger (www.codedriven.net) *
+ * Date: 15.05.2012 *
+ * *
+ * This code is licensed under the Creative Commons Attribution 3.0 License *
+ * (http://creativecommons.org/licenses/by/3.0/de/). *
+ * ************************************************************************* */
+
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using MashedVVM.Base.Contracts;
+using MashedVVM.Base.Enum;
+
+namespace MashedVVM.Base
+{
+
+ public abstract class DataObject: NotifyableObject, IDataObject // , IEditableObject, IDataErrorInfo
+ {
+
+ // ----- ctor ---------------------------------------------------------
+
+ protected DataObject()
+ : this(DataObjectStatus.Original)
+ {
+
+ }
+
+
+ protected DataObject(DataObjectStatus objectStatus)
+ {
+ ObjectStatus = objectStatus;
+ ObjectStatusIgnoringProperties = new List<string>() { "IsBusy", "IsDirty", "HasErrors" };
+ PropertyChanged += (o, e) =>
+ {
+ if (ObjectStatus == DataObjectStatus.Original
+ && (!e.PropertyName.Equals(ExtractPropertyName(() => ObjectStatus))
+ || ObjectStatusIgnoringProperties.Contains(e.PropertyName)))
+ {
+ ObjectStatus = DataObjectStatus.Modified;
+ }
+ };
+
+ }
+
+
+ // ----- implementation of IDataObject --------------------------------
+
+ public IEnumerable<string> ObjectStatusIgnoringProperties { get; set; }
+
+
+ private DataObjectStatus _objectStatus = DataObjectStatus.Original;
+ public DataObjectStatus ObjectStatus
+ {
+ get { return _objectStatus; }
+ set
+ {
+ if (_objectStatus != value)
+ {
+ _objectStatus = value;
+ RaisePropertyChanged(() => ObjectStatus);
+ }
+ }
+ }
+
+
+ public virtual void Validate()
+ {
+
+ }
+
+
+ // ----- implementaion of IEditableObject -----------------------------
+
+ private Dictionary<string, object> _memento;
+
+ public void BeginEdit()
+ {
+ _memento = new Dictionary<string, object>();
+ PropertyInfo[] properties = this.GetType().GetProperties();
+ foreach (PropertyInfo pi in properties)
+ {
+ // ignore R/O properties!
+ if (pi.CanWrite)
+ {
+ _memento.Add(pi.Name, pi.GetValue(this, null));
+ }
+ }
+ }
+
+
+ public void EndEdit()
+ {
+ _memento = null;
+ }
+
+
+ public void CancelEdit()
+ {
+ PropertyInfo[] properties = this.GetType().GetProperties();
+ foreach (PropertyInfo pi in properties)
+ {
+ if (pi.CanWrite)
+ {
+ pi.SetValue(this, _memento[pi.Name], null);
+ }
+ }
+ EndEdit();
+ }
+
+
+ // ----- implementaion of IDataErrorInfo ------------------------------
+
+ public virtual string Error
+ {
+ get { return string.Empty; }
+ }
+
+
+ public virtual string this[string columnName]
+ {
+ get { return string.Empty; }
+ }
+
+ }
+
+}
View
20 Base/Enum/DataObjectStatus.cs
@@ -0,0 +1,20 @@
+/* ************************************************************************* *
+ * MashedVVM *
+ * *
+ * Created with SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/) *
+ * By : Michael Seeger (www.codedriven.net) *
+ * Date: 15.05.2012 *
+ * *
+ * This code is licensed under the Creative Commons Attribution 3.0 License *
+ * (http://creativecommons.org/licenses/by/3.0/de/). *
+ * ************************************************************************* */
+
+namespace MashedVVM.Base.Enum
+{
+
+ public enum DataObjectStatus
+ {
+ Original, Modified, Added, Deleted
+ }
+
+}
View
86 Base/NotifyableObject.cs
@@ -0,0 +1,86 @@
+/* ************************************************************************* *
+ * MashedVVM *
+ * *
+ * Created with SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/) *
+ * By : Michael Seeger (www.codedriven.net) *
+ * Date: 15.05.2012 *
+ * *
+ * This code is licensed under the Creative Commons Attribution 3.0 License *
+ * (http://creativecommons.org/licenses/by/3.0/de/). *
+ * ************************************************************************* */
+
+using System;
+using System.ComponentModel;
+using System.Linq.Expressions;
+using System.Reflection;
+
+namespace MashedVVM.Base
+{
+ public class NotifyableObject : INotifyPropertyChanged
+ {
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+
+ protected string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
+ {
+ if (propertyExpression == null)
+ {
+ throw new ArgumentNullException("propertyExpression");
+ }
+
+ var memberExpression = propertyExpression.Body as MemberExpression;
+ if (memberExpression == null)
+ {
+ throw new ArgumentException("The expression is not a member access expression.", "propertyExpression");
+ }
+
+ var property = memberExpression.Member as PropertyInfo;
+ if (property == null)
+ {
+ throw new ArgumentException("The member access expression does not access a property.", "propertyExpression");
+ }
+
+ var getMethod = property.GetGetMethod(true);
+ if (getMethod == null)
+ {
+ throw new ArgumentException("The referenced property does not have a get method.", "propertyExpression");
+ }
+
+ if (getMethod.IsStatic)
+ {
+ throw new ArgumentException("The referenced property is a static property.", "propertyExpression");
+ }
+
+ return memberExpression.Member.Name;
+ }
+
+
+ protected virtual void RaisePropertyChanged(string propertyName)
+ {
+ var handler = PropertyChanged;
+ if (handler != null)
+ {
+ handler(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+
+
+ protected void RaisePropertyChanged(params string[] propertyNames)
+ {
+ foreach (var name in propertyNames)
+ {
+ RaisePropertyChanged(name);
+ }
+ }
+
+
+ protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
+ {
+ var propertyName = ExtractPropertyName(propertyExpression);
+ RaisePropertyChanged(propertyName);
+ }
+
+ }
+
+}
View
81 Framework/EntityViewModelBase.cs
@@ -0,0 +1,81 @@
+/* ************************************************************************* *
+ * MashedVVM *
+ * *
+ * Created with SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/) *
+ * By : Michael Seeger (www.codedriven.net) *
+ * Date: 15.05.2012 *
+ * *
+ * This code is licensed under the Creative Commons Attribution 3.0 License *
+ * (http://creativecommons.org/licenses/by/3.0/de/). *
+ * ************************************************************************* */
+
+using System;
+using System.ComponentModel;
+using System.Windows;
+using MashedVVM.Base.Contracts;
+using DataObject = MashedVVM.Base.DataObject;
+
+namespace MashedVVM.Framework
+{
+
+ public abstract class EntityViewModelBase : DataObject, IViewModel
+ {
+
+ private IView _view;
+ public IView View {
+ get { return _view; }
+ set
+ {
+ _view = value;
+ _view.ViewModel = this;
+ }
+ }
+
+
+ private bool _isBusy;
+ public bool IsBusy
+ {
+ get { return _isBusy; }
+ set
+ {
+ if (_isBusy != value)
+ {
+ _isBusy = value;
+ RaisePropertyChanged(() => IsBusy);
+ }
+ }
+ }
+
+
+ public Boolean InDesign { get; private set; }
+
+
+ protected EntityViewModelBase(IView view)
+ {
+
+ if (view != null)
+ {
+ View = view;
+ }
+
+ InDesign = (bool)DependencyPropertyDescriptor
+ .FromProperty(DesignerProperties.IsInDesignModeProperty,
+ typeof(FrameworkElement)).Metadata.DefaultValue;
+
+ Initialize();
+ }
+
+
+ public virtual void Initialize()
+ {
+
+ }
+
+
+ public override string ToString()
+ {
+ return string.Format("EntityViewModel {0}", GetType().Name);
+ }
+
+ }
+}
View
34 Framework/ViewBase.cs
@@ -0,0 +1,34 @@
+/* ************************************************************************* *
+ * MashedVVM *
+ * *
+ * Created with SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/) *
+ * By : Michael Seeger (www.codedriven.net) *
+ * Date: 15.05.2012 *
+ * *
+ * This code is licensed under the Creative Commons Attribution 3.0 License *
+ * (http://creativecommons.org/licenses/by/3.0/de/). *
+ * ************************************************************************* */
+
+using System.Windows.Controls;
+using MashedVVM.Base.Contracts;
+
+namespace MashedVVM.Framework
+{
+
+ public abstract class ViewBase: UserControl, IView
+ {
+
+ public virtual IViewModel ViewModel
+ {
+ get
+ {
+ return (IViewModel)DataContext;
+ }
+ set
+ {
+ DataContext = value;
+ }
+ }
+
+ }
+}
View
79 Framework/ViewModelBase.cs
@@ -0,0 +1,79 @@
+/* ************************************************************************* *
+ * MashedVVM *
+ * *
+ * Created with SharpDevelop (http://www.icsharpcode.net/OpenSource/SD/) *
+ * By : Michael Seeger (www.codedriven.net) *
+ * Date: 15.05.2012 *
+ * *
+ * This code is licensed under the Creative Commons Attribution 3.0 License *
+ * (http://creativecommons.org/licenses/by/3.0/de/). *
+ * ************************************************************************* */
+
+using System;
+using System.ComponentModel;
+using System.Windows;
+using MashedVVM.Base;
+using MashedVVM.Base.Contracts;
+
+namespace MashedVVM.Framework
+{
+
+ public abstract class ViewModelBase : NotifyableObject, IViewModel
+ {
+
+ private IView _view;
+ public IView View {
+ get { return _view; }
+ set
+ {
+ _view = value;
+ _view.ViewModel = this;
+ }
+ }
+
+
+ private bool _isBusy;
+ public bool IsBusy
+ {
+ get { return _isBusy; }
+ set
+ {
+ if (_isBusy != value)
+ {
+ _isBusy = value;
+ RaisePropertyChanged(() => IsBusy);
+ }
+ }
+ }
+
+
+ public Boolean InDesign { get; private set; }
+
+
+ protected ViewModelBase(IView view)
+ {
+ if (view != null)
+ {
+ View = view;
+ }
+
+ InDesign = (bool)DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
+ typeof(FrameworkElement)).Metadata.DefaultValue;
+
+ Initialize();
+ }
+
+
+ public virtual void Initialize()
+ {
+
+ }
+
+
+ public override string ToString()
+ {
+ return string.Format("ViewModel {0}", GetType().Name);
+ }
+
+ }
+}
View
18 MashedVVM.sln
@@ -0,0 +1,18 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+# SharpDevelop 4.2.0.8783
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MashedVVM", "MashedVVM.csproj", "{EDE7F6B3-0476-49C5-892C-CD2071A264D7}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|x86 = Debug|x86
+ Release|x86 = Release|x86
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {EDE7F6B3-0476-49C5-892C-CD2071A264D7}.Debug|x86.Build.0 = Debug|x86
+ {EDE7F6B3-0476-49C5-892C-CD2071A264D7}.Debug|x86.ActiveCfg = Debug|x86
+ {EDE7F6B3-0476-49C5-892C-CD2071A264D7}.Release|x86.Build.0 = Release|x86
+ {EDE7F6B3-0476-49C5-892C-CD2071A264D7}.Release|x86.ActiveCfg = Release|x86
+ EndGlobalSection
+EndGlobal
Please sign in to comment.
Something went wrong with that request. Please try again.