0

I'm working with a C# API Wrapper project that is entirely flat, meaning 1 project and namespace for all the files. I'm attempting to put the Models into a separate Models project, but I've hit a design snag.

The Model classes all have a static Parse method for loading the properties of the Model from the JSON.

Ex.

public static Category Parse(APIClient client, dynamic categoryElement)
{
    var category = new Category();

    //Parse Attributes
    category.ID = categoryElement.id as string;
    category.RuleSetID = categoryElement.rulesetid as string;
    category.Name = categoryElement.name as string;

    //linked property
    category.RuleSet = client.RuleSetClient.GetRuleSet(category.RuleSetID);

    return category
}

The problem is the Parse method uses the APIClient object to make additional API calls to load larger "linked" properties. The APIClient class is in the Main project so once I moved the models to their own project they can't access this object thus erring out. I can't add a reference from the Models project from the Main Project because that's a circular reference (main project is already referencing Models project). So I definitely have to move the Parse methods out of the Model classes, the question is where do I move them.

I'm thinking the Parse methods needs to go in the Client classes in the Main Project and not the Model classes. Originally I was thinking about putting them all in an extension method class, but I think that's too much business logic in the extensions. I think the "Client" classes are actually servies, so this is where the various parse methods should go. For example, CateogryClient.cs will have both "GetCategory" method and "Parse" method.

Is this a good design concept? Is it normal for a service layer have both the API calls and the Parsing of the Models? Are these client classes effectively services (If so I might re-name them for clarity)?

  • 1
    The encountered problem demonstrates the issues with violating single responsibility principle. You are correct in realizing that the responsibility should be that of the client. Let the model be simply models. – Nkosi Aug 18 '19 at 22:34
  • 1
    Parse can be seen as an implementation concern that probably does not even need to be exposed. the client/service in getting its model will need to parse data, but that is of no concern to the caller of the get method – Nkosi Aug 18 '19 at 22:36
  • Yea that makes sense, parse really could be private to the client classes. Do you think the “client” classes are really service layer classes? – mgmedick Aug 18 '19 at 22:42
  • 1
    IMO they act more like services. – Nkosi Aug 18 '19 at 22:44
  • @Nkosi ok thankyou. – mgmedick Aug 18 '19 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.