1 Replies - 8818 Views - Last Post: 09 May 2010 - 09:45 AM Rate Topic: -----

#1 skylancer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-May 10

3 tier app, data access layer, business logic layer

Posted 08 May 2010 - 11:21 PM

hi friends,
I want to develop 3 tier app but I still confused how to implement it

1. In data access layer, could I return datareader? it's the better approach? some people tell it is not good way, you better used arrayList.

2. I have one method with 3 transaction inside (insert, update, delete)
I make this in business layer, so that the OleDBConnection (open and close), OleDbTransaction placed in business layer and I pass this as parameter to data access layer, is that good way?

3. if anyone have the good tutorial especially in C#, please tell me.

this is my sample code, please tell me + -

DAL
public static OleDbDataReader GetAll(OleDbConnection conn, bool status)
        {
            try
            {
                string query =
                    "SELECT [Id], [Nama], [CreatedDate], [CreatedBy], " +
                    "   [UpdatedDate], [UpdatedBy] " +
                    "FROM [Role] " +
                    "WHERE [Status] = @pStatus " +
                    "ORDER BY [Nama] ASC";

                OleDbCommand cmd = new OleDbCommand(query, conn);
                OleDbParameter pStatus = cmd.Parameters.Add("@pStatus", 
                    OleDbType.Boolean);
                pStatus.Value = status;
                OleDbDataReader dr = cmd.ExecuteReader();

                return dr;
            }
            catch (OleDbException ex)
            {
                throw new Exception("OleDb Error: " + ex.Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



BLL
public List<Role> GetAll(bool status)
        {
            List<Role> listRole = new List<Role>();

            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }

                OleDbDataReader dr = RoleData.GetAll(conn, status);

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Role role = new Role();
                        role.Id = int.Parse(dr["Id"].ToString());
                        role.Nama = dr["Nama"].ToString();
                        role.CreatedBy = dr["CreatedBy"].ToString();
                        role.CreatedDate = (string.Empty.Equals(dr["CreatedDate"].ToString()) ?
                            DateTime.MinValue :
                            DateTime.Parse(dr["CreatedDate"].ToString()));
                        role.UpdatedBy = dr["UpdatedBy"].ToString();
                        role.UpdatedDate = (string.Empty.Equals(dr["UpdatedDate"].ToString()) ? 
                            DateTime.MinValue : 
                            DateTime.Parse(dr["UpdatedDate"].ToString()));
                        listRole.Add(role);
                    }
                }

                return listRole;
            }
            catch (OleDbException ex)
            {
                throw new Exception("OleDb Error: " + "\n" + ex.Message);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                conn.Close();
            }
        }



Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: 3 tier app, data access layer, business logic layer

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: 3 tier app, data access layer, business logic layer

Posted 09 May 2010 - 09:45 AM

1) Returning a datareader is a bad idea, as this requires your BL layer to understand databases, how to read from them, and how to close it once it is done. It's better to set it up so that the DAL does all this, then passes back a collection of some sort containing the requested data. What type of structure depends on your data and how you want to use it.

2) Again, your BL knows too much about the database. What if you switched from an OLE database to a SqlServer, or some other database? You'd have to rewrite all your BL logic do deal with this. Your DAL should have methods to handle insert, update, delete and your BL should pass the data needed to those methods. This way if you revise your database, there is no change in the BL as it doesn't care what goes on in the DAL, it just knows "If I call this method, this thing happens".

3) I'm a fan of the Apress books, and if you decide to get one, go with the Expert whatever books. The expert books cover everything that is in the beginning books and more. If you are looking for an 3-Tier tutorial in C#, then try http://lmgtfy.com/?q...ication+in+C%23
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1