locked
Business Logic Layer Help RRS feed

  • Question

  • User-1498408751 posted

    Hello,
    I have been using ASP.NET for a few years now, but have never took up OOP, and this is something I should really pickup. I understand the basic concepts but am having difficulty with the business logic layer.
    I have a page I am working on that pulls some data out of the database, and was wondering if anyone could guide me in creating my page the proper way. Here's how I was doing it:
    For my UI I had a label called lblNav
    On the code behind I had the following to put the data on the page:
    Dim code As String
                code = "<ul class='tabs'>"
                Dim sqlConn3 As New SqlConnection()
                sqlConn3.ConnectionString = ConfigurationManager.ConnectionStrings("DemoConnectionString").ConnectionString
                Dim cmd3 As New SqlCommand
                cmd3.CommandText = "SELECT * FROM [Content] INNER JOIN [Map] ON Content.id=Map.id WHERE active=1 ORDER BY Title ASC"
                cmd3.Connection = sqlConn3
                sqlConn3.Open()
                cmd3.ExecuteNonQuery()
                Dim reader3 As SqlDataReader = cmd3.ExecuteReader()
                While reader3.Read()
                    code = code & "<li class='tabs'><a href='./page.aspx?id="
                    code = code & reader3("id")
                    code = code & "'>" '<div class='nav'>"
                    code = code & reader3("Title")
                    code = code & "</div></a></li>"
                End While
                code = code & "</ul>"
                lblNav.Text = code
                reader3.Close()
                sqlConn3.Close()
    When re-developing the page using OOP I have the SQL statement in the Data Access Layer: ContentTableAdapter > Fill, GetData()
    But I have no idea how to tackle the BLL - any suggestions or help is greatly appreciated.
    Thanks

    Hello,

    I have been using ASP.NET for a few years now, but have never took up OOP, and this is something I should really pickup. I understand the basic concepts but am having difficulty with the business logic layer.

    I have a page I am working on that pulls some data out of the database, and was wondering if anyone could guide me in creating my page the proper way. Here's how I was doing it:


    For my UI I had a label called lblNav

    On the code behind I had the following to put the data on the page:

    Dim code As String
    
                code = "<ul class='tabs'>"
    
                Dim sqlConn3 As New SqlConnection()
    
                sqlConn3.ConnectionString = ConfigurationManager.ConnectionStrings("DemoConnectionString").ConnectionString
    
                Dim cmd3 As New SqlCommand
    
                cmd3.CommandText = "SELECT * FROM [Content] INNER JOIN [Map] ON Content.id=Map.id WHERE active=1 ORDER BY Title ASC"
    
                cmd3.Connection = sqlConn3
    
                sqlConn3.Open()
    
                cmd3.ExecuteNonQuery()
    
                Dim reader3 As SqlDataReader = cmd3.ExecuteReader()
    
                While reader3.Read()
    
                    code = code & "<li class='tabs'><a href='./page.aspx?id="
    
                    code = code & reader3("id")
    
                    code = code & "'>" '<div class='nav'>"
    
                    code = code & reader3("Title")
    
                    code = code & "</div></a></li>"
    
                End While
    
                code = code & "</ul>"
    
                lblNav.Text = code
    
                reader3.Close()
    
                sqlConn3.Close()


    When re-developing the page using OOP I have the SQL statement in the Data Access Layer: ContentTableAdapter > Fill, GetData()

    But I have no idea how to tackle the BLL - any suggestions or help is greatly appreciated.

    Thanks

    Tuesday, February 16, 2010 11:11 AM

Answers

  • User-952121411 posted

    The best approach at this point since you are quite new to this design may be to download the full working code from the tutorials, and debug and play around with the code to see how it works, before immediately applying anything to your own application.  Several of the questions you are asking are quite specific, and a lot of detail is missing so it is going to be difficult to iron out every single issue.

    I recommend downloading the code (the link is at the beginning of each tutorial).  As you step through the code you will see how it works better and then be able to better apply the principals to your application. 

    The code by the way whiting those tutorials using table adapters, etc is not exactly the way you have to make your code; the purpose is just to show the different responsibilities of each layer and how they communicate.  You original question stemmed from "What is the purpose of the BLL?"  Those tutorials are best meant to help you understand the purpose of the layers, and then apply some of the techniques to your own application. 

    Lastly, take things in small steps; if you are still trying to grasp the different logical layers, make changes a little at a time instead of ripping out all of your code trying to make your app look like someone else's.  Because if you don't understand what is going on, you will run into a lot of headaches and issues.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2010 3:24 PM

All replies

  • User-952121411 posted

    Welcome to the world of OOP! Laughing  I can see from your code that it appears everything was right in the code behind file in the UI.  It is correct to begin parsing out portions of your application into a UI-BLL-DAL architecture to start with.  Typically in a nutshell the DAL will extract the data and package it up, the BLL will manipulate the data based on business needs and potentially create business objects based on the logical model and not necessarily the one of the physical database structure, and the UI will consume and bind the data.  Of course there is a lot more going on than just these parts, but these are the high points.  The BLL specifically isolates the logic of the application and should be independent of the UI and database technologies. This will allow you to change the way the app communicates to the database or binds to the UI without effecting the primary logic of the application.  The BLL acts as the intermediary for communication between the (2) layers. 

    Below are (2) decent tutorials written on creating a BLL and a DAL.  Take a look at them, and after understanding the purpose better, then apply that to your individual application.

    Creating a Business Logic Layer:

    http://www.asp.net/learn/data-access/tutorial-02-vb.aspx

    Creating a Data Access Layer:

    http://www.asp.net/learn/data-access/tutorial-01-vb.aspx

    Hope this helps! Smile

    Tuesday, February 16, 2010 3:06 PM
  • User-2074625069 posted

    Its a good idea to start with seperating your concerns, you could split your code up like so:

    // Data Transfer Object
    public class Content
    {
        public string Id { get; set; }
        public string Title { get; set; }
    }
    
    // Data Access Object
    public class DAO
    {
        public IEnumerable<Content> GetAllContent()
        { 
            IList<Content> content = new List<Content>();
    
            using (SqlConnection conn = new SqlConnection() )
            {
                conn.ConnectionString = ConfigurationManager.ConnectionStrings("DemoConnectionString").ConnectionString;
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM [Content] INNER JOIN [Map] ON Content.id=Map.id WHERE active=1 ORDER BY Title ASC" ;
    
                conn.Open();
    
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        content.Add(new Content { Id = reader["id"].ToString(), Title = reader["Title"].ToString() });
                    }
                }
    
            }
    
            return content; 
        }
    }
    
    
    // Business Service
    public class ContentService
    {
        private DAO _dao;
    
        public ContentService(DAO dao)
        {
            _dao = dao;
        }
    
        public IEnumerable<Content> GetAllContent()
        { 
            // Can first check the cache for the content..
            return _dao.GetAllContent(); 
        }
    }


     Then you could have a simple repeater on your page to format the presentation again like so:

            <asp:Repeater ID="Repeater1" runat="server">
                <HeaderTemplate>
                    <ul class='tabs'>
                </HeaderTemplate>
                <ItemTemplate>
                    <li class='tabs'><a href='./page.aspx?id=<%# Eval("Id") %>'><div class='nav'><%#Eval("Title") %></div></a></li>
                </ItemTemplate> 
                <FooterTemplate>
                    </ul>
                </FooterTemplate> 
            </asp:Repeater>


     

     

    Tuesday, February 16, 2010 5:44 PM
  • User-1498408751 posted

    Thanks for the tips and resources. I have been playing around with the tutorials to get familiar with all of this OOP stuff and I am stuck on the DAL article at:

    Imports NorthwindTableAdapters Partial Public Class Northwind Partial Public Class SuppliersRow Public Function GetProducts() As Northwind.ProductsDataTable Dim productsAdapter As New ProductsTableAdapter Return productsAdapter.GetProductsBySupplierID(Me.SupplierID) End Function End Class End Class


    I am trying to adapt all of this to my own code so I can learn better. Here's what I have

    In my DAL I have "MenuTableAdapter" with Fill, GetData() and FillByDeptId,GetDataByDeptId(@DeptId)

    For my Class called "NavMenu.vb I have the following:

    Imports NavTableAdapters
    
    Public Class NavMenu
        Partial Public Class Menu
            Partial Public Class MenuRow
                Public Function GetItems() As NavTableAdapters.MenuTableAdapter
                    Dim MenuAdapter As New MenuTableAdapter
                    Return MenuAdapter.GetDataByDeptId() 'This generates Argument Not Specified for paramater DeptId
                End Function
            End Class
        End Class
    End Class
    ' I also don't understand why I am creating so many different Classes - I understand the idea behind the function


    Any help/explanations are appreciated. Thanks!

    Thursday, February 18, 2010 12:23 PM
  • User-952121411 posted

    In my DAL I have "MenuTableAdapter" with Fill, GetData() and FillByDeptId,GetDataByDeptId(@DeptId)
     

    As you can see the 'GetDataByDeptId()' method accepts a parameter named 'DeptID' which your code on the follwoing line is not passing anything into the method:

    Return MenuAdapter.GetDataByDeptId()


     

    Friday, February 19, 2010 11:58 AM
  • User-1498408751 posted

    That makes sense, but when I add in DeptId I get Name is Not Declared.

    Even if I add in 1 just to test it I get "Value of type 'Nav.MenuDataTable' cannot be converted to 'NavTableAdapters.MenuTableAdapter'." -> No idea what this means
     

    Return MenuAdapter.GetDataByDeptId(1) 



    Friday, February 19, 2010 3:09 PM
  • User-952121411 posted

    The best approach at this point since you are quite new to this design may be to download the full working code from the tutorials, and debug and play around with the code to see how it works, before immediately applying anything to your own application.  Several of the questions you are asking are quite specific, and a lot of detail is missing so it is going to be difficult to iron out every single issue.

    I recommend downloading the code (the link is at the beginning of each tutorial).  As you step through the code you will see how it works better and then be able to better apply the principals to your application. 

    The code by the way whiting those tutorials using table adapters, etc is not exactly the way you have to make your code; the purpose is just to show the different responsibilities of each layer and how they communicate.  You original question stemmed from "What is the purpose of the BLL?"  Those tutorials are best meant to help you understand the purpose of the layers, and then apply some of the techniques to your own application. 

    Lastly, take things in small steps; if you are still trying to grasp the different logical layers, make changes a little at a time instead of ripping out all of your code trying to make your app look like someone else's.  Because if you don't understand what is going on, you will run into a lot of headaches and issues.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 19, 2010 3:24 PM