0

I have a system, which stores different objects in DB. Each such object has properties. Some of them are relevant for all objects (like Name, Description), but some are specific to each object and depend on its type.

Now I need to create a web method, that will retrieve that objects and their properties as set of POCOs.

Ideally, the response should be an array of such items:

public class ObjectInfo
{
     public string Name {get;set;}
     public Dictionary<string,string> Props {get;set;}
}

Before generating ObjectInfo collection - code goes to database and retrieves business objects. For instance, one object has type "text", another one - type "number". Number type has decimal property, which is not relevant for "text". Currently my code still puts into collection of props for text as well. But this is what I need to get change. And there is only such thing in my head - add if/else branches for each specific type - to indicate if it's needed to search for type specific property and add it to target props collection.

Is there a way better from SOLID and clean code perspective than if/else portion to add needed properties to Props dictionary based on type? So that they appear only for Objects where they are relevant. I thought about different creational patterns, but didn't find relevant.

        var resultItems = new List<ObjectInfo>();
        var businessObjects = this.GetBusinessObjects();
        foreach(var businessObject in businessObjects) 
        {
            var props = new Dictionary<string,string>();
            switch(businessObject.Type)
            {
                case "text":
                    props.Add("UsedInSearch", businessObject["UsedInSearch"]);
                    break;
                case "number":
                    props.Add("Decimal", businessObject["Decimal"]);
                    break;
                // number of cases will be potentially big. And number of props for each will also be different and bigger than one
            }

            var objectInfo = new ObjectInfo 
            {
                Name = businessObject["Name"],
                Props = props
            };
            resultItems.Add(objectInfo);
        }
  • I don't understand your question. Your code example doesn't include the code that adds properties to your ObjectInfo objects, so I don't know how we're supposed to meaningfully evaluate your design. – Robert Harvey May 14 at 21:18
  • Can you also explain what you mean by "better," without resorting to wandering generalities like "best practice" "more elegant" or "most popular?" Overall your question (and your objectives) needs to be more specific. – Robert Harvey May 14 at 21:19
  • When writing code, I always try to keep in mind: SOLID and clean code. But cannot find good resolution here. I've extended the question a bit. – AsmaMan May 14 at 21:26
  • There isn't any way to tell from the code you've posted so far whether or not you're being SOLID and "clean." – Robert Harvey May 14 at 21:35
  • if-else branches aren't necessarily dirty or bad or "unclean." The Factory Method pattern uses them to decide which class to instantiate. See here. – Robert Harvey May 14 at 21:44
1

Looking at your code, you only appear to have one "prop" per object type. Therefore, you don't need a Dictionary<string, string>. Just a KeyValuePair<string, string> or even a tuple, (string key, string value) would suffice.

Secondly, your switch or if/else approach is far too procedural for this task. It's a declarative problem, so use a declarative solution:

public class ObjectInfo
{
    public string Name {get;set;}
    public (string key,string value) Prop {get;set;}
}

…

private static readonly Dictionary<string, string> ObjectPropMappings =
    new Dictionary<string, string> {
        ["text"] = "UsedInSearch",
        ["number"] = "Decimal",
        ...
    };

...

var resultItems = GetBusinessObjects().Select(businessObject => new ObjectInfo
{
    Prop = (businessObject.Type, businessObject[ObjectPropMappings[businessObject.Type]]),
    Name = businessObject["Name"]
}.ToList();

If my assumption that there is only one prop per business object type if false, then re-introduced that Dictionary into ObjectInfo and make ObjectPropMappings a Dctionary<string, List<string>> to handle multiple props.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.