locked
My design of a Business Layer and DAL Layer (please critique) RRS feed

  • Question

  • Hello to all. I'm a little a doubtful of my design of Business layer and Data access layer objects. Please critique. Here's the sample code:

    Code Snippet

    // Assuming we are registering / logging in a user to gain access to a web site.

    public class User  // Business entity
    {
        private string _username, _password;

        // assuming the fields already have getters and setters.
    }

    public static class UserManagement  // Business entity
    {
        public static boolean UserExists(string username); 
       

       public static void RegisterUser(User user)
        {
            if(!UserExists(user.Username))
            {
                UserData.AddUser(user);   // UserData == Data access object
            }
            else
            {
                // Notify the user of the error.
            }
        }
        public static User LogInUser(string username, string password)
        {
            // This method simulates the login process. Returns a user object if login is successful; Returns null otherwise.

            User user = UserData.GetUser(username, password);
           
            if(user != null)
                // successfull login.
                return user;
            else
                // failed login.
                return null;
        }

    }
    public static class UserData // Data access object
    {
        public static void AddUser(User user)
        {
            // Some ADO.NET code to add a user record to the database using the data given by the user object.
            dbCommand.CommandText = "INSERT INTO Users (Username, Password) VALUES (@Username, @Password)";
            dbCommand.Parameters.Add("@Username", user.Username);
            dbCommand.Parameters.Add("@Password", user.Password);
            dbCommand.ExecuteNonQuery();
        }
        public static User GetUser(string username, string password)
        {
            // This method connects to the database and check whether a user with the given username and password exists.

           if(datareader.HasRows())
            {
                // Returns a User object if found;
                User user = new User();
                user.Username = username;
                user.Password = password;
                return user;
            }
            else
                // Returns null otherwise
                return null;
        }
    }

     

    All critiques are welcome... Thanks in advance!

     

    - Raymond

     

    Sunday, July 8, 2007 12:42 PM

Answers

  • Hi;

    The code looks OK and the seperation between business and data access is there. However, I suggest that you look around for other solutions that are open source. The topic is not as easy is one might think and the key point is how fast and easy you can work with the framework (Handle changes in your database and business objects definitions). You need something that automates this for you.

     

    CodeSmith templates allow you to generate Business layer objects and generate their DataAcceess objects with a button click. It connects to the database and generate your classes. Many templates exist and each template employ a different technique to this problem. Also, some open source frameworks exist that handle this issue. I suggest that you take a look at this.

     

    Microsoft Entity Framework and LINQ is also targeting this issue. It's not yet released.

     

    When looking around the internet, search for Persistence Framework and Database Broker.

     

     

     

    Sunday, July 8, 2007 12:55 PM

All replies

  • Hi;

    The code looks OK and the seperation between business and data access is there. However, I suggest that you look around for other solutions that are open source. The topic is not as easy is one might think and the key point is how fast and easy you can work with the framework (Handle changes in your database and business objects definitions). You need something that automates this for you.

     

    CodeSmith templates allow you to generate Business layer objects and generate their DataAcceess objects with a button click. It connects to the database and generate your classes. Many templates exist and each template employ a different technique to this problem. Also, some open source frameworks exist that handle this issue. I suggest that you take a look at this.

     

    Microsoft Entity Framework and LINQ is also targeting this issue. It's not yet released.

     

    When looking around the internet, search for Persistence Framework and Database Broker.

     

     

     

    Sunday, July 8, 2007 12:55 PM
  • I like the code, but I think you could easily have all that functionality and more using code generators.

     

    I suggest you have a look at  the link below specially if you are generating against a SQL Server database. After you get the hang of it, it will be easy to create your own templates and support any engine you like.

    http://www.nettiers.net/

     

    Sunday, July 15, 2007 5:59 PM