toothful
toothful

Reputation: 892

Convert one object to another in C#

I am new in C#, dotnet core. I am getting data from database like below. This data is very flat. I need to convert to another object.

public class Lead
    {
        public long Id { get; set; }

        public short LeadCreatorId { get; set; }

        public short LeadOwnerId { get; set; }

        public string ProductId { get; set; }

        public LeadPriority Priority { get; set; }

        public LeadStatus Status { get; set; }

        public long LeadCustomerId { get; set; }  

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string EmailAddress { get; set; }

        public string MobileNo { get; set; }

        public DateTime CreatedOn { get; set; }
    }

I need to convert it to below object.

public class LeadDto
    {
        public long Id { get; set; }

        public short LeadCreatorId { get; set; }

        public short LeadOwnerId { get; set; }

        public string ProductId { get; set; }

        public LeadPriority Priority { get; set; }

        public LeadStatus Status { get; set; }

        public LeadCustomer LeadCustomer { get; set; }

        public DateTime CreatedOn { get; set; }
    }

Where LeadCustomer is like below -

public class LeadCustomer{

            public long Id { get; set; }

            public string FirstName { get; set; }

            public string LastName { get; set; }

            public string EmailAddress { get; set; }

            public string MobileNo { get; set; }
}

How can I easily do that? Can I use dto for the conversion.

Upvotes: 3

Views: 8246

Answers (4)

ttugates
ttugates

Reputation: 6291

Since LeadDto and LeadCustomer depend on Lead:

One option would be to add a constructor to LeadDto and LeadCustomer that take the Lead object as a param and map to the properties.

Another option would be to create a static extension method.

public static LeadDto ToLeadDto(this Lead lead)
{
   return new LeadDto(){
      this.Id = lead.Id;
      // Etc..
   }
}

Then you can use like

LeadDto myObj = someLead.ToLeadDto();

And yet another option would be to use a library such as AutoMapper. I have never used it, and is overkill for your stated need, IMO.

Upvotes: 3

toothful
toothful

Reputation: 892

As @G_S suggested I have used automapper like below -

        CreateMap<Lead, LeadDto>()
            .ForMember(desc => desc.LeadCustomer, opt => opt.ResolveUsing(src =>
            {
                return new LeadCustomer()
                {
                    Id = src.LeadCustomerId,
                    FirstName = src.FirstName,
                    LastName = src.LastName,
                    EmailAddress = src.EmailAddress,
                    MobileNo = src.MobileNo
                };
            }));

Upvotes: 1

G_S
G_S

Reputation: 7110

They are many many mapper libraries out in the internet. To name a few

  • automapper
  • mapster
  • emitmapper

All of them have their own pros and cons. I would personally feel writing your own mapper methods would be worth it as you will have complete control of what you write.

I agree it could take some time but it doesn't take so long.

Upvotes: 4

Jasmeet
Jasmeet

Reputation: 1460

A simple straightforward approach could be to have a function on Lead class to return the object you need.

public class Lead
{
    //// your properties
    public LeadDto GetOtherObject()
    {
        LeadDto object = new LeadDto();
        ////Map properties from this to object
        return object;
    }
}

Else you can have static utility function if above approach doesn't seems right in given context.

public static class Utility
{
    public static LeadDto GetOtherObject(Lead leadObject)
    {
        LeadDto object = new LeadDto();
        ////Map properties from leadObject to object
        return object;
    }
}

If the coupling of LeadDto is strictly only with Lead, you can have a Lead object as parameter itself in constructor of LeadDto.

While mapping properties from Lead to LeadDto, please make sure you consider LeadPriority and LeadStatus with caution as they might work differently based on whether they are structs or classes.

Upvotes: 2

Related Questions