This is a migrated thread and some comments may be shown as answers.

Need some help with building good code

6 Answers 97 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tevez
Top achievements
Rank 1
Tevez asked on 02 Aug 2011, 01:36 AM
Hi everyone,
Maybe not the smartest question, but i hope someone wants to write a few lines about this.
I'm new to this type of product, but i see it can help me a lot to write better structured code.

I have investigated all sample projects in the SDK and i really like the way some things work, but i need some help with this.
I've opened the "bestpracticewebdevelopment 1", it has the model, that is clear to me, the web projects also, but in between, the BusinessLayer, it has the BLL and the DAL folders.

What i see is that the BLL methods just call all the DAL methods. What is the use of this? Can these 2 be merged, or is that a strange idea?
And what if i want to keep it this way, where should i add extra logic? For instance, if i want to add a method that can select a list objects not just by ID, but with some more parameters. I have to add 2 methods? One to the BLL and one to the DAL? And would the BLL just pass on the parameters to the DAL to do the Linq?
What is the advantage of using these 2 separated methods?

Best Regards, Tevez

6 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 02 Aug 2011, 07:28 AM
Hi Tevez,

We use the exact architecture in our product based on WCF and there's a reason to stick with separated layers.

Like, in our architecture, we have:

WCF
BLL
Repository (DAL)

We consider the WCF layer only a way to expose the methods of the BLL to the "outside world"... It is in the BLL where the real work of the WCF service method takes place.
I prefer to call the methods in the BLL for a "business transaction", thus also the transaction demarcation for your OpenAccess Context / Scope. In a "business transaction" a workflow is executed that makes sense (and value) for the caller...

Consider a booking system. There might be a method in the BLL called "CreateBooking"
However, this method coordinates and delegates most of its work to the Repository and its methods for example:

When creating a booking:

1) The calender of the resource booked needs to be marked as occupied in the booked time frame.
2) Maybe we need to create an invoice and create entries on the account of the bookee
3) We need to send a confirmation E-mail to bookee for confirmation.
4) .....

The above is a workflow - that I prefer to call a "business transaction" - that executed in full makes sense (value) to the caller / your customer. The steps in the workflow is executed and coordinated by a BLL method, but the REAL work is delegated to a set of repository methods. For example:

1) BLL method will call ResouceRepository method MarkAsOccupied(resourceId, startDate, endDate)
2) BLL method will call AccountingRepository method CreateInvoice(payerId, resourceId, startDate, endDate)
3) BLL method will call DocumentGatewayRepository method SendBookingConfirmation(bookeeId);

Second, in respect to test driven development this is highly testable, because you can test the individual steps in the workflow and the workflow in full separately.

I know in the examples it *looks* like the two layers could easily be merged. Some BLL methods will only have one call to a repository method, but just a slightly more complex and it will have two or more.

In the end it is all matter of separation of concerns.

hope this shed some light on the issue.

Regards

Henrik



0
Tevez
Top achievements
Rank 1
answered on 02 Aug 2011, 08:09 AM
Hi Henrik,

Thanks you very much for your thoughts, this will help me understand a bit better i guess.
But, reading your 3 steps of example code i still have one big question and that concerns the .SaveChanges();
That is a database-transaction-commit, right? Now i see all methods in the DAL have a .SaveChanges(); , but how would do this in your scenario? Of course i first want to execute all 3 steps, (or at least 2) (MarkAsOccupied and CreateInvoice) and then i want to commit things to the database. Is there an easy and nice way to control these steps in combination with the database transactions?

Or did i get the whole .SaveChanges wrong?

Thanks again for helping me out!
Tevez
0
Accepted
IT-Als
Top achievements
Rank 1
answered on 02 Aug 2011, 08:19 AM
Hi Tevez,

Well, I don't know what the reasons in the example are for placing the SaveChanges method in the DAL.

However, as we used this concept, we consider the business transaction to be the OpenAccess transaction demarcation, too. Thus, you will not need to have SaveChanges in the DAL. You are correct that the SaveChanges will start a database transaction and commit the changes registered in the Context / Scope since you created it.

Like (pseudo code) in BLL

Context.Transaction.Begin (I know this is by default started automatically when you instantiate the Context)

// Do your calls to the repository here.... they will also work on the Model (your entities, with the same Context)
...
....

Context.SaveChanges();


0
IT-Als
Top achievements
Rank 1
answered on 03 Aug 2011, 12:47 PM
Hi Tevez,

Did it make sense to you?

Regards

Henrik
0
Tevez
Top achievements
Rank 1
answered on 16 Aug 2011, 10:21 AM
Hi Henrik,
I didn't see your last message here. But yes, it all makes sense to me. Your posts helped me quite a lot!
Thanks!
0
IT-Als
Top achievements
Rank 1
answered on 16 Aug 2011, 10:23 AM
Great, Tevez...

Glad to help... Please mark the thread as answered, so that other forum visitors can find a similar answer faster..

Thanks :-)
Tags
Getting Started
Asked by
Tevez
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Tevez
Top achievements
Rank 1
Share this question
or