Created
March 7, 2012 22:05
-
-
Save ferventcoder/1996547 to your computer and use it in GitHub Desktop.
Automapper config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// one way mapping shown for clarity sake and lots of ForMember() relationships removed | |
public class AutoMapperProfileWeb : Profile | |
{ | |
public override string ProfileName | |
{ | |
get { return "SomeApp.Web"; } | |
} | |
protected override void Configure() | |
{ | |
Mapper.CreateMap<TheEvent, EventModel>() | |
; | |
Mapper.CreateMap<Order, OrderModel>() | |
; | |
Mapper.CreateMap<OrderLine, OrderLineModel>() | |
; | |
Mapper.CreateMap<Item, ItemModel>() | |
; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TheEvent : BaseDomainObject<long> | |
{ | |
public TheEvent() | |
{ | |
Orders = new HashSet<Order>(); | |
} | |
public DateTime EventDate { get; set; } | |
public string Location { get; set; } | |
public virtual ICollection<Order> Orders { get; set; } | |
} | |
public class Order : BaseDomainObject<long> | |
{ | |
public Order() | |
{ | |
OrderLines = new HashSet<OrderLine>(); | |
} | |
public DateTime OrderDate { get; set; } | |
public virtual Event Event { get; set; } | |
public long EventId { get; set; } | |
public virtual ICollection<OrderLine> OrderLines { get; set; } | |
} | |
public class OrderLine : BaseDomainObject<long> | |
{ | |
public virtual Item Item { get; set; } | |
public long ItemId { get; set; } | |
public int Quantity { get; set; } | |
public virtual Order Order { get; set; } | |
public long OrderId { get; set; } | |
} | |
public class Item : BaseDomainObject<long> | |
{ | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lots of stuff removed for brevity | |
public class EventModel : IModel | |
{ | |
public long Id { get; set; } | |
public EventModel() | |
{ | |
Orders = new HashSet<OrderModel>(); | |
} | |
public ICollection<OrderModel> Orders { get; set; } | |
public DateTime EventDate { get; set; } | |
public string Location { get; set; } | |
} | |
public class OrderModel : IModel | |
{ | |
public OrderModel() | |
{ | |
OrderLines = new HashSet<OrderLineModel>(); | |
} | |
public long Id { get; set; } | |
public long EventId { get; set; } | |
public DateTime OrderDate { get; set; } | |
public virtual ICollection<OrderLineModel> OrderLines { get; set; } | |
public EventModel Event { get; set; } | |
} | |
public class OrderLineModel : IModel | |
{ | |
public long Id { get; set; } | |
public int Quantity { get; set; } | |
public ItemModel Item { get; set; } | |
public long ItemId { get; set; } | |
} | |
public class ItemModel : IModel | |
{ | |
public long Id { get; set; } | |
public string Name { get; set; } | |
public string Description { get; set; } | |
} |
Workaround for now:
public class EventIndexViewModel : EventModel
{
}
Mapper.CreateMap<TheEvent, EventIndexViewModel>()
.ForMember(dest => dest.Orders, opt => opt.Ignore())
;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am using EF with lazy loading turned on. In some of my views I do not want to get orders back when I was for events, but in some views I do.
What is currently happening is it walks all the way down the graph (basically nullifying my lazy loading) before it returns.
What I would like to do is override the profile at run time and in certain instances to cut down on either creating very specific view models and specific mapping for them.
OR
Thoughts?