I have a framework of classes. Each class represents some entity and has basic static methods: Add, Get, Update and Delete.
This methods made static because i want to allow perform some action without instantiating of the object. E.g., to add some object i have to do this: Foo.Add(foo).
Now i want to have number of methods which would be invoked from another framework for every class. But the functionality of this methods will be the same: for example i want to check if object exists and if it doesn't - create, otherwise - update.
What is the best approach to implement it?
Should i do it in this way for every class:
E.g.:
public void DoSomethingWithFoo(Foo foo)
{
if (Foo.Get(foo.id) != null)
Foo.Update(foo);
else
Foo.Add(foo);
}
public void DoSomethingWithBar(Bar bar)
{
if (Bar.Get(bar.id) != null)
Bar.Update(bar);
else
Bar.Add(bar);
}
Or is it better to do it using InvokeMember(according to idea to have all the code in one place)?
E.g.:
public void DoSomethingWithFoo(Foo foo)
{
DoSomethingWithObject(foo);
}
private void DoSomethingWithObject(object obj)
{
Type type = obj.GetType();
object[] args = {type.GetProperty("ID").GetValue(obj, null)};
object[] args2 = { obj };
if (type.InvokeMember("Get", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, args) != null)
{
type.InvokeMember("Update", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, args2);
}
else
{
type.InvokeMember("Add", BindingFlags.Default | BindingFlags.InvokeMethod, null, null, args2);
}
}
What approach is better and more cleaner? Or maybe you will suggest another approach?
Thanks