Looks for an attribute (decoration) to specify the name of a source property that works with auto-mapper

I have my DTOs that look like this:

  public class SomeDTO
{
    public string last_name{ get; set; }
    public string account_number { get; set; }
}

      

My property name has underscores because I have to match it to source property names. It works great.

but I am looking for some attribute that will help me name my DTOs correctly. So I could have something like.

   public class SomeDTO
{
    [Something("last_name")]   
    public string LastName{ get; set; }
    [Something("account_number")]   
    public string AccountNumber { get; set; }
}

      

"Column" doesn't work because I think it only works with EF Only.

This is the general auto-matching code. which maps the data reader to a DTO.

        public T ExecuteQuerySingle<T>(List<SqlParameter> paramList, string commandString)
    {
        using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString))

        using (var cmd = new SqlCommand(commandString, conn))
        {

                cmd.CommandType = CommandType.StoredProcedure;
                foreach (var param in paramList)
                {
                    cmd.Parameters.Add(param);
                }
                conn.Open();
                var reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    T item;
                    Mapper.CreateMap<IDataReader, T>();
                    while (reader.Read())
                    {
                        item = Mapper.Map<IDataReader, T>(reader);
                        return item;
                    }
                    conn.Close();
                    return default(T);
                }
                conn.Close();
                return default(T);

        }
    }

      

+3


source to share


1 answer


you could implement INamingConvention

to convert the name to usable and you could implement logic to read custom attributes and fill fields manually



0


source







All Articles