I am writing a small program. The interface I am writing to control each repository that is made defines a method of Save(IPublicObject). I am using LINQ for the SQL Version of the repository CRUD. My question is this. I would like to have only the one method which accepts the interface type. I want to think how I can best locate the Save action for the inherited type I then pass in.
In the book I am reading Patterns of Enterprise Application Architecture. I am leaning on the Inheritance Maping. So I create a derived object of
public class ConcretePublicObjectOne : IPublicObject{}
I want to then pass this into the Save Function of the respository. It is at this point where I am trying to think how best to say, ok we need to use "WHAT?" Save Method etc...
Should I use a registry, configuration setting mapping the types?
For LINQ-to-SQL, the data-context already does a lot of the mapping for you. As such, I think generics might be the best way to achieve a save while still having some consideration of your interface (although I'm not quite sure what the interface is giving you in this scenario...).
You can access the generic aspect of the data-context via GetTable<T>()
:
static void Save<T>(T item)
where T : class, IPublicObject
{
using (DataContext ctx = GetDataContext())
{
Table<T> table = ctx.GetTable<T>();
// for insert...
table.InsertOnSubmit(item);
// for update...
table.Attach(item, true);
// for delete...
table.DeleteOnSubmit(item);
ctx.SubmitChanges();
}
}
Note that type-inference should mean that you don't need to specify the T
when saving:
Foo foo = new Foo();
Save(foo); // <Foo> is inferred
Is that any use?