Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

Attribute-flavored Domain Driven Design

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
6 Apr 2008CPOL8 min read 39.1K   231   25  
A centralized business domain with a common base
using System;
using System.ComponentModel;
using System.Globalization;
using Cubox.Model;

namespace Cubox.Model
{
	internal class LocationConverter : ExpandableObjectConverter
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="context"></param>
		/// <param name="sourceType"></param>
		/// <returns></returns>
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			if (sourceType == typeof(string))
			{
				return true;
			}
			return base.CanConvertFrom (context, sourceType);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="context"></param>
		/// <param name="culture"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			if (value is string)
			{
				try
				{
					char c = char.Parse(";");
					string[] s = (value as string).Split(c);

					Location loc = new Location();
					loc.Latitude = double.Parse(s[0]);
					loc.Longitude = double.Parse(s[1]);
					return loc;
				}
				catch
				{
					throw new ArgumentException("Cannot convert from '" + (string)value + "' to type Location");
				}
			}
			return base.ConvertFrom (context, culture, value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="context"></param>
		/// <param name="culture"></param>
		/// <param name="value"></param>
		/// <param name="destinationType"></param>
		/// <returns></returns>
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType == typeof(string) && value is Location)
			{
				Location loc = value as Location;
				
				return (loc.Latitude + "; " + loc.Longitude);

			}
			return base.ConvertTo (context, culture, value, destinationType);
		}

	}


}

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 (Senior)
Netherlands Netherlands
Software engineer & architect.

Comments and Discussions