3

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...

2
  • 1
    Not sure about the correctness of the technique but you could have a base class that all the 'tree' classes inherit from and then you could pass a list of the base class. Doesn't feel 'right' but it should work. Commented Mar 26, 2013 at 16:25
  • TreeModels are represented DB tables. So if I do your suggestion, I should create additional DTO models, and cast them again to DB table models... Commented Mar 26, 2013 at 16:29

4 Answers 4

2

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, ... ));
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

0

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.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.