tags:

views:

34

answers:

0

Hi,

I'm using Subsonic 3.0 on a Web application that has fairly simple databse demands, so I've opted to use the SimpleRepository approach. Ideally I wanted to encapsulate functionality as much as possible instead of creating SimpleRepository objects within business objects as and when they're required. So I came up with the following architecture.

DbBase - Abstract base class which contains a protected SimpleRepository object (lazily loaded), as well as Save(), Load() and Delete() methods, a property called ID (int) for use by the SimpleRepository and a readonly const called NEW which is equal to 0 for comparative purposes on the Save (to determine whether to call repository.Add or repository.Update).

All business objects that persist to the Database are to then inherit from DbBase, and a call to Save() will trigger the underlying base implementation. This is where I'm getting stuck... I don't seem to be able to tell the base class to use the type of the concrete class in the calls to the underlying SimpleRepository methods.

Ideally I'd like to be able to do something like this in my DbBase class but obviously it doesn't work:

public void Save()
{  
  if(ID == NEW)
  {
     Repository.Add<typeof(this)>(this);
  }
  else
  {
    Repository.update<typeof(this)>(this);   
  }
}

I don't really want to have to implement an interface on all business objects then have essentially the same (or very similar) code strewn around my BO's to handle saving and loading etc. when effectively SubSonic is taking care of all of this for me.

Any suggestions or comments would be greatly appreciated :)