Mapping conventions

  • Name -> Name
  • PropertyName -> PropertyName or Property.Name
  • AnotherPropertyName -> AnotherPropertyName or Another.PropertyName or AnotherProperty.Name or Another.Property.Name
  • PROPERTY_NAME -> PROPERTY_NAME or PROPERTY.NAME

Usage

We use EF Code First for our examples. But it also works for any ORM (LINQ2SQL, NHibernate etc.).
  • Very simple
public class Person
{
   public int Id {get;set;}
   public string Name {get;set;}
}
public class PersonView
{
   public int Id {get;set;}
   public string Name {get;set;}
}
public class DataContext: DbContext
{
   public DbSet<Person> People {get;set;}
}
...
var person = new DataContext().People.Project().To<PersonView>().First();
  • Nested objects
public class Person
{
   public int Id {get;set;}
   public string Name {get;set;}
   public Country Citizenship {get;set;}
}
public class Country 
{
   public int Id {get;set;}
   public string Name {get;set;}
}
public class PersonView
{
   public int Id {get;set;}
   public string Name {get;set;}
   public int? CitizenshipId {get;set;}
   public string CitizenshipName {get;set;}
}
public class DataContext: DbContext
{
   public DbSet<Person> People {get;set;}
}
...
var person = new DataContext().People.Project().To<PersonView>().First();
  • Custom mapping
public class Person
{
   public int Id {get;set;}
   public string Name {get;set;}
   public ICollection<Post> Posts {get;set;}
}
public class PersonView
{
   public int Id {get;set;}
   public string PersonName {get;set;}
   public int NumberOfPosts {get;set;}
}
public class DataContext: DbContext
{
   public DbSet<Person> People {get;set;}
}
...
var person = new DataContext().People.Project().To<PersonView>(
   m=>m
   .Map(t=>t.PersonName, s=>s.Name)
   .Map(t=>t.NumberOfPosts, s=>s.Posts.Count())
   ;
).First();

Last edited Mar 11, 2013 at 7:15 AM by kgolovchenko, version 9

Comments

No comments yet.