Pradeep Balakrishnan
Pradeep Balakrishnan

Reputation: 99

Best place to implement this business rule?

I have a service call that returns to me an IEnumerable of CustomObject, this is a third party call that I don't have the liberty to modify. CustomObject can be assumed to have a definition like below:

public class CustomObject
{
    public int Id { get; set; }
    public string Name { get; set; }

    ...
    ...
    ...

    public int Points { get; set; }
    public bool IsPrivate { get; set; }
}

Among the list of objects returned, I could have special CustomObject objects. I need to implement some special rules such as:

  1. If elements with Ids 1 and 3 both exist in the list, only render one of them based on rules a. If either one of them has IsPrivate flagged to true, display the one that has IsPrivate set to false b. If neither have IsPrivate set to true, display the one with higher points ... and so on

What would be the best place to implement these rules. I thought about implementing an IEqualityComparer and do a .Distinct() on my service call, doesn't seem like what IEqualityComparer is meant to do.

Suggestions?

Upvotes: 2

Views: 245

Answers (1)

NtscCobalt
NtscCobalt

Reputation: 1689

Since you cannot modify the class 'CustomObject' I would add the business logic into the 'render' pipeline and just call some function such as below where you have a specific business rule that applies. Then just remove id1 and id3 from your list of objects to render and only render the object that is returned from the following function.

CustomObject BizRule3293(IEnumerable<CustomObject> objects)
{
    CustomObject id1 = objects.SingleOrDefault(t => t.Id == 1);
    CustomObject id3 = objects.SingleOrDefault(t => t.Id == 3);
    if (id1 != null && id3 !=null)
    {
        if (!id1.IsPrivate && !id3.IsPrivate)
            return id1.Points > id3.Points ? id1 : id3;

        return id1.IsPrivate ? id3 : id1;
        // No logic stated if both are private
    }

    return id1 ?? id3;
}

Upvotes: 2

Related Questions