Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to develop methods which has the same functionality for some entities?
    text
    copied!<p>I have a framework of classes. Each class represents some entity and has basic static methods: Add, Get, Update and Delete. </p> <p>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).</p> <p>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.</p> <p>What is the best approach to implement it?</p> <p>Should i do it in this way for every class:</p> <p>E.g.:</p> <pre><code>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); } </code></pre> <p>Or is it better to do it using InvokeMember(according to idea to have all the code in one place)?</p> <p>E.g.:</p> <pre><code> 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); } } </code></pre> <p>What approach is better and more cleaner? Or maybe you will suggest another approach?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload