SlideShare a Scribd company logo
1 of 8
Introduction
AutoMapper is an object-to-object mapper, which
allows you to solve issues with mapping of the same
properties in one object of one type to another object
of another type. For example, mapping a heavy entity
Customer object to the CustomerDTO could be done
with AutoMapper automatically.
Problem
Customer customer = GetCustomerFromDB();
CustomerViewItem customerViewItem = new CustomerViewItem()
{
FirstName = customer.FirstName,
LastName = customer.LastName,
DateOfBirth = customer.DateOfBirth,
NumberOfOrders = customer.NumberOfOrders
};
ShowCustomerInDataGrid(customerViewItem);
Get Started
Mapper.CreateMap<Customer, CustomerViewItem>();
Customer customer = GetCustomerFromDB();
CustomerViewItem customerViewItem =
Mapper.Map<Customer, CustomerViewItem>(customer);
ShowCustomerInDataGrid(customerViewItem);
Custom Map
Mapper.CreateMap<Customer, CustomerViewItem>() .ForMember(cv =>
cv.FullName, m => m.MapFrom( s => s.FirstName + " " + s.LastName));
Flattening
public class Customer { public Company Company { get; set; } }
public class Company { public string Name { get; set; } }
public class CustomerViewItem
{
public string CompanyName { get; set; }
}
AutoMapper goes in to the depth of your classes, and if names
match, it will do the mapping for you.
Custom Type Resolvers
public class Customer { public bool VIP { get; set; } }
public class CustomerViewItem { public string VIP { get; set; }
public class VIPResolver : ValueResolver<bool, string>
{
protected override string ResolveCore(bool source)
{
return source ? "Y" : "N";
}
}
.ForMember(cv => cv.VIP, m =>
m.ResolveUsing<VIPResolver>().FromMember(x => x.VIP));
Custom Formatters
public class DateFormatter:IValueFormatter
{
public string FormatValue(ResolutionContext context)
{
return ((DateTime) context.SourceValue).ToLongDateString();
}
}
.ForMember(cv => cv.DateOfBirth, m =>
m.AddFormatter<DateFormatter>());

More Related Content

Similar to 16 auto mapper

Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011
Johan Nilsson
 

Similar to 16 auto mapper (9)

Automapper
AutomapperAutomapper
Automapper
 
Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011Custom UI Components at Android Only 2011
Custom UI Components at Android Only 2011
 
Kotlin Mullets
Kotlin MulletsKotlin Mullets
Kotlin Mullets
 
AutoMapper
AutoMapperAutoMapper
AutoMapper
 
Domain Driven Design 101
Domain Driven Design 101Domain Driven Design 101
Domain Driven Design 101
 
Google Chart Tools Kanika Garg (10BM60035) Lavanya R. (10BM60042)
Google Chart Tools Kanika Garg (10BM60035) Lavanya R. (10BM60042)Google Chart Tools Kanika Garg (10BM60035) Lavanya R. (10BM60042)
Google Chart Tools Kanika Garg (10BM60035) Lavanya R. (10BM60042)
 
Google Chart Tools
Google Chart Tools Google Chart Tools
Google Chart Tools
 
A head start on cloud native event driven applications - bigdatadays
A head start on cloud native event driven applications - bigdatadaysA head start on cloud native event driven applications - bigdatadays
A head start on cloud native event driven applications - bigdatadays
 
Creating an Uber Clone - Part XXII - Transcript.pdf
Creating an Uber Clone - Part XXII - Transcript.pdfCreating an Uber Clone - Part XXII - Transcript.pdf
Creating an Uber Clone - Part XXII - Transcript.pdf
 

Recently uploaded

Recently uploaded (20)

Chandrapur Call Girl Just Call 8084732287 Top Class Call Girl Service Available
Chandrapur Call Girl Just Call 8084732287 Top Class Call Girl Service AvailableChandrapur Call Girl Just Call 8084732287 Top Class Call Girl Service Available
Chandrapur Call Girl Just Call 8084732287 Top Class Call Girl Service Available
 
BADDI 💋 Call Girl 9827461493 Call Girls in Escort service book now
BADDI 💋 Call Girl 9827461493 Call Girls in  Escort service book nowBADDI 💋 Call Girl 9827461493 Call Girls in  Escort service book now
BADDI 💋 Call Girl 9827461493 Call Girls in Escort service book now
 
HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024
 
Pre Engineered Building Manufacturers Hyderabad.pptx
Pre Engineered  Building Manufacturers Hyderabad.pptxPre Engineered  Building Manufacturers Hyderabad.pptx
Pre Engineered Building Manufacturers Hyderabad.pptx
 
WheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond InsightsWheelTug Short Pitch Deck 2024 | Byond Insights
WheelTug Short Pitch Deck 2024 | Byond Insights
 
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
 
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTSDurg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
Durg CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN durg ESCORTS
 
Nashik Call Girl Just Call 7091819311 Top Class Call Girl Service Available
Nashik Call Girl Just Call 7091819311 Top Class Call Girl Service AvailableNashik Call Girl Just Call 7091819311 Top Class Call Girl Service Available
Nashik Call Girl Just Call 7091819311 Top Class Call Girl Service Available
 
SRI GANGANAGAR 💋 Call Girl 9827461493 Call Girls in Escort service book now
SRI GANGANAGAR 💋 Call Girl 9827461493 Call Girls in  Escort service book nowSRI GANGANAGAR 💋 Call Girl 9827461493 Call Girls in  Escort service book now
SRI GANGANAGAR 💋 Call Girl 9827461493 Call Girls in Escort service book now
 
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAIGetting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
Getting Real with AI - Columbus DAW - May 2024 - Nick Woo from AlignAI
 
Falcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investorsFalcon Invoice Discounting: The best investment platform in india for investors
Falcon Invoice Discounting: The best investment platform in india for investors
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
JAJPUR CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN JAJPUR ESCORTS
JAJPUR CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN JAJPUR  ESCORTSJAJPUR CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN JAJPUR  ESCORTS
JAJPUR CALL GIRL ❤ 82729*64427❤ CALL GIRLS IN JAJPUR ESCORTS
 
Falcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business PotentialFalcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business Potential
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptxQSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
QSM Chap 10 Service Culture in Tourism and Hospitality Industry.pptx
 
Home Furnishings Ecommerce Platform Short Pitch 2024
Home Furnishings Ecommerce Platform Short Pitch 2024Home Furnishings Ecommerce Platform Short Pitch 2024
Home Furnishings Ecommerce Platform Short Pitch 2024
 
PARK STREET 💋 Call Girl 9827461493 Call Girls in Escort service book now
PARK STREET 💋 Call Girl 9827461493 Call Girls in  Escort service book nowPARK STREET 💋 Call Girl 9827461493 Call Girls in  Escort service book now
PARK STREET 💋 Call Girl 9827461493 Call Girls in Escort service book now
 
KOTA 💋 Call Girl 9827461493 Call Girls in Escort service book now
KOTA 💋 Call Girl 9827461493 Call Girls in  Escort service book nowKOTA 💋 Call Girl 9827461493 Call Girls in  Escort service book now
KOTA 💋 Call Girl 9827461493 Call Girls in Escort service book now
 
Cuttack Call Girl Just Call 8084732287 Top Class Call Girl Service Available
Cuttack Call Girl Just Call 8084732287 Top Class Call Girl Service AvailableCuttack Call Girl Just Call 8084732287 Top Class Call Girl Service Available
Cuttack Call Girl Just Call 8084732287 Top Class Call Girl Service Available
 

16 auto mapper

  • 1.
  • 2. Introduction AutoMapper is an object-to-object mapper, which allows you to solve issues with mapping of the same properties in one object of one type to another object of another type. For example, mapping a heavy entity Customer object to the CustomerDTO could be done with AutoMapper automatically.
  • 3. Problem Customer customer = GetCustomerFromDB(); CustomerViewItem customerViewItem = new CustomerViewItem() { FirstName = customer.FirstName, LastName = customer.LastName, DateOfBirth = customer.DateOfBirth, NumberOfOrders = customer.NumberOfOrders }; ShowCustomerInDataGrid(customerViewItem);
  • 4. Get Started Mapper.CreateMap<Customer, CustomerViewItem>(); Customer customer = GetCustomerFromDB(); CustomerViewItem customerViewItem = Mapper.Map<Customer, CustomerViewItem>(customer); ShowCustomerInDataGrid(customerViewItem);
  • 5. Custom Map Mapper.CreateMap<Customer, CustomerViewItem>() .ForMember(cv => cv.FullName, m => m.MapFrom( s => s.FirstName + " " + s.LastName));
  • 6. Flattening public class Customer { public Company Company { get; set; } } public class Company { public string Name { get; set; } } public class CustomerViewItem { public string CompanyName { get; set; } } AutoMapper goes in to the depth of your classes, and if names match, it will do the mapping for you.
  • 7. Custom Type Resolvers public class Customer { public bool VIP { get; set; } } public class CustomerViewItem { public string VIP { get; set; } public class VIPResolver : ValueResolver<bool, string> { protected override string ResolveCore(bool source) { return source ? "Y" : "N"; } } .ForMember(cv => cv.VIP, m => m.ResolveUsing<VIPResolver>().FromMember(x => x.VIP));
  • 8. Custom Formatters public class DateFormatter:IValueFormatter { public string FormatValue(ResolutionContext context) { return ((DateTime) context.SourceValue).ToLongDateString(); } } .ForMember(cv => cv.DateOfBirth, m => m.AddFormatter<DateFormatter>());