encapsulate custom mapping and error with Sum

Mar 25, 2013 at 5:49 AM
Edited Mar 25, 2013 at 6:33 AM
Hi. Thanks for sharing this. I have a several question.
  1. Is it possible to encapsulate the custom mappings?
    Something like that:
var person = new DataContext().People.Project().To<PersonView>(PersonToPersonViewMapping).First();
The PersonToPersonViewMapping will contain the mapping.

And other question.
I have one-to-many ref:
public class Order:Entity
    {
        public Order()
        {
            OrderDetails = new List<OrderDetail>();
        }
        public DateTime Date { get; set; }
        public string Description { get; set; }
        public ICollection<OrderDetail> OrderDetails { get; set; }
    }

public class OrderDetail:Entity
    {
        public virtual Order Order { get; set; }
        public decimal Sum { get; set; }
    }
This is view model class:
public class OrderViewModel
    {
        public string Description { get; set; }
        public decimal Sum { get; set; }
    }
This is my query:
EfRepository<Order> repository = new EfRepository<Order>(new EFSContext());
         
 var result = repository.Query().Where(o=>o.Id == 1).Project().To<OrderViewModel>( m=>m.Map(v=>v.Sum, o=>o.OrderDetails.Sum(d=>d.Sum)));
Butm I get the error:
Instance property "System.Decimal Sum" is not defined for type "EFSPrototype.Domain.Order"
Yes, the Sum property is defined in OrderDetail and in the query I get Sum from OrderDetails. Where is a error?

Thanks.
Coordinator
Apr 4, 2013 at 4:55 PM
Edited Apr 4, 2013 at 5:25 PM
sharok9 wrote:
Hi. Thanks for sharing this. I have a several question.
  1. Is it possible to encapsulate the custom mappings?
    Something like that:
var person = new DataContext().People.Project().To<PersonView>(PersonToPersonViewMapping).First();
The PersonToPersonViewMapping will contain the mapping.
This is possible. You shoud define your PersonToPersonViewMapping as a method that takes Mapper<Person, PersonView> as parameter:
static void PersonToPersonViewMapping(Mapper<Person, PersonView> m)
{
     //m.Map( ... );
}
Your second question revealed a bug in the library. Thank's. Now it is fixed. You should get latest version.
Or wait for new version which is planned to be published this month.
Apr 5, 2013 at 3:50 AM
Thanks for answer.
I would also like to see a method that does not map fields by default, only uses custom mappings. Terribly uncomfortable to write Ignore for all unnecessary fields.
For example we have a class:
public class Document
{
   public DateTime CreatedDate {get; set;}
   public string Name {get; set;}
   public string Description {get; set;}
   public Person Author {get; set;}
  ...
}
and, we have a DTO:
public class DocumentDto
{
   public DateTime CreatedDate {get; set;}
   public string Name {get; set;}
   public string Description {get; set;}
   public string AuthorFullName {get; set;}
}
We use this DTO in the several places. For drop-down list we need only the name and CreatedDate fields. In this case I have to map only this two fields, but ignore all others. This is not hard for this example, but what if the class have about 15 fields. In detail view I need to all fields so I will map all fields.
Thanks