1

I have a similar structure to the one below

Base class

public class BaseClass
{
    public string Name { get; set; }
    public string Address { get; set; }
    public int Age { get; set; }
    public Guid Guid { get; set; }
    public string Hometown { get; set; }
}

Derived Class

public class DerivedClass : BaseClass
{
    public List<DerivedClassDataItem> Data { get; set; }
}

Data class

public class DerivedClassDataItem
{
    public string Datum1 { get; set; }
    public string Datum2 { get; set; }
    public string Datum3 { get; set; }
    public string Datum4 { get; set; }
    public int Datum5 { get; set; }
    public DateTime Datum6 { get; set; }
}

What is the best practice to return specific set of info from the DerivedClass?
a potential set could be: Name, Address, Guid and then a Data list that only contains Datum1 and Datum4

I could see anonymousTypes, Tuples or another set of class(es), all to be valid approaches.

My concern about creating new set of classs for the set returned is that the class(s) structure will be similar to the structure of the three mentioned above except it will have fewer selected members, which to me, does not sound ideal. (duplicate code and structure)

Using anonymousTypes was my initial solution to tackle this, something like

List<DerivedClass> list = new List<DerivedClass>();

var mySet = list.Select(d => new
{
    Name = d.Name,
    Address = d.Address,
    .
    .
    .
    .
    .
    Data = d.Data.Select(item => new
    {
        Datum1 = item.Datum1,
        Datum4 = item.Datum4
    })
});

but again, that was a headache for us to track through httpResponse and through out API calls.

Should I go with Tuple?

Any insights as to what is the best practice for doing this?

Edit I am using this set of data to be a response returned by a API/GET call. I will send the set back using HttpRespose and then the framework will transform that into json

this is an actual method we have now

private void populateReturnFile()
{
    var returnFileAnonymous = new
    {
        Vendor = this.Vendor,
        OrganizationName = this.OrganizationName,
        User = this.User,
        Platform = this.Platform,
        DictionaryType = this.DictionaryType,
        UseCaseId = this.UseCaseId,
        Data = this.Data.Select(d => new
            {
                MigrationTermId = d.MigrationTermId,
                ImoLexicalCode = d.ImoLexicalCode
            })
    };

    this.returnFile = returnFileAnonymous;
}

Then my GET will return the retunFile (this is a very simple method, i have remove irrelevant code)

[HttpGet]
public HttpResponseMessage Get(Guid migrationFileId)
{
    ProblemList problemList = ProblemList.GetProblemList(migrationFileId);

    return Request.CreateResponse(HttpStatusCode.OK, problemList.ReturnFile, new JsonMediaTypeFormatter());
}
Rami Alshareef
  • 7,015
  • 12
  • 47
  • 75
  • 1
    *to return specific set of info from the `DerivedClass`* - return where? Data objects can also have hierarchy, there could be base methods to operate with base properties (`Name`, `Address`, etc.) and inherited methods which does something with inherited properties as well (`Datum1`, etc.). – Sinatr Jul 24 '15 at 15:07
  • Please clarify your goal. So far it is very unclear as classes don't return anything... You may be looking for ["map"](https://en.wikipedia.org/wiki/Map_(higher-order_function)) concept... Additionally asking for "best practices" without clarifying what's wrong with your current approach is asking for "opinion based" close - some existing questions like http://stackoverflow.com/questions/13640322/what-and-when-to-use-tuple may need to be mentioned to show what you already looked at. – Alexei Levenkov Jul 24 '15 at 15:14
  • 1
    I have updated the question. Hope that makes it clearer for others – Rami Alshareef Jul 24 '15 at 15:14

1 Answers1

2

If API calls is where you are using these classes, then I personally like to keep it simple and avoid complex inheritance hierarchy. Remember, simple code is good code.

I would make a separate class for each api request/response call. For very simple api calls (ajax requests for example) I like to use anonymous types, but for controllers that only handle API calls I like to create separate classes, organized in a nice folder structure.

Everyone has their "style" but as long as you strive for simplicity your code will be maintainable.

Thufir Hawat
  • 456
  • 5
  • 15
  • I would like to read about this. Do you have sources (articles, KBs) to backup what was your motivation for this chosen style? – Rami Alshareef Jul 24 '15 at 15:37
  • I don't believe there is much reading material on this because these ideas are very opinion based and there are many schools of thought, from my experience this structure works well in most cases. I suggest building the structure you think is best, then after completing the project analyze if your solution was easy to work with, easy to understand, easy to update. – Thufir Hawat Jul 24 '15 at 15:57