Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

TDD Approach to ASP.NET WebForm Development

, 10 Feb 2010
An MVC pattern for WebForm development
MVCWebForm.zip
MVCWebFormPrototype
Lib
nunit.framework.dll
MVCWebForm
bin
Debug
MVCWebForm.dll
MVCWebForm.pdb
obj
Debug
MVCWebForm.dll
MVCWebForm.pdb
Refactor
MVCWebForm.dll
TempPE
Properties
MVCWebFormPrototype.4.5.resharper.user
UnitTest
bin
Debug
MVCWebForm.dll
MVCWebForm.pdb
nunit.framework.dll
UnitTest.dll
UnitTest.pdb
Controllers
DataSources
Model
obj
Debug
Refactor
ResolveAssemblyReference.cache
TempPE
UnitTest.dll
UnitTest.pdb
Properties
Web
App_Code
Controllers
DataSources
Model
App_Data
Bin
MVCWebForm.dll
MVCWebForm.pdb
_ReSharper.MVCWebFormPrototype
CachesImage.bin
ProjectModel
ProjectModel.dat
TodoCache
.version
9
13d15da3.dat
WebsiteFileReferences
.version
WordIndex.New
.version
3
2e9c86c8.dat
Xaml
CacheProvider.dat
using System;
using System.ComponentModel;

namespace MVCWebForm
{
    public class PresentationEntityBase: 
        IPresentationEntity,
        System.ComponentModel.INotifyPropertyChanged
    {
        private object _key;
        private bool _isNew = true;
        private bool _isDeleted;
        private bool _isDirty = true;

        protected PresentationEntityBase()
            : this(null)
        {
        }

        protected PresentationEntityBase(object key)
        {
            this._key = key;

            if (this._key == null)
                this._key = PresentationEntityBase.NewKey();
        }

        private static object NewKey()
        {
            return Guid.NewGuid();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        #endregion

        #region IEntity Members

        public object Key
        {
            get { return this._key; }

            internal set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("Entity Key", "The Key property cannot be set to null.");
                }
                this._key = value;
            }
        }

        public bool IsDirty
        {
            get { return _isDirty; }
        }

        public bool IsValid
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsDeleted
        {
            get { return _isDeleted; }
        }

        public bool IsNew
        {
            get { return _isNew; }
        }

        public bool IsSavable
        {
            get { return (IsDirty && IsValid); }
        }

        #endregion

        #region IsNew, IsDeleted, IsDirty, IsSavable

        public virtual void MarkNew()
        {
            _isNew = true;
            _isDeleted = false;
            MarkDirty();
        }

        public virtual void MarkOld()
        {
            _isNew = false;
            MarkClean();
        }

        public void MarkDeleted()
        {
            _isDeleted = true;
            MarkDirty();
        }

        public void MarkDirty()
        {
            MarkDirty(false);
        }

        protected void MarkDirty(bool suppressEvent)
        {
            _isDirty = true;
            if (!suppressEvent)
                OnUnknownPropertyChanged();
        }

        protected void PropertyHasChanged()
        {
            string propertyName =
              new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);
            PropertyHasChanged(propertyName);
        }

        protected virtual void PropertyHasChanged(string propertyName)
        {
            MarkDirty(true);
            OnPropertyChanged(propertyName);
        }

        protected void MarkClean()
        {
            _isDirty = false;
            OnUnknownPropertyChanged();
        }

       
        #endregion

        #region PropertyChanged

        protected virtual void OnIsDirtyChanged()
        {
            OnUnknownPropertyChanged();
        }

        protected virtual void OnUnknownPropertyChanged()
        {
            OnPropertyChanged(string.Empty);
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Max Civino
Software Developer (Senior) SnowValley Ltd
United Kingdom United Kingdom
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 10 Feb 2010
Article Copyright 2010 by Max Civino
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid