PageRenderTime 141ms CodeModel.GetById 131ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Otis/MemberMappingDescriptor.cs

http://otis-lib.googlecode.com/
C# | 205 lines | 133 code | 23 blank | 49 comment | 7 complexity | e5f809ddaada7cc162d6175533245b47 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Text.RegularExpressions;
  4using Otis.Parsing;
  5
  6namespace Otis
  7{
  8	/// <summary>
  9	/// Describes a transformation for one field/property within a type transformation
 10	/// </summary>
 11	public class MemberMappingDescriptor
 12	{
 13		private string m_member;
 14		private string m_expression;
 15		private string m_format;
 16		private string m_nullValue;
 17		private Type m_type;
 18		private Type m_ownerType;
 19		private bool m_isArray;
 20		private bool m_isList;
 21		private AggregateMappingDescription m_aggregateDescriptor = null;
 22		ProjectionInfo m_projections = new ProjectionInfo();
 23		List<string> m_nullableParts = new List<string>();
 24
 25		public MemberMappingDescriptor()
 26		{
 27		}
 28
 29		public MemberMappingDescriptor(MemberMappingDescriptor copyFrom)
 30		{
 31			m_member = copyFrom.m_member;
 32			m_expression = copyFrom.m_expression;
 33			m_format = copyFrom.m_format;
 34			m_nullValue = copyFrom.m_nullValue;
 35			m_type = copyFrom.m_type;
 36			m_ownerType = copyFrom.m_ownerType;
 37			m_isArray = copyFrom.m_isArray;
 38			m_isList = copyFrom.m_isList;
 39			m_projections = copyFrom.m_projections;
 40			m_nullableParts = copyFrom.m_nullableParts;
 41		}
 42
 43		/// <summary>
 44		/// Gets/sets name of the mapped field or property on the target class of transformation.
 45		/// </summary>
 46		public string Member
 47		{
 48			get { return m_member; }
 49			set { m_member = value; }
 50		}
 51
 52		/// <summary>
 53		/// Gets/sets name of the expression to which <see cref="Member"/> is mapped.
 54		/// </summary>
 55		public string Expression
 56		{
 57			get { return m_expression; }
 58			set
 59			{
 60				m_expression = ExpressionParser.NormalizeExpression(value.Trim());
 61				UpdateNullableParts(m_expression);
 62			}
 63		}
 64
 65		/// <summary>
 66		/// Gets/sets the value which will be assigned to target <see cref="Member"/> if the
 67		/// <see cref="Expression"/> is null
 68		/// </summary>
 69		public string NullValue
 70		{
 71			get { return m_nullValue; }
 72			set
 73			{
 74				if (value == null)
 75					m_nullValue = null;
 76				else
 77					m_nullValue = ExpressionParser.NormalizeExpression(value.Trim());
 78			}
 79		}
 80
 81		/// <summary>
 82		/// Gets/sets the formatting string for dtring members
 83		/// </summary>
 84		public string Format
 85		{
 86			get { return m_format; }
 87			set { m_format = value; }
 88		}
 89
 90		/// <summary>
 91		/// Gets/sets the type of the target member
 92		/// </summary>
 93		public Type Type
 94		{
 95			get { return m_type; }
 96			set { m_type = value; }
 97		}
 98
 99		/// <summary>
100		/// Gets/sets the type where the memeber is defined
101		/// </summary>
102		public Type OwnerType
103		{
104			get { return m_ownerType; }
105			set { m_ownerType = value; }
106		}
107
108		/// <summary>
109		/// Gets/sets whether the target member is an array
110		/// </summary>
111		public bool IsArray
112		{
113			get { return m_isArray; }
114			set { m_isArray = value; }
115		}
116
117		/// <summary>
118		/// Gets/sets whether the target member is an implementation of <see cref="ICollection{T}"/>
119		/// </summary>
120		public bool IsList
121		{
122			get { return m_isList; }
123			set { m_isList = value; }
124		}
125
126		/// <summary>
127		/// returns whether the mapping has the formatting string attached
128		/// </summary>
129		public bool HasFormatting
130		{
131			get { return !string.IsNullOrEmpty(m_format); }
132		}
133
134		/// <summary>
135		/// returns whether the mapped expression is a aggregate expression
136		/// </summary>
137		public bool IsAggregateExpression
138		{
139			get { return ExpressionParser.IsAggregateExpression(Expression); }
140		}
141
142		/// <summary>
143		/// Returns the descriptor for aggregate expression
144		/// </summary>
145		public AggregateMappingDescription AggregateMappingDescription
146		{
147			get
148			{
149				if (m_aggregateDescriptor == null)
150					m_aggregateDescriptor = new AggregateMappingDescription(Expression, Type);
151
152				return m_aggregateDescriptor;
153			}
154		}
155
156		/// <summary>
157		/// Returns the projections
158		/// </summary>
159		public ProjectionInfo Projections
160		{
161			get { return m_projections; }
162			set { m_projections = value; }
163		}
164																	   
165		/// <summary>
166		/// Returns the list of nullable parts in the mapped expression
167		/// </summary>
168		public List<string> NullableParts
169		{
170			get { return m_nullableParts; }
171		}
172
173		/// <summary>
174		/// returns whether there are parts in mapped expression which can have null value
175		/// </summary>
176		public bool HasNullableParts
177		{
178			get { return NullableParts.Count > 0; }
179		}
180
181		/// <summary>
182		/// returns whether the mapping has a custom value if mapped expression is null
183		/// </summary>
184		public bool HasNullValue
185		{
186			get { return NullValue != null; }
187		}
188
189
190		private void UpdateNullableParts(string expression)
191		{
192			m_nullableParts.Clear();
193			Regex regex = new Regex(@"(\$[A-Za-z]\w*(\(.*?\))?)");
194			MatchCollection matches = regex.Matches(expression);
195			if (matches.Count == 0)
196				return;
197
198			foreach (Match match in matches)
199			{
200				m_nullableParts.Add(match.Value);
201			}
202
203		}
204	}
205}