Business Rules Implementation

Hi, how is the best way to implement business rules for my domain classes?
I know in the "set" part of a property i can use Regular Expressions and alike, but that is not the point, my point is if it could be better encapsulating business rules in clasess created for this purposes or should i mix the rules within the domain objects.

Putting it short and simple, in the example bellow i want to be sure that the variable departmentID is allways a 5 digits number:

public class Employee:Person
{
     private int departmentID;
     public int DepartmentID
    {
        get { return departmentID; }
        set { ??? }
    }
}
LVL 10
EBatistaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prajeebkumarCommented:
I think this is the best place to enforce that rule... this way you can always be sure there will never be an employee class with a department ID other than 5 digits.. if you put this rule somewhere else, may be there is a hack.. also, your business entity (employee) should know what data it can contain..  but something like creating an employee class criteria (may be you have Manager entity) should lie in your business manager or something like that... this is my 10 cents..
0
EBatistaAuthor Commented:
>I think this is the best place to enforce that rule
you meant the rule hardcoded in the set part or in a separate class
thanks for your comments prajeebkumar
0
prajeebkumarCommented:
Have a class which says rule.cs or bizRules.cs or something.. you can have all your business rules for biz entities defined there.. these rules could be reused by many entities.. say Manager and Employee entity could have the same rule of 5 digits dept ID, you don't have to code it twice..hope that makes sense.



0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

prajeebkumarCommented:
** this is of course if you have many rules and many entities.... otherwise, you can hard code it also, if not reused... it can be a static class.
0
EBatistaAuthor Commented:
then this will be ok?:

set
{
  if Rules.CheckDpoID(value)
  {
       departmentID=value;
  }
  else
      //notify
}
0
prajeebkumarCommented:
I think thats right on the money!!
0
devsolnsCommented:
It really depends on a number of things...

If you where using web services I would place as many of the business rules as I could in an XML schema (fyi xml schemas have facets that use regular expressions, lengths ect).  I would then use a custom soap extension to validate the input before it was even serialized and called the webmethod.  Since you'd need an xml schema to generate the wsdl this is hardly any extra work.

If you dont expect these rules to change ever or very often then I would implement them via code.  However if you expect changes could happen at any time perhaps you should keep these rules in a database or xml file.

If this is a multi-tiered platform make sure you keep all of the rules away from the user tier.  If changes are made it will have no impact on that part of the application and you wont need to re-deploy which is a cost in of itself.

I would not advise making a class called "BusinessRules".  It seems to work against OOP principals and would become a dumping ground for business rules with no clear autonomy.

-gary p.
0
EBatistaAuthor Commented:
hi devsolns, thanks for replaying, i agree with you that putting the business logic in the user tier is bad design.
Now,  if you does not support making a class called "BusinessRules", then which could be the best  approach to apply business rules that fulfill OOP principles?.
This is for a multi-tiered system.



0
devsolnsCommented:
EBatista,

How often do you expect these rules to change?

What cost is associated with this change (typically much more expensive given the size of an organization and how critical the application is)?

How do you access your middle tier? (Web services, .NET Remoting or else).
0
EBatistaAuthor Commented:
Well some rules changes often, others are the same for almost 15 years.
My architecture:

GUI (WinForm)
Domain clasess
Object Persistent Layer (Gentle.NET)
Database
0
prajeebkumarCommented:
As the above user stated, we can design the application to any extent we want. But depending on lot of factors (time, resources, expenses, criticality) you have to draw a line somewhere. I agree you probably shouldn't name your class as businessrules.cs. We once had a rule engine for one of our applications, where there was a rule engine factory that returns the proper rule engine for the entities etc. Of course, code level rule engine implies your rules don't change often. If you need that level of flexibility, you can even write your own customized rule engine enterpriselibrary application block which stores everything in xml configuration files and gives you a visual data editability. You just need to change the rules in the enterprise library GUI which has already been created for you by Microsoft.. But I'm not sure how complicated, flexible you want to make your application. You could also store your rules in xml files but then you need to manually edit them..  database is extra overhead since you may have to build another app to update rules.. probably xml files are the best.
0
devsolnsCommented:
I would compile some suggestions and get them to you soon..
0
EBatistaAuthor Commented:
ok thanks
0
devsolnsCommented:
Ok real quick, what sort of "business rules" are you talking about.

The your first post it looks like your are validating input for proper format.  So can the extent of your rules validation be for input formats/ranges and other facets or does your rules validation need to extend to deeper logic like such:

if(balance <= 0)
{
    //do this
}
else if(balance > 0 && balance < 10000)
{
   //do this
}
...


0
EBatistaAuthor Commented:
yes you are right, the rules can be more complex than the one i posted here, that was just for simplicity. My goal is to apply the business rules following best practices. I wonder if there exist any known pattern to implement business rules.
0
devsolnsCommented:
I guess the best "pattern" to follow is keep a tiered approach.  Data layer, business layer, presentation layer.  How strict you need to be regarding this depends on your application platform.  If its thick client then keep ALL business logic out of your presentation layer and use a well designed web service/remoting tier.  that will allow you to always make changes without having to recompile and deploy the presentation layer (keeeping in mind your not changing the interface.)  If its a think client like asp.net then its not so critical that it be another "physical tier" but do indeed keep it a logical tier by keeping logic out of presentation classes (pages, forms, controls).

also try to keep logic in resources that you can edit without recompile like database/xml/config.

as far as the industry goes when business logic gets crazy you should begin looking at a message bus like solution ie Microsoft Biztalk Server.  It allows for amazing business logic construction and change with little/no code.  it creates processes by using BPEL (business process execution language) that basically assembling services.

so this is hardddddddd one to answer because in this case, "it really does just depend."

oh by the way this just came to mind.  another good practice if you dont want another physicall tier to hold logic (web services) then keep all of your logic in seperate assemblies (.dll) that way if you make a logic change your only deploying a single dll instead of an entire install.

good luck.

-gary p.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.