Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

c# - AutoMapper: Mapping between a IDataReader and DTO object

I have a DataReader which contains the result of a stored procedure. The naming convention for the columns use underscores for spaces.

I have been able to successfully map between IDataReader and IEnumerable, but only if the fields match exactly. I do not want to the naming convention used in the stored procedures to dictate the way I name fields in my objects. And the same is true on the database side. I do not think I would be successful enforcing Pascal Case upon the DBAs.

I would like to avoid having to use ForMember() foreach field I need to map. That would defeat the purpose of using AutoMapper.

I found a previous post on the topic which I have used as a reference in my tests. I have not been able to get the correct configuration/mapping for the test to pass successfully. I am hoping someone can assist.

public class DataReaderTests
{
    private DTOObject _result;
    private IDataReader _dataReader;

    protected override void Establish_context()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
            cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
            cfg.CreateMap<IDataReader, IEnumerable<DTOObject>>();
        });

        _dataReader = new DataBuilder().BuildDataReader();
        _result = Mapper.Map<IDataReader, IEnumerable<DTOObject>>(_dataReader).FirstOrDefault();
    }

    [Test]
    public void Then_a_column_containing_phone_number_should_be_read()
    {
        Assert.That(_result.PhoneNumber, Is.EqualTo(_dataReader[FieldName.PhoneNumber]));
    }
}

public class DataBuilder
{
    public IDataReader BuildDataReader()
    {
        var resultData = new DataTable();    
        resultData.Columns.Add(FieldName.PhoneNumber, typeof(string));

        var resultDataRow = resultData.NewRow();
        resultDataRow[FieldName.PhoneNumber] = "111-222-3333";

        resultData.Rows.Add(resultDataRow);

        return resultData.CreateDataReader();
    }
}

internal class FieldName
{
    public const String Id = "id";
    public const String Name = "name";
    public const String PhoneNumber = "phone_number";
    public const String CreateDate = "create_date";   
}

public class DTOObject
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string PhoneNumber { get; set; }
    public DateTime CreatedDate { get; set; }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We have written custom attributes to achieve this. We do mapping-assignment with the help of reflection and here's some sample code for you.

The attribute that is applied to Business object proerties for coloumn-mapping.

/// <summary>
    /// Holds mapping information between business objects properties and database table fields.
    /// </summary>
    [global::System.AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public sealed class DataFieldMappingAttribute : Attribute
    {
        /// <summary>
        /// Initializes a new instance of the DataFieldMappingAttribute class.
        /// </summary>
        /// <param name="fieldName">Name of the Field in Database Table that the business object properties maps to.</param>
        public DataFieldMappingAttribute(string fieldName)
        {
            this.MappedField = fieldName;
        }

        /// <summary>
        /// Gets or Sets the mapped Database Table Field.
        /// </summary>
        public string MappedField
        {
            get;
            private set;
        }
    }

A sample business object would look like this in my application.

User.cs

[TableMapping("Users")]
public class User : EntityBase
{
    #region Constructor(s)
    public AppUser()
    {
        BookCollection = new BookCollection();
    }
    #endregion

    #region Properties

    #region Default Properties - Direct Field Mapping using DataFieldMappingAttribute

    private System.Int32 _UserId;

    private System.String _FirstName;
    private System.String _LastName;
    private System.String _UserName;
    private System.Boolean _IsActive;

    [DataFieldMapping("UserID")]
    [DataObjectFieldAttribute(true, true, false)]
    [NotNullOrEmpty(Message = "UserID From Users Table Is Required.")]
    public override int Id
    {
        get
        {
            return _UserId;
        }
        set
        {
            _UserId = value;
        }
    }

    [DataFieldMapping("UserName")]
    [Searchable]
    [NotNullOrEmpty(Message = "Username Is Required.")]
    public string UserName
    {
        get
        {
            return _UserName;
        }
        set
        {
            _UserName = value;
        }
    }

    [DataFieldMapping("FirstName")]
    [Searchable]
    public string FirstName
    {
        get
        {
            return _FirstName;
        }
        set
        {
            _FirstName = value;
        }
    }

    [DataFieldMapping("LastName")]
    [Searchable]
    public string LastName
    {
        get
        {
            return _LastName;
        }
        set
        {
            _LastName = value;
        }
    }

    [DataFieldMapping("IsActive")]
    public bool IsActive
    {
        get
        {
            return _IsActive;
        }
        set
        {
            _IsActive = value;
        }
    }

    #region One-To-Many Mappings
    public BookCollection Books { get; set; }

    #endregion

    #region Derived Properties
    public string FullName { get { return this.FirstName + " " + this.LastName; } }

    #endregion

    #endregion

    public override bool Validate()
    {
        bool baseValid = base.Validate();
        bool localValid = Books.Validate();
        return baseValid && localValid;
    }
}

BookCollection.cs

/// <summary>
/// The BookCollection class is designed to work with lists of instances of Book.
/// </summary>
public class BookCollection : EntityCollectionBase<Book>
{
    /// <summary>
    /// Initializes a new instance of the BookCollection class.
    /// </summary>
    public BookCollection()
    {
    }

    /// <summary>
    /// Initializes a new instance of the BookCollection class.
    /// </summary>
    public BookCollection (IList<Book> initialList)
        : base(initialList)
    {
    }
}

Here's the DataRow to BusinessObject transacformation method which is wrapped around the call of an Extension method.

    /// <summary>
    /// Transforms DataRow into business object.
    /// </summary>
    /// <typeparam name="TEntity">A type that inherits EntityBase.</typeparam>
    /// <typeparam name="TDataRow">A type that inherits DataRow.</typeparam>
    /// <param name="dataRow">DataRow object which is transformed from business object.</param>
    /// <param name="entity">business object which is transformed into DataRow object.</param>
    public static void TransformDataRowToEntity<TEntity, TDataRow>(ref TDataRow dataRow, ref TEntity entity)
        where TDataRow : DataRow
        where TEntity : EntityBase
    {
        IQueryable<DataField> entityFields = entity.GetDataFields();

        foreach (var entityField in entityFields)
        {
            if (dataRow[entityField.DataFieldMapping.MappedField] is System.DBNull)
            {
                entityField.Property.SetValue(entity, null, null);
            }
            else
            {
                if (entityField.Property.GetType().IsEnum)
                {
                    Type enumType = entityField.Property.GetType();
                    EnumConverter enumConverter = new EnumConverter(enumType);
                    object enumValue = enumConverter.ConvertFrom(dataRow[entityField.DataFieldMapping.MappedField]);
                    entityField.Property.SetValue(entity, enumValue, null);
                }
                else
                {
                    entityField.Property.SetValue(entity, dataRow[entityField.DataFieldMapping.MappedField], null);
                }
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...