1
\$\begingroup\$

T_PROJECT is the class that will be retrieve data from linq to sql and ProjectOwnerDataFields is the class that will be used to show data in view. in edit mode i have to get value from model and send it back like this. is there anyway to refactor this and remove the reverse duplication on ProjectOwnerDataFields and UpdateToModel ?

    public class ProjectOwnerDataFields
    {
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public string ContactPerson { get; set; }
        public string CountryIsoLetter { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string Fax { get; set; }
        public string Location { get; set; }
        public string Province { get; set; }
        public string Remark { get; set; }

        public string CountryFullName
        {
            get
            {
                if (String.IsNullOrEmpty(this.CountryIsoLetter) == false)
                {
                    return new RegionInfo(this.CountryIsoLetter).EnglishName;
                }

                return "";
            }
        }

        public ProjectOwnerDataFields()
        {

        }

        public ProjectOwnerDataFields(T_PROJECT project)
        {
            this.Id                =     project.proj_owner_id            ;
            this.CompanyName       =     project.proj_owner               ;
            this.ContactPerson     =     project.proj_owner_contact       ;
            this.CountryIsoLetter  =     project.proj_owner_country       ;
            this.Phone             =     project.proj_owner_phone         ;
            this.Email             =     project.proj_owner_email         ;
            this.Fax               =     project.proj_owner_fax           ;
            this.Location          =     project.proj_owner_location      ;
            this.Province          =     project.proj_owner_province      ;
            this.Remark            =     project.proj_remark              ;
        }

        public void UpdateToModel(T_PROJECT project)
        {              
            project.proj_owner              =   this.CompanyName          ;
            project.proj_owner_contact      =   this.ContactPerson        ;
            project.proj_owner_country      =   this.CountryIsoLetter     ;
            project.proj_owner_phone        =   this.Phone                ;
            project.proj_owner_email        =   this.Email                ;
            project.proj_owner_fax          =   this.Fax                  ;
            project.proj_owner_location     =   this.Location             ;
            project.proj_owner_province     =   this.Province             ;
            project.proj_remark             =   this.Remark               ;
        }
    }
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

This is very close to what AutoMapper is for. Though it doesn't directly support bidirectional maps, so you'll have to write a simple class that builds two mappers at the same time:

public class TwoWayMapper<TSource, TDestination>
{
    private readonly IMappingExpression<TSource, TDestination> m_sourceMapping =
        Mapper.CreateMap<TSource, TDestination>();
    private readonly IMappingExpression<TDestination, TSource> m_destinationMapping =
        Mapper.CreateMap<TDestination, TSource>();

    public TwoWayMapper<TSource, TDestination> ForMember(
        Expression<Func<TSource, object>> sourceMember,
        Expression<Func<TDestination, object>> destinationMember)
    {
        m_sourceMapping.ForMember(destinationMember, opt => opt.MapFrom(sourceMember));
        m_destinationMapping.ForMember(sourceMember, opt => opt.MapFrom(destinationMember));
        return this;
    }
}

You can then set up your mapping like this (maybe in a static constructor of ProjectOwnerDataFields?):

new TwoWayMapper<ProjectOwnerDataFields, T_PROJECT>()
    .ForMember(f => f.Id, p => p.proj_owner_id)
    .ForMember(f => f.CompanyName, p => p.proj_owner)
    .ForMember(f => f.ContactPerson, p => p.proj_owner_contact)
    .ForMember(f => f.CountryIsoLetter, p => p.proj_owner_country);
    // etc.

With that, your methods simplify to:

public ProjectOwnerDataFields(T_PROJECT project)
{
    Mapper.Map(project, this);
}

public void UpdateToModel(T_PROJECT project)
{
    Mapper.Map(this, project);
}
\$\endgroup\$
0
0
\$\begingroup\$

Anyone may correct me if I'm wrong but honestly: I don't think so.

(Offtopic)

This can be rewritten:

if (String.IsNullOrEmpty(this.CountryIsoLetter) == false)
{
    return new RegionInfo(this.CountryIsoLetter).EnglishName;
}
return "";

to:

return String.IsNullOrEmpty(CountryIsoLetter) ? "" : new RegionInfo(CountryIsoLetter).EnglishName;

Also you can ommit 'this' in your code as it is not qualifying a hidden member with a same name.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ BTW, I think the second part of your question is not off topic. It's okay to review the code in question in ways that weren't asked for. \$\endgroup\$
    – svick
    Commented Jun 27, 2013 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.