ObjectSpaces - thin?
One of the most innovative things about ObjectSpaces is the fact that your business entities are not required to inherit from any base class.
With every persistence framework I have worked with – this has always been a requirement. As in this example, EntityBase would provide your O/R mapping and persistence functionality:
public class Employee : EntityBase
{
private int _ID;
private string _fullName;
public int ID {get {return _ID;} set{_ID = value;}}
public string FullName{get {return _fullName;} set{_fullName = value;}}
public Employee()
{
}
}
By not requiring use of a base class – I think Microsoft has done a great job keeping ObjectSpaces as thin and transparent as possible.
How was this accomplished?
The ObjectSpace classes (driven by map files) consume your custom classes, instead of providing functionality through inheritance.
For example to retrieve an employee,
ObjectSpace os = new ObjectSpace("map.xml", conn);
ObjectReader reader = os.GetObjectReader(new ObjectQuery(typeof(Employee), "ID = 1234’"));
Employee myEmp = (Employee) reader.Current;
Very nice. However, you might say… how is this considered thin? I’m used to instantiating business objects as easily as this:
A) Employee myEmp = new Employee(id);
OR
B) Employee myEmp = Employee.GetByID(id);
Well, the good news is… if you have been using a factory design pattern to instantiate your biz objects (as in B) – then you will be able to encapsulate all of the ObjectSpace classes within your static method (GetByID):
Similar to this,
public class Employee
{
private int _ID;
private string _fullName;
public int ID {get {return _ID;} set{_ID = value;}}
public string FullName{get {return _fullName;} set{_fullName = value;}}
public Employee()
{
}
public static Employee GetById(int ID)
{
ObjectSpace os = new ObjectSpace("map.xml", conn);
ObjectReader reader = os.GetObjectReader(new ObjectQuery(typeof(Employee), String.Format("ID = {0}", ID)));
return((Employee)reader.Current);
}
}
So, if you have been using a factory creational pattern – perhaps the developers consuming your business objects need not be aware of a conversion to ObjectSpaces at all…. Seems pretty tight to me. Of course, I have not seen examples of inserts/updates using ObjectSpaces yet. Please comment!
-Mike
PDC presentation , Lucas Bolonese on ObjectSpaces.