Business Logic – Organizing Complex Business Logic Rules

Architecturebusiness-logicorm

I have 20 entities and 50 user requests. And i have very complex business-logic (set of rules). How can i make my architecture so I'm not confused when adding new rules.

Now if i add property "InTheBattle" to fleet, i must consider this in 5 requestDispatchers and 3 methods in Verifications classes.
What will happen when i will have 100 entities and 500 user requests?

Maybe i must use some pattern or tool like excel? How can i contain all rules in my head? 🙂

Entity = orm model class

Sample of one request dispatcher:

public class CreateShipRequestHandler : IRequestHandler
{
    private RepoContainer Repo;

    private int mFleetId;
    private int mShipDesignId;

    public void CreateShipHandler(RepoContainer repo, int fleetId, int shipDesignId)
    {
        this.Repo = repo;

        this.mFleetId = fleetId;
        this.mShipDesignId = shipDesignId;
    }

    public bool Dispatch(out string message)
    {
        //INTERESTING PLACE:

        message = string.Empty;

        var fleet = Repo.FleetRepo.GetById(mFleetId);
        var shipDesign = Repo.ShipDesignRepository.GetById(mShipDesignId);

        if(fleet == null || shipDesign == null)
        {
            message = "VerificationError";
            return false;
        }

        var userResearchedScience = Repo.UserResearchedScienceRepo.GetByUserAndScience(fleet.UserId, shipDesign.RequiredScienceId);

        //I am using ShipVerification on the client too (for activate buttons)
        if(!ShipVerification.IsCanCreateShip(fleet, userResearchedScience, out message))
        {
            return false;
        }

        var newShip = new Ship(mFleetId, mShipDesignId);
        Repo.ShipRepo.AddOrUpdate(newShip);

        return true;
    }
}

Best Answer

You are worried about the combinatorial explosion, that could make your design difficult to maintain.

Combinatorial explosion

It's difficult to maintain all the relationships and dependencies between all these entities. The mediator pattern can help to sort this out: Entities, Requests, perhaps even Rules are colleagues, and the mediator encapsulates the interactions.

Rule engine

You have many business rules, and rules evolve often. The best approach is to adopt an architecture implementing a rule engine.

There are different ways to implement this. One way could be to use a chain of responsibility . You can find some examples of implementation on wikipedia.

Another way to implement the rule engine could be to use an event processor. An event (e.g. new entity is created) is first inserted in an event queue. The rule engine fires all the rules relevant for this event, and removes the event. Each rule could fire other events that are inserted in the event loop. Once the event queue is empty, all rules and their consequences are processed. Of course, a rule engine requires a little bit more intelligence (for example identify if rules are cylcing, tigerring each other endlessly). But it's already a good start.