Click here to Skip to main content
15,893,266 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 49.1K   572   11  
This article explains how to write unit tests for MVVM using Catel.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ViewModelToModelAttribute.cs" company="Catel development team">
//   Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
//   Attribute to link a property in a view model to a model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;

namespace Catel.MVVM
{
	/// <summary>
	/// Attribute to link a property in a view model to a model.
	/// </summary>
	[AttributeUsage(AttributeTargets.Property)]
	public class ViewModelToModelAttribute : Attribute
	{
		#region Constructor & destructor
		/// <summary>
		/// Initializes a new instance of the <see cref="ViewModelToModelAttribute"/> class. This constructor assumes that the name
		/// of the property in the view model is the same as in the model.
		/// </summary>
		/// <param name="model">The property name that holds the model object.</param>
		public ViewModelToModelAttribute(string model)
			: this(model, string.Empty) { }

		/// <summary>
		/// Initializes a new instance of the <see cref="ViewModelToModelAttribute"/> class.
		/// </summary>
		/// <param name="model">The property name that holds the model object.</param>
		/// <param name="property">The property of the model object that should be linked to the <see cref="ViewModelBaseWithoutServices"/> property.</param>
		public ViewModelToModelAttribute(string model, string property)
		{
			Model = model;
			Property = property;
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets the property name that holds the model object.
        /// <para />
		/// Must be a property on the <see cref="ViewModelBaseWithoutServices"/> implementation, but is allowed to be private.
		/// </summary>
		/// <value>The model property name.</value>
		public string Model { get; internal set; }

		/// <summary>
        /// Gets the property of the model object that should be linked to the <see cref="ViewModelBaseWithoutServices"/> property.
		/// </summary>
		/// <value>The property.</value>
		public string Property { get; internal set; }

		/// <summary>
		/// Gets or sets a value indicating whether to support LLBLGen. When a property on the view model changes, the view model
		/// will try to find the related field ID instead of the related field since LLBLGen does not support nullable related entities.
		/// </summary>
		/// <value><c>true</c> if [support LLBL gen]; otherwise, <c>false</c>.</value>
		public bool SupportLLBLGen { get; set; }

		/// <summary>
		/// Gets or sets the LLBLGen entity property to use when retrieving the value from the view model entity.
        /// <para />
		/// The actual mapping will be like [ViewModel].[ViewModelLLBLGenEntityProperty] = [Model].[ModelLLBLGenEntityProperty].
        /// <para />
        /// If the value is empty, the property to map to is automatically determined by the <see cref="ViewModelBaseWithoutServices"/>.
		/// </summary>
		/// <remarks>
		/// This property is only used when <see cref="SupportLLBLGen"/> is <c>true</c>.
		/// </remarks>
		/// <value>The LLBLGen entity property.</value>
		public string ViewModelLLBLGenEntityProperty { get; set; }

		/// <summary>
		/// Gets or sets the LLBLGen entity property to use when retrieving the value from the view model entity.
        /// <para />
		/// The actual mapping will be like [ViewModel].[ViewModelLLBLGenEntityProperty] = [Model].[ModelLLBLGenEntityProperty].
        /// <para />
        /// If the value is empty, the property to map to is automatically determined by the <see cref="ViewModelBaseWithoutServices"/>.
		/// </summary>
		/// <remarks>
		/// This property is only used when <see cref="SupportLLBLGen"/> is <c>true</c>.
		/// </remarks>
		/// <value>The LLBLGen entity property.</value>
		public string ModelLLBLGenEntityProperty { get; set; }

		/// <summary>
		/// Gets or sets the value to set on the LLBLGen back-end property when the view model property is null.
		/// </summary>
		/// <value>The null value.</value>
		public object NullValue { get; set; }
		#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)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions