Exemple #1
0
        /// <summary>
        ///     Gets a <see cref="bool"/> value indicating whether a given <see cref="Type"/>
        ///     has been registered with a <see cref="ColumnMappingCollection"/>.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> to check for.</typeparam>
        /// <param name="mappings">The instance of <see cref="ColumnMappingCollection"/> to check.</param>
        /// <returns>
        ///     A <see cref="bool"/> value indicating whether a given <see cref="Type"/>
        ///     has been registered with a <see cref="ColumnMappingCollection"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if the <paramref name="mappings"/> parameter is <c>null</c>.
        /// </exception>
        public static bool TypeHasBeenMapped <T>(this ColumnMappingCollection mappings)
        {
            if (mappings == null)
            {
                throw new ArgumentNullException(nameof(mappings));
            }

            return(mappings.Mappings.ContainsKey(typeof(T)));
        }
Exemple #2
0
        /// <summary>Registers a new column mapping for a specific <see cref="Type"/>.</summary>
        /// <typeparam name="T">The <see cref="Type"/> associated with the column mapping.</typeparam>
        /// <param name="mappings">The <see cref="ColumnMappingCollection"/> to register the mapping with.</param>
        /// <returns>
        ///     An instance of <see cref="MappedType{T}"/> that has been
        ///     registered with the <see cref="ColumnMappingCollection"/>
        ///     provided in the <paramref name="mappings"/> parameter.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if the <paramref name="mappings"/> parameter is <c>null</c>.
        /// </exception>
        public static MappedType <T> RegisterType <T>(this ColumnMappingCollection mappings)
        {
            if (mappings == null)
            {
                throw new ArgumentNullException(nameof(mappings));
            }

            // TODO: Consider returning the MappedType instance if it's already registered vs. throwing an exception.
            var mappedType = new MappedType <T>();

            mappings.Mappings.Add(typeof(T), mappedType);

            return(mappedType);
        }
Exemple #3
0
        /// <summary>
        ///     Gets the <see cref="MappedType{T}"/> for a specific <see cref="Type"/>
        ///     that has a mapping registered with a <see cref="ColumnMappingCollection"/>.
        /// </summary>
        /// <typeparam name="T">
        ///     The registered <see cref="Type"/> to get the column mappings for.
        /// </typeparam>
        /// <param name="mappings">
        ///     The instance of <see cref="ColumnMappingCollection"/> containing the registered instance of
        ///     <see cref="MappedType{T}"/> associated with the <see cref="Type"/> specified for <typeparamref name="T"/>.
        /// </param>
        /// <param name="throwIfTypeNotMapped">
        ///     When set to <c>true</c>, an exception will be thrown if the <see cref="Type"/> specified for
        ///     <typeparamref name="T"/> has not been registered with the <see cref="ColumnMappingCollection"/>.
        /// </param>
        /// <returns>
        ///     The <see cref="MappedType{T}"/> that has been registered with the
        ///     <see cref="ColumnMappingCollection"/> for the <see cref="Type"/>
        ///     specified for <typeparamref name="T"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if the <paramref name="mappings"/> parameter is <c>null</c>.
        /// </exception>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException">
        ///     Thrown if the <paramref name="throwIfTypeNotMapped"/> parameter is set to <c>true</c>,
        ///     and if the <see cref="Type"/> specified for <typeparamref name="T"/> has not been
        ///     registered with the <see cref="ColumnMappingCollection"/>.
        /// </exception>
        public static MappedType <T> GetTypeMapping <T>(this ColumnMappingCollection mappings, bool throwIfTypeNotMapped)
        {
            if (mappings == null)
            {
                throw new ArgumentNullException(nameof(mappings));
            }

            if (mappings.TypeHasBeenMapped <T>())
            {
                return(mappings.Mappings[typeof(T)] as MappedType <T>);
            }

            if (!throwIfTypeNotMapped)
            {
                return(null);
            }

            var message = $"The {typeof(T)} type has not been registered with the {typeof(ColumnMappingCollection).Name}.";

            throw new System.Collections.Generic.KeyNotFoundException(message);
        }
Exemple #4
0
        /// <summary>Registers a collection of mapped types with the <see cref="SqlMapper"/>.</summary>
        /// <param name="columnMappingCollection">
        ///     An instance of <see cref="ColumnMappingCollection"/> containing each of the mappings to be registered.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if the <paramref name="columnMappingCollection"/> parameter is <c>null</c>.
        /// </exception>
        public static void RegisterWithDapper(this ColumnMappingCollection columnMappingCollection)
        {
            if (columnMappingCollection == null)
            {
                throw new ArgumentNullException(nameof(columnMappingCollection));
            }

            foreach (var typeMapping in columnMappingCollection)
            {
                Func <Type, string, System.Reflection.PropertyInfo> propertySelector =
                    (type, columnName) =>
                {
                    // Fall back to the property's name if there isn't an explicit mapping
                    var mappedPropertyName = typeMapping.ColumnHasBeenMapped(columnName)
                                               ? typeMapping[columnName]
                                               : columnName;
                    return(type.GetProperty(mappedPropertyName));
                };

                Type mappedType = typeMapping.MappedType;
                var  typeMap    = new Dapper.CustomPropertyTypeMap(mappedType, propertySelector);
                Dapper.SqlMapper.SetTypeMap(mappedType, typeMap);
            }
        }
Exemple #5
0
 /// <summary>
 ///     Gets the <see cref="MappedType{T}"/> for a specific <see cref="Type"/>
 ///     that has a mapping registered with a <see cref="ColumnMappingCollection"/>.
 /// </summary>
 /// <typeparam name="T">
 ///     The registered <see cref="Type"/> to get the column mappings for.
 /// </typeparam>
 /// <param name="mappings">
 ///     The instance of <see cref="ColumnMappingCollection"/> containing the registered instance of
 ///     <see cref="MappedType{T}"/> associated with the <see cref="Type"/> specified for <typeparamref name="T"/>.
 /// </param>
 /// <returns>
 ///     If the <see cref="Type"/> specified for <typeparamref name="T"/> has
 ///     been registered with the <see cref="ColumnMappingCollection"/>, then
 ///     the registered <see cref="MappedType{T}"/>; otherwise, <c>null</c>.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if the <paramref name="mappings"/> parameter is <c>null</c>.
 /// </exception>
 public static MappedType <T> GetTypeMapping <T>(this ColumnMappingCollection mappings)
 {
     return(GetTypeMapping <T>(mappings, false));
 }