Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial versions of base and frammework files.
- Loading branch information
Showing
10 changed files
with
518 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.