Sunday, May 29, 2011

When it comes to ORM, be mapping-agnostic

Why we need to be mapping-agnostic, especially when we are using a strongly-typed language? One way or another, the classes themselves are a form of mapping.


If you are using a non-strongly-typed language, mapping things explicitly is the way things are ought to be done. This hypothetical non-strongly-typed language warrants explicit mapping:

class Country
{
 CountryId;

 CountryName;
 Population;
 People;
}


class Band
{
 BandId;
  
 BandName;
 Fans;
}


class Person
{
 PersonId;

 PersonName;
 Country;
 FavoriteBand;
}

But the following classes and properties already has all the necessary information that your ORM need to automatically jumpstart mapping your classes to tables, your properties to fields.

public class Country
{
 public virtual int CountryId { get; set; }

 public virtual string CountryName { get; set; }
 public virtual int Population { get; set; }
 public virtual IList<Person> People { get; set; }
}


public class Band
{
 public virtual int BandId { get; set; }
  
 public virtual string BandName { get; set; }
 public virtual IList<Person> Fans { get; set; }
}



public class Person
{
 public virtual int PersonId { get; set; }

 public virtual string PersonName { get; set; }
 public virtual Country Country { get; set; }
 public virtual Band FavoriteBand { get; set; }
}

Aside from your properties has a type already, class reference on another class is already a clue for your ORM to map your class as many-to-one to another class, the IList is already a clue to your ORM that your class is one-to-many to the other class.


So don't let yourselves get caught up with the brewing tension between James Gregory and Fabio Maulo. Your strongly-typed code is already a form of mapping. NHibernate 3.2's built-in Fluent^H^H^H^H^H^HLoquacious-mapper is indeed loquacious. Maybe it's just me, but I've got a feeling that Loquacious mapper won't take off.


That being said, I still highly recommend James Gregory's Fluent mapper instead and its Auto Mapping; by far, Fluent NHibernate is still the simplest way to override some minor things that are not ought to be automapped. Still though, I want James Gregory to make the next Fluent NHibernate not internally process mappings as XMLs. Fluent NHibernate 2.0 FTW! go go go! 加油!

No comments:

Post a Comment