Injecting Business Rules ... possible?

Injecting Business Rules ... possible?

Old forum URL: forums.lhotka.net/forums/t/6797.aspx


raz0rf1sh posted on Wednesday, April 15, 2009

We have a situation where different roles that are editing an object follow a different set of business rules. For example, when entering a purchase order, Managers are authorized up to X$, while regulars Users are authorized up to X$, considerably less than managers.

My thought was if I could somehow inject the business rules I should be able to handle this? Just wondering if this is possible and interested in seeing how other users handle this situation.

ajj3085 replied on Wednesday, April 15, 2009

Injection might be fancy.. but I would just make the rule method take the users role into account when doing the check.

nermin replied on Wednesday, April 15, 2009

But what is preventing you to implement a custom validation method that inside checks for
(IPrincipal)user.IsInRole("Manager") and based on that compares to one max value or otherwise to another (one assigned to regular users).

After all this custom validation rules are nothing but a static methods that follow predefined signature.

Nermin



________________________________

From: raz0rf1sh [mailto:cslanet@lhotka.net]
Sent: Wed 4/15/2009 12:53 PM
To: Nermin Dibek
Subject: [CSLA .NET] Injecting Business Rules ... possible?



We have a situation where different roles that are editing an object follow a different set of business rules. For example, when entering a purchase order, Managers are authorized up to X$, while regulars Users are authorized up to X$, considerably less than managers.

My thought was if I could somehow inject the business rules I should be able to handle this? Just wondering if this is possible and interested in seeing how other users handle this situation.

RockfordLhotka replied on Wednesday, April 15, 2009

I agree with Nermin.

And if you want better factoring, there are several object-oriented techniques you could use - to avoid having a simple if..else structure in the rule method.

You could have the rule as the current user token (Identity) for its authorized amount.

Of you could have a PurchaseOrderAuthorizationManager object, and ask it the question.

Either of these latter two options could be using models where the authorized amount is stored in a different class based on the type of user, or where the amount was loaded from a config table in the database (because I assume these aren't hard-coded values).

tiago replied on Friday, April 17, 2009

I think injectiing business rules is a very interesting possibility. Suppose you have a standard application you sell to different clients and each one needs different business rules. Using injection you could do just that.

The point is: has anyone tryed it yet?

raz0rf1sh replied on Saturday, April 18, 2009

Thanks for all the feedback! I too was looking at trying to us DI to have custom rules ... I also thought it would be nice, if the rules were interfaced base ... to inject my own rules engine.

RockfordLhotka replied on Sunday, April 19, 2009

The problem with making rules interface based, is that you'd have to have a
class per rule. And that would prevent you from putting rule methods into
your business classes, which is often a nice technique.

You can make a pretty good OOD case that a rule is a thing with a
responsibility, so you should have to instantiate an object to invoke a
rule. But there's so much overhead and code limitation around that approach
that I decided not to go that way.

However, there's absolutely nothing stopping you from creating a generic
rule method that did instantiate an object to execute a rule. To make it
really generic, the rule method would accept some "rule name" that your DI
or IoC framework would translate into an actual type so you could create an
instance of that type. But that's easily done with a specific RuleArgs
subclass.

Rocky

-----Original Message-----
From: raz0rf1sh [mailto:cslanet@lhotka.net]
Sent: Saturday, April 18, 2009 7:09 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Injecting Business Rules ... possible?

Thanks for all the feedback! I too was looking at trying to us DI to have
custom rules ... I also thought it would be nice, if the rules were
interfaced base ... to inject my own rules engine.

Copyright (c) Marimer LLC