using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace DapperExtensions.Mapper { /// /// Maps an entity property to its corresponding column in the database. /// public interface IPropertyMap { string Name { get; } string ColumnName { get; } bool Ignored { get; } bool IsReadOnly { get; } KeyType KeyType { get; } PropertyInfo PropertyInfo { get; } } /// /// Maps an entity property to its corresponding column in the database. /// public class PropertyMap : IPropertyMap { public PropertyMap(PropertyInfo propertyInfo) { PropertyInfo = propertyInfo; ColumnName = PropertyInfo.Name; } /// /// Gets the name of the property by using the specified propertyInfo. /// public string Name { get { return PropertyInfo.Name; } } /// /// Gets the column name for the current property. /// public string ColumnName { get; private set; } /// /// Gets the key type for the current property. /// public KeyType KeyType { get; private set; } /// /// Gets the ignore status of the current property. If ignored, the current property will not be included in queries. /// public bool Ignored { get; private set; } /// /// Gets the read-only status of the current property. If read-only, the current property will not be included in INSERT and UPDATE queries. /// public bool IsReadOnly { get; private set; } /// /// Gets the property info for the current property. /// public PropertyInfo PropertyInfo { get; private set; } /// /// Fluently sets the column name for the property. /// /// The column name as it exists in the database. public PropertyMap Column(string columnName) { ColumnName = columnName; return this; } /// /// Fluently sets the key type of the property. /// /// The column name as it exists in the database. public PropertyMap Key(KeyType keyType) { if (Ignored) { throw new ArgumentException(string.Format("'{0}' is ignored and cannot be made a key field. ", Name)); } if (IsReadOnly) { throw new ArgumentException(string.Format("'{0}' is readonly and cannot be made a key field. ", Name)); } KeyType = keyType; return this; } /// /// Fluently sets the ignore status of the property. /// public PropertyMap Ignore() { if (KeyType != KeyType.NotAKey) { throw new ArgumentException(string.Format("'{0}' is a key field and cannot be ignored.", Name)); } Ignored = true; return this; } /// /// Fluently sets the read-only status of the property. /// public PropertyMap ReadOnly() { if (KeyType != KeyType.NotAKey) { throw new ArgumentException(string.Format("'{0}' is a key field and cannot be marked readonly.", Name)); } IsReadOnly = true; return this; } } /// /// Used by ClassMapper to determine which entity property represents the key. /// public enum KeyType { /// /// The property is not a key and is not automatically managed. /// NotAKey, /// /// The property is an integery-based identity generated from the database. /// Identity, /// /// The property is a Guid identity which is automatically managed. /// Guid, /// /// The property is a key that is not automatically managed. /// Assigned } }