ویرگول
ورودثبت نام
پرووید
پرووید
پرووید
پرووید
خواندن ۶ دقیقه·۶ سال پیش

نگاشت کردن Complex Type ها به Primitive Type ها با استفاده از AutoMapper

در این مقاله که بخشی از سری آموزش AutoMapper از وبسایت پرووید است در رابطه با نگاشت کردن و یا همان map کردن complex type ها به primitive type ها با استفاده از AutoMapper صحبت خواهیم کرد. برای درک هرچه بهتر ماهیت complex type ها و primitive type ها توصیه می‌کنیم از بسته ی آموزش ویدئویی سی شارپ دات نت استفاده کنید.

زمانی که یکی از دو کلاسی که در عملیات نگاشت کردن شرکت می کنند شامل property هایی از نوع primitive type ها می باشند و کلاس دیگر شامل property هایی از نوع complex type هستند سناریوی نگاشت کمی پیچیده‌تر می‌شود. در چنین شرایطی نیاز به پیکربندی های خاصی است که با مطالعه کردن همین مقاله می توانید آنها را درک کنید.

حتماً توصیه می‌کنیم که مقاله های قبلی موجود در این سری آموزشی را مطالعه کرده تا درک بهتری از مفاهیم مطرح شده در این مقاله به دست بیاورید. برای درک بهتر مباحث مطرح شده در این آموزش شبیه به چیزی که در تصویر زیر مشاهده می کنید از چهار کلاس برای عملیات mapping استفاده خواهیم کرد.

public class Employee

{

public string Name { get; set; }

public int Salary { get; set; }

public string Department { get; set; }

public Address address { get; set; }

}

public class EmployeeDTO

{

public string Name { get; set; }

public int Salary { get; set; }

public string Department { get; set; }

public AddressDTO address { get; set; }

}

public class Address

{

public string City { get; set; }

public string Stae { get; set; }

public string Country { get; set; }

}

public class AddressDTO

{

public string City { get; set; }

public string Stae { get; set; }

public string Country { get; set; }

}

}

همان طور که در تصویر بالا می بینید ما می‌خواهیم که یک complex property که مربوط به کلاس Employee می باشد را به property هایی به اسم City و State و Country که در EmployeeDTO تعریف شده‌اند نگاشت کنیم. برای انجام دادن این کار ابتدا باید پیکربندی های AutoMapper را شبیه به چیزی که در کد زیر مشاهده می کنید انجام بدهیم.


static void InitializeAutomapper()

{

Mapper.Initialize(cfg =>

{

cfg.CreateMap<Employee, EmployeeDTO>();

});

}

همانطور که در قسمت بالا مشاهده می کنید ما هر property از درون complex type یعنی همان پروپرتی Address که در کلاس Employee تعریف شده است را به property هایی که در کلاس EmployeeDTO تعریف شده اند، نگاشت کرده‌ایم. کدی که در قسمت زیر مشاهده می کنید تمامی این کار را به شما نشان می دهد.

using AutoMapper;

namespace AutoMapperDemo

{

class Program

{

static void Main(string[] args)

{

//Step1: Call the InitializeAutomapper method to initialize the Mapper

//In InitializeAutomapper method we defined the Mappings

InitializeAutomapper();

//Step2: Create and populate the Employee object

Address empAddres = new Address()

{

City = "Mumbai",

State = "Maharashtra",

Country = "India"

};

Employee emp = new Employee();

emp.Name = "James";

emp.Salary = 20000;

emp.Department = "IT";

emp.address = empAddres;

//Step3: Use the mapper to map the employee data with the Employee DTO

var empDTO = Mapper.Map<Employee, EmployeeDTO>(emp);

Console.WriteLine("Name:" + empDTO.Name + ", Salary:" + empDTO.Salary +", Department:" + empDTO.Department);

Console.WriteLine("City:" + empDTO.City + ", State:" + empDTO.State + ", Country:" + empDTO.Country);

Console.ReadLine();

}

static void InitializeAutomapper()

{

Mapper.Initialize(cfg =>

{

cfg.CreateMap<Employee, EmployeeDTO>()

.ForMember(dest => dest.City, act => act.MapFrom(src => src.address.City))

.ForMember(dest => dest.State, act => act.MapFrom(src => src.address.State))

.ForMember(dest => dest.Country, act => act.MapFrom(src => src.address.Country));

});

}

}

public class Employee

{

public string Name { get; set; }

public int Salary { get; set; }

public string Department { get; set; }

public Address address { get; set; }

}

public class EmployeeDTO

{

public string Name { get; set; }

public int Salary { get; set; }

public string Department { get; set; }

public string City { get; set; }

public string State { get; set; }

public string Country { get; set; }

}

public class Address

{

public string City { get; set; }

public string State { get; set; }

public string Country { get; set; }

}

}

حال می توانید برنامه را اجرا کنید و خروجی را دریافت کنید.

بگذارید کمی مسئله را تغییر دهیم. فرض کنید با ایجاد شدن یک تغییر در نیازمندی، حال می‌خواهیم property هایی که از نوع primitive type هستند را به یک property که از نوع complex type است نگاشت کنیم. این موضوع در تصویر زیر نشان داده شده است.


public class EmployeeDTO

{

public string Name { get; set; }

public int Salary { get; set; }

public string Department { get; set; }

public AddressDTO addressDTO { get; set; }

}

همانطور که در قسمت بالا می بینید ما در این مثال می خواهیم که property هایی به اسم City و State و Country که در Employee تعریف شده اند را به یک property به نام Address که در EmployeeDTO تعریف شده است نگاشت کنیم.


Below is the complete example.

using AutoMapper;

namespace AutoMapperDemo

{

class Program

{

static void Main(string[] args)

{

//Step1: Call the InitializeAutomapper method to initialize the Mapper

//In InitializeAutomapper method we defined the Mappings

InitializeAutomapper();

//Step2: Create and populate the Employee object

Address empAddres = new Address()

{

City = "Mumbai",

State = "Maharashtra",

Country = "India"

};

Employee emp = new Employee();

emp.Name = "James";

emp.Salary = 20000;

emp.Department = "IT";

emp.City = "Mumbai";

emp.State = "Maharashtra";

emp.Country = "India";

//Step3: Use the mapper to map the employee data with the Employee DTO

var empDTO = Mapper.Map<Employee, EmployeeDTO>(emp);

Console.WriteLine("Name:" + empDTO.Name + ", Salary:" + empDTO.Salary +", Department:" + empDTO.Department);

Console.WriteLine("City:" + empDTO.address.City + ", State:" + empDTO.address.State + ", Country:" + empDTO.address.Country);

Console.ReadLine();

}

static void InitializeAutomapper()

{

Mapper.Initialize(cfg =>

{

cfg.CreateMap<Employee, EmployeeDTO>()

.ForMember(dest => dest.address, act => act.MapFrom(src => new Address()

{

City = src.City,

State = src.State,

Country = src.Country

}));

});

}

}

public class Employee

{

public string Name { get; set; }

public int Salary { get; set; }

public string Department { get; set; }

public string City { get; set; }

public string State { get; set; }

public string Country { get; set; }

}

public class EmployeeDTO

{

public string Name { get; set; }

public int Salary { get; set; }

public string Department { get; set; }

public Address address { get; set; }

}

public class Address

{

public string City { get; set; }

public string State { get; set; }

public string Country { get; set; }

}

}

حال اگر برنامه را اجرا کنید خروجی را دریافت خواهید کرد. در قسمت بعدی از این مقاله در رابطه با نگاشت معکوس و یا reverse mapping با AutoMapper صحبت خواهم کرد.

۰
۰
پرووید
پرووید
شاید از این پست‌ها خوشتان بیاید