diff Chronosv2/source/Presentation/ViewModel/EntityViewModel.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Chronosv2/source/Presentation/ViewModel/EntityViewModel.cs	Tue Feb 21 17:25:44 2012 +0700
@@ -0,0 +1,135 @@
+/*
+The MIT License
+
+Copyright (c) 2009-2010. Carlos Guzmán Álvarez. http://chronoswpf.codeplex.com/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+using System;
+using System.ComponentModel;
+using Chronos.Presentation.Core.ViewModel;
+
+namespace Chronos.Presentation.ViewModel
+{
+    /// <summary>
+    /// Base class for entity viewmodel implementations
+    /// </summary>
+    /// <typeparam name="TEntity"></typeparam>
+    public abstract class EntityViewModel<TEntity>
+        : ViewModelBase, IEntityViewModel<TEntity> where TEntity : class, IDataErrorInfo, new()
+    {
+        #region · Fields ·
+
+        private TEntity entity;
+
+        #endregion
+
+        #region · IEntityViewModel<TEntity> Properties ·
+
+        /// <summary>
+        /// Gets the entity model instance
+        /// </summary>
+        public TEntity Entity
+        {
+            get { return this.entity; }
+        }
+
+        /// <summary>
+        /// Gets value indicating whether this instance is valid
+        /// </summary>
+        public virtual bool IsValid
+        {
+            get { return String.IsNullOrEmpty(this.Error); }
+        }
+
+        /// <summary>
+        /// Gets value indicating whether this instance has changes
+        /// </summary>
+        public virtual bool HasChanges
+        {
+            get { return false; }
+        }
+
+        /// <summary>
+        /// Gets an error message indicating what is wrong with this object.
+        /// </summary>
+        /// <value>
+        /// An error message indicating what is wrong with this object. The default is
+        /// an empty string ("").
+        /// </value>
+        public virtual string Error
+        {
+            get
+            {
+                if (this.Entity != null)
+                {
+                    return this.Entity.Error;
+                }
+
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// Gets the error message for the property with the given name.
+        /// </summary>
+        /// <param name="columnName">The name of the property whose error message to get.</param>
+        /// <value>The error message for the property. The default is an empty string ("").</value>
+        public virtual string this[string columnName]
+        {
+            get
+            {
+                if (this.Entity != null)
+                {
+                    return this.Entity[columnName];
+                }
+
+                return null;
+            }
+        }
+
+        #endregion
+
+        #region · Constructors ·
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="EntityViewModel"/> class
+        /// </summary>
+        protected EntityViewModel()
+            : base()
+        {
+            this.entity = new TEntity();
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="EntityViewModel"/> class
+        /// with the given entity model
+        /// </summary>
+        /// <param name="entity"></param>
+        protected EntityViewModel(TEntity entity)
+            : this()
+        {
+            this.entity = entity;
+        }
+
+        #endregion
+    }
+}
+