|
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Security.Permissions;
using CodeProject;
using CodeProject.ComponentModel;
namespace TaskList.DataModel
{
[Serializable]
public sealed class Task : INotifyPropertyChanged, IDataErrorInfo, ISerializable, IEditableObject
{
private string title = string.Empty;
private string description = string.Empty;
private DateTime? startDate;
private DateTime? endDate;
private bool isCompleted;
private DataErrorInfo errorInfo;
private HybridDictionary oldState;
public Task()
{
errorInfo = new DataErrorInfo(this);
errorInfo.PropertyChanged += errorInfo_PropertyChanged;
errorInfo.Update();
}
private Task(SerializationInfo info, StreamingContext context)
{
title = info.GetString("Title");
description = info.GetString("Description");
startDate = (DateTime?)info.GetValue("StartDate", typeof(DateTime?));
endDate = (DateTime?)info.GetValue("EndDate", typeof(DateTime?));
isCompleted = info.GetBoolean("IsCompleted");
errorInfo = new DataErrorInfo(this);
errorInfo.PropertyChanged += errorInfo_PropertyChanged;
errorInfo.Update();
}
public bool IsValid
{
get { return errorInfo.IsValid; }
}
[StringLengthValidator(Max=30, Min=1)]
public string Title
{
get { return title; }
set
{
Precondition.ShouldNotBeNull(value, "value");
if (title != value)
{
title = value;
OnPropertyChanged("Title");
}
}
}
public string Description
{
get { return description; }
set
{
Precondition.ShouldNotBeNull(value, "value");
if (description != value)
{
description = value;
OnPropertyChanged("Description");
}
}
}
public DateTime? StartDate
{
get { return startDate; }
set
{
if (startDate != value)
{
startDate = value;
OnPropertyChanged("StartDate");
}
}
}
public DateTime? EndDate
{
get { return endDate; }
set
{
if (endDate != value)
{
endDate = value;
OnPropertyChanged("EndDate");
}
}
}
public bool IsCompleted
{
get { return isCompleted; }
set
{
if (isCompleted != value)
{
isCompleted = value;
OnPropertyChanged("IsCompleted");
}
}
}
#region INotifyPropertyChanged Members
[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
private void errorInfo_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "IsValid")
OnPropertyChanged("IsValid");
}
#endregion
#region IDataErrorInfo Members
public string Error
{
get { return errorInfo.Error; }
}
public string this[string columnName]
{
get { return errorInfo[columnName]; }
}
#endregion
#region ISerializable Members
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Title", Title);
info.AddValue("Description", Description);
info.AddValue("StartDate", StartDate);
info.AddValue("EndDate", EndDate);
info.AddValue("IsCompleted", IsCompleted);
}
#endregion
#region IEditableObject Members
public void BeginEdit()
{
oldState = new HybridDictionary();
oldState["Title"] = Title;
oldState["Description"] = Description;
oldState["StartDate"] = StartDate;
oldState["EndDate"] = EndDate;
oldState["IsCompleted"] = IsCompleted;
}
public void CancelEdit()
{
Title = (string)oldState["Title"];
Description = (string)oldState["Description"];
StartDate = (DateTime?)oldState["StartDate"];
EndDate = (DateTime?)oldState["EndDate"];
IsCompleted = (bool)oldState["IsCompleted"];
oldState = null;
}
public void EndEdit()
{
oldState = null;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Windows developer with 10+ years experience working in the banking industry.