Skip to content

Commit

Permalink
Initial versions of base and frammework files.
Browse files Browse the repository at this point in the history
  • Loading branch information
miseeger committed May 15, 2012
1 parent e99c8b5 commit 35732a2
Show file tree
Hide file tree
Showing 10 changed files with 518 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 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();
}

}
20 changes: 20 additions & 0 deletions 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; }
}

}
24 changes: 24 additions & 0 deletions 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();
}
}
130 changes: 130 additions & 0 deletions 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; }
}

}

}
20 changes: 20 additions & 0 deletions 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
}

}
86 changes: 86 additions & 0 deletions 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);
}

}

}
81 changes: 81 additions & 0 deletions 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);
}

}
}

0 comments on commit 35732a2

Please sign in to comment.