Reply to topic  [ 6 posts ] 
Any developers... 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
A question that I've come across today that I thought I'd put out there.

When separating Business Logic and Data Access at what point does data access become business logic?

i.e. If I want to get all of the customers for region 2 and their total sales for the past month. Does data access mean getting all of the customers and then the Logic goes through the customers and goes to the DA for the sales for each customer and the BL then calculate the total.

Or does the DA get the customers and then the BL go through each customer and then go back to the DA to get a pre calculated total for each customer.

Or does the BL go to the DA to get the list of customers and a value for each customer in one go?

In all three cases the DA layer is only accessing the data but where do you draw the line?

Any help?

Thanks!

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Aug 06, 2009 8:09 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
Hmm, the DB should be used to do the summing up...

The BL should collect the list of customers that need to be reported on and pass them as a parameter to the database layer, which will execute the relevant stored procedure, with the list of customers (or null list for all customers)... The stored procedure (or alternatively, if you aren't using SPs, a query built in the DB access layer) should then retrieve the sum of the sales for each customer - should just be a single query - and returns the data to the BL, which then fills out the collection of customers.

Accessing the database once for each customer, or worse once for each sale for each customer, is a very poor way to do it, it increases the amount of IO, processing and memory that would be needed...

The BL and DAL need to be a little flexible to fully optimise the process. You really need to think about how you will need to access the data, in order to minimise the number of transactions and maximise performance.

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Thu Aug 06, 2009 8:48 pm
Profile ICQ
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
I think the flexibility is the key here.

At the moment our BL and DA is mixed into one lump with logic and data access throughout.

I think the trick is balancing small IO traffic and fewest database reads with code reusability.

I suppose if there is a particular data read that is performed over and over then this needs to be put into a DA layer so that the BL can reuse the code.

I think the trick is also in getting the parameters passed into the DAL correct. I.e. do you go to the DAL for each customer or do you go to the DAL once with a list of customers and get a list of output values, etc... (Thus allowing a single customer query (list of 1), multiple customer query or all customer query (empty list)).

Thanks for the tips

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Aug 06, 2009 8:57 pm
Profile WWW
What's a life?
User avatar

Joined: Thu Apr 23, 2009 8:25 pm
Posts: 10691
Location: Bramsche
Reply with quote
You should go to the DAL once, with a list of customers to return.

I tried it the other way round on a web site recently, which loaded the customer, then loaded a delivery and each product on the delivery that was in the studio and each movement of that product inside the studio. When testing, it was fine, but when it went live and they had a couple of hundred products on an order and half a dozen movements. The server pretty much died, because the number of reads a second exceeded 4000, for over 10 seconds, then started blowing memory limits.

Loading all the products in one read, then loading all the histories for each product individually solved the load problems. In fact we didn't load any history, unless it was specifically needed, the load became an extra method, as opposed to being part of the class instantiation.

Due to the way I had written the system, switching from loading everything in one hit, as opposed to individual reads took around half an hour, for around 50 classes. Likewise, we further reduced the load by having "paging" parameters: There was no point loading 250 products and their movement information, if the system is only display 10 of them.

_________________
"Do you know what this is? Hmm? No, I can see you do not. You have that vacant look in your eyes, which says hold my head to your ear, you will hear the sea!" - Londo Molari

Executive Producer No Agenda Show 246


Fri Aug 07, 2009 4:30 am
Profile ICQ
Site Admin
User avatar

Joined: Fri Apr 24, 2009 6:12 am
Posts: 7011
Location: Wiltshire
Reply with quote
big_D wrote:
There was no point loading 250 products and their movement information, if the system is only display 10 of them.


That is an incredible valid point that is usually missed completely as the UI is the last thing that is devised or even worse is designed separately.

_________________
<input type="pickmeup" name="coffee" value="espresso" />


Fri Aug 07, 2009 6:50 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
AlunD wrote:
big_D wrote:
There was no point loading 250 products and their movement information, if the system is only display 10 of them.


That is an incredible valid point that is usually missed completely as the UI is the last thing that is devised or even worse is designed separately.

It is something we used to do (before my time).

Now we have a variable that can be passed into a query that determines the number of records it gets each time.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Aug 07, 2009 10:19 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.