Reputation: 15866
Sorry for title, I cant find the correct one. I have more than one method that returns the same result.
returning type
public class JsonTreeView
{
public int id { get; set; }
public string text { get; set; }
public string state { get; set; }
public string @checked { get; set; }
public string attributes { get; set; }
public List<JsonTreeView> children { get; set; }
}
first method
List<JsonTreeView> FromReportTree(List<ReportTree> list)
{
}
second method
List<JsonTreeView> FromLocationTree(List<LocationTree> list)
{
}
and anothers... properties of Tree models are different. for example :
LocationTree (id, name, parent, text)
ReportTree (sno, name, parent, desc)
Is it possible to write one method for all these tree models? Any suggestion or starting point?
Thanks...
Upvotes: 3
Views: 179
Reputation: 86650
If you make all your Trees to implement an interface, you can.
interface iMyTree
{
int MyTreeID {get; set;}
string MyTreePame {get; set;}
object MyTreeParent {get; set;}
string MyTreeText {get; set;}
}
class AnyTree : iMyTree
{
//any properties
//implements iMyTree
}
And that method:
List<JsonTreeView> FromMyTree(List<iMyTree> list)
{
//all trees that implement iMyTree will have the same methods, any kind of tree implementing iMyTree can be used.
}
Upvotes: 0
Reputation: 7140
It depends on what happens in those methods. You say that the various Tree models have different properties; does the logic in the method need any of the non-common properties? If the logic in each of those methods is the same, you can do this:
List<JsonTreeView> FromReportTree<T>(List<T> list) where T : BaseTree
{
//some logic
}
assuming you have a BaseTree
model of some kind, otherwise T : class
or just leave that off (not recommended).
If the logic differs, you can still do it like that by doing a check if (list is LocationTree)
and using that to do the logic specific to LocationTree
, but that can get messy.
Upvotes: 1
Reputation: 3249
Your question is a bit confusing but I think I understand. You want a single FromReportTree function.
For that, you'd most likely want ReportTree and LocationTree to have a common base class. Like:
public abstract class ReportLocationTree {
public int id { get; set; }
}
public class ReportTree : ReportLocationTree {
public string moreStuff { get; set; }
}
public class LocationTree : ReportLocationTree {
public string evenMoreStuff { get; set; }
}
List<JsonTreeView> FromReportTree(List<ReportLocationTree> list)
{
list.Select(t => new JsonTreeView { id = t.id }).ToList();
}
I wasn't sure how you were serailizing, so I Didn't include it in my code, but it's bad form to follow a different naming convention on your properties just because they're being serailized.
JSON.Net makes it pretty easy on you: http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializationAttributes.htm
Upvotes: 0
Reputation: 700800
I suggest that you make a private method that does the grunt work, and keep the overloaded methods for the different types. Call the private method from the other methods, with a function that creates a JsonTreeView
object from the specific objects of that method:
private List<JsonTreeView> FromReportTree<T>(List<T> list, Func<T, JsonTreeView> convert) {
// loop through the list and call convert to create items
List<JsonTreeView> result = new List<JsonTreeView>();
foreach (T item in list) {
result.Add(convert(item));
}
return result;
}
List<JsonTreeView> FromReportTree(List<ReportTree> list) {
return FromReportTree(list, t => new JsonTreeView(t.id, t.text, ... ));
}
List<JsonTreeView> FromReportTree(List<LocationTree> list) {
return FromReportTree(list, t => new JsonTreeView(t.sno, t.desc, ... ));
}
Upvotes: 2