Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

(Note: The examples are simplified)

I've seperated my solution into 3 projects

  • WebSite
  • Business Logic
  • Data Access Logic

The Website has access to BL and DAL
The BL has access to nothing
The DAL has access to the BL

On the website I can do something like

BL.User user = DAL.GetUser(1)

I also have this type of logic in the website

user = [new user information]

If Not DAL.DoesEmailExists(user) Then
    DAL.InsertNewUser(user)
End If

What would be my option to remove this if/insert from the website?
My ideal option would be to put this logic inside the BL.User but that's not possible. If the BL has access to the DAL it will make a circular reference.

share|improve this question
    
which platform is it? –  Sunny Mar 11 '13 at 20:09
3  
What if you put it into the DAL? Something like DAL.InsertOrUpdateUser(user)? –  Dan Pichelman Mar 11 '13 at 20:17
    
@Sundeep I added the platform –  the_lotus Mar 11 '13 at 20:22
add comment

2 Answers

up vote 2 down vote accepted

This type of code actually belongs in the Business Layer, from where you control the behavior of your application, make decisions about inserting an object or displaying an alert to the user, etc.,

// BL
If Not DAL.Exists(user) Then
    DAL.InsertNewUser(user)
Else
    // Maybe warn the user about inserting a duplicate user object
    WL.Alert("User already exists!") 
End If

You BL should then have access to both the DAL and the WL, and your DAL should not have a dependency on the BL (it only knows how to retrieve data on demand). Do not confuse a Business Object (BO), which can also be a Data Transfer Object (DTO), with a BL: a BO is simply for transferring or passing data from layer to layer.

share|improve this answer
    
That's exactly the mistake I made, thanks. –  the_lotus Mar 11 '13 at 21:26
add comment

I assume its ASP.NET webforms, go through building layered applications series by Imar Spaanjaars which explains same concept you've described. In short, below is the approach.

Presentation Layer

From aspx.cs:

var objCustomer = CustomerBLL.GetCustomerById(id);
if(objCustomer != null)
{
  //assign values to UI controls
}

In Business Logic Layer

public static class CustomerBLL
{
  public static Customer GetCustomerById(int id)
  {
    //call database layer
    return CustomerDAL.GetCustomerById(id);
  }
}

In Data Access Layer

public static class CustomerDAL
{
  public static Customer GetCustomerById(int id)
  {
    Customer objCustomer = new Customer();
    //use ADO.NET or any data access methodology to retrieve data
    //and assign properties of objCustomer
    return objCustomer;
  }
}

Business Objects

These are just DTO's that are used to pass data between layers. It gets referenced in both Business Logic and Data Access Layers.

public class Customer
{
   public string Name {get;set;}
  //other properties
}

IMO, it works well with small to medium scale applications.

Edit

Depending upon your code, both layers DAL and BLL are getting called from presentation layer. So there is no right answer, unless we know responsibility of each layer and how it is designed.

On the website I can do something like

BL.User user = DAL.GetUser(1)

I also have this type of logic in the website

user = [new user information]

If Not DAL.DoesEmailExists(user) Then
    DAL.InsertNewUser(user)
End If

From existing code, as mentioned in the comments section, try this way:

Website

DAL.UpsertUser(objUser);

In store proc:

IF EXISTS(select 1 FROM users where userid = @userid)
BEGIN
    --insert
END
ELSE
BEGIN
   --update
END
}

It works for both insert/update.

share|improve this answer
    
This is good general advice, but what is the answer to the OP's specific question? –  Robert Harvey Mar 11 '13 at 20:56
    
You made me realized that I only created 2 layers since my Business Logic is actually the Business Object (DTO). –  the_lotus Mar 11 '13 at 20:59
    
@RobertHarvey, the reason I'd this advice since I realized there is no proper separation of concerns in the presented code. Both BL and DAL layers are called from the website. But I accept this is not the solution. –  Sunny Mar 11 '13 at 21:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.