[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Business Rules Implementation

Posted on 2006-04-06
16
Medium Priority
?
552 Views
Last Modified: 2008-03-17
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 { ??? }
    }
}
0
Comment
Question by:EBatista
  • 6
  • 5
  • 5
16 Comments
 
LVL 3

Expert Comment

by:prajeebkumar
ID: 16392132
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
 
LVL 10

Author Comment

by:EBatista
ID: 16392310
>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
 
LVL 3

Expert Comment

by:prajeebkumar
ID: 16392421
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:prajeebkumar
ID: 16392494
** 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
 
LVL 10

Author Comment

by:EBatista
ID: 16392711
then this will be ok?:

set
{
  if Rules.CheckDpoID(value)
  {
       departmentID=value;
  }
  else
      //notify
}
0
 
LVL 3

Expert Comment

by:prajeebkumar
ID: 16392744
I think thats right on the money!!
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16393903
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
 
LVL 10

Author Comment

by:EBatista
ID: 16394514
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
 
LVL 13

Expert Comment

by:devsolns
ID: 16395469
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
 
LVL 10

Author Comment

by:EBatista
ID: 16396485
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
 
LVL 3

Assisted Solution

by:prajeebkumar
prajeebkumar earned 400 total points
ID: 16396566
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
 
LVL 13

Expert Comment

by:devsolns
ID: 16408765
I would compile some suggestions and get them to you soon..
0
 
LVL 10

Author Comment

by:EBatista
ID: 16417219
ok thanks
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16430212
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
 
LVL 10

Author Comment

by:EBatista
ID: 16439653
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
 
LVL 13

Accepted Solution

by:
devsolns earned 400 total points
ID: 16449928
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question