3 Tier Architecture - In need of an example

前端 未结 9 1457
孤城傲影
孤城傲影 2020-12-12 13:53

Presently I am working using single tier architecture. Now I am wanting to learn how to write code using 3 tier architecture. Please can you provide me with a simple example

相关标签:
9条回答
  • 2020-12-12 14:27
    connection class
    -----------------------
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web .UI.WebControls ;
    /// <summary>
    /// Summary description for conn
    /// </summary>
    namespace apm_conn
    {
        public class conn
        {
            public SqlConnection getcon()
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString );
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                return con;
            }
            #region execute command
            public string  Executecommand(SqlParameter []sqlparm,string sp)
            {
                string r_val = "";
                try
                {
    
                    SqlConnection con = new SqlConnection();
                    con = getcon();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = sp;
                    cmd.CommandType = CommandType.StoredProcedure;
                    foreach (SqlParameter loopvar_parm in sqlparm)
                    {
                        cmd.Parameters.Add(loopvar_parm);
    
                    }
                    cmd.Parameters.Add("@Var_Output", SqlDbType.VarChar, 20).Direction = ParameterDirection.Output;
                    cmd.ExecuteNonQuery();
                    r_val = (string)cmd.Parameters["@Var_Output"].Value;
                    con.Close();
                }
                catch { }
                return r_val;
    
            }
    
            #endregion
            #region Execute Dataset
             public DataSet ExeccuteDataset(SqlParameter[] sqlParm, string sp)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlConnection con = new SqlConnection();
                con = getConn();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = sp;
                foreach (SqlParameter LoopVar_param in sqlParm)
                {
                    cmd.Parameters.Add(LoopVar_param);
                }
                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
    
            }
            catch
            { }
            return ds;
        }
            #endregion
            #region grid
            public void Bindgrid(DataSet ds,GridView g)
            {
                try
                {
                    g.DataSource = ds.Tables[0];
                    g.DataBind();
    
                }
                catch { }
            }
            #endregion
            #region Dropdownlist
            public void Binddropdown(DropDownList dl,DataSet ds,string text,string value)
            {
                try
                {
                    dl.DataSource = ds.Tables[0];
                    dl.DataTextField = text;
                    dl.DataValueField = value;
                    dl.DataBind();
                }
                catch
                {}
    
            }
            #endregion
            public conn()
            {
                //
                // TODO: Add constructor logic here
                //
            }
        }
    }
    
    
    
    
    dal
    ---------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using apm_conn;
    using System.Data.SqlClient;
    using apm_ent;
    
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    namespace apm_dal
    {
        public class dal
        {
            conn ob_conn = new conn();
            public dal()
            {
                //
                // TODO: Add constructor logic here
                //
            }
            public string insert(ent obj_ent)
            {
                SqlParameter[] sqlparm =
            {
                new SqlParameter ("@Var_Action",obj_ent.Var_Action),
                new SqlParameter ("@Int_Id",obj_ent.Int_Id ),
                 new SqlParameter ("@Var_Product",obj_ent.Var_Product ),
                  new SqlParameter ("@Dc_Price",obj_ent.Var_Price ),
                  new SqlParameter ("@Int_Stat",obj_ent.Int_Stat ),
    
                                   };
                return ob_conn.Executecommand(sqlparm, "Proc_product");
            }
            public string ins(ent obj_ent)
            {
                SqlParameter[] parm =
            {
                new SqlParameter ("@Var_Action",obj_ent .Var_Action),
                new SqlParameter ("@Int_Id",obj_ent .Int_Id),
                                };
                return ob_conn.Executecommand(parm, "Proc_product");
            }
        }
    
    }
    
    bal
    -------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using apm_ent;
    using apm_dal;
    
    /// <summary>
    /// Summary description for bal
    /// </summary>
    namespace apm_Bal
    {
    
        public class bal
        {
            dal ob_dal = new dal();
            string r_val = "";
            public bal()
            {
                //
                // TODO: Add constructor logic here
                //
            }
            public string insert(ent obj_ent)
            {
                return ob_dal.insert(obj_ent);
            }
        }
    }
    
    
    
    Ent
    ------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for ent
    /// </summary>
    namespace apm_ent
    {
        public class ent
        {
            public ent()
            {
                //
                // TODO: Add constructor logic here
                //
            }
            #region Ent
            public int Int_Id { get; set; }
            public string  Var_Action { get; set; }
            public string Var_Product { get; set; }
            public decimal  Var_Price { get; set; }
            public int Int_Stat { get; set; }
            #endregion
        }
    }
    
    
    
    page code
    --------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using apm_conn;
    using apm_ent;
    using apm_Bal;
    using apm_conn;
    public partial class _Default : System.Web.UI.Page
    {
        conn obj_conn = new conn();
        ent obj_ent = new ent();
        bal obj_bal = new bal();
        string r_val = "";
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void btnsub_Click(object sender, EventArgs e)
        {
            obj_ent.Var_Action = "INS";
            obj_ent.Var_Product = txtproduct.Text;
            obj_ent.Var_Price = Convert.ToDecimal (txtprice.Text);
            r_val = obj_bal.insert(obj_ent);
            if (r_val == "1")
            {
                Response.Write("<script>alert('Inserted Sucessfully')</script>");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 14:29

    Three-tier (layer) is a client-server architecture in which the user interface, business process (business rules) and data storage and data access are developed and maintained as independent modules or most often on separate platforms.

    Basically, there are 3 layers:

    • tier 1 (presentation tier, GUI tier)
    • tier 2 (business objects, business logic tier)
    • tier 3 (data access tier). These tiers can be developed and tested separately.

    What is the need for dividing the code in 3-tiers? Separation of the user interface from business logic and database access has many advantages. Some of the advantages are as follows:

    Reusability of the business logic component results in quick development. Let's say we have a module that handles adding, updating, deleting and finding customers in the system. As this component is developed and tested, we can use it in any other project that might involve maintaining customers.

    Transformation of the system is easy. Since the business logic is separate from the data access layer, changing the data access layer won’t affect the business logic module much. Let's say if we are moving from SQL Server data storage to Oracle there shouldn’t be any changes required in the business layer component and in the GUI component.

    Change management of the system is easy. Let's say if there is a minor change in the business logic, we don’t have to install the entire system in individual user’s PCs. E.g. if GST (TAX) is changed from 10% to 15% we only need to update the business logic component without affecting the users and without any downtime.

    Having separate functionality servers allows for parallel development of individual tiers by application specialists.

    Provides more flexible resource allocation. Can reduce the network traffic by having the functionality servers strip data to the precise structure needed before sending it to the clients.

    0 讨论(0)
  • 2020-12-12 14:30

    Wikipedia have a nice explanation: Multitier architecture:

    'Three-tier' is a client-server architecture in which the user interface, functional process logic ("business rules"), computer data storage and data access are developed and maintained as independent modules, most often on separate platforms.

    alt text

    Web development usage

    In the web development field, three-tier is often used to refer to websites, commonly electronic commerce websites, which are built using three tiers:

    • A front end web server serving static content, and potentially some cached dynamic content.
    • A middle dynamic content processing and generation level application server, for example Java EE, ASP.net, PHP platform.
    • A back-end database, comprising both data sets and the database management system or RDBMS software that manages and provides access to the data.
    0 讨论(0)
  • 2020-12-12 14:30

    A 3-tier architecture usually has the following components:

    1. Client Browser
    2. Web server hosting the ASP.NET application
    3. Some backend storage such as database that is being accessed by the ASP.NET application

    So to answer your question on how to write code for a 3-tier architecture, you develop an ASP.NET application that communicates with a data storage.

    0 讨论(0)
  • 2020-12-12 14:32

    3-tier architecture can have different meanings depending on context. Generally it means that responsibilities in the application are divided between different tiers. Typically, 3-tier refers to :

    • presentation tier" (actual user interface)
    • logic tier (application/business logic)
    • data tier (database, data storage)

    The details vary by application.

    Wikipedia, as usual, has a nice overview: http://en.wikipedia.org/wiki/Multitier_architecture

    A simple example would be a typical business app:

    • presentation: browser, or fat client
    • logic tier: business logic, typically in an application server (based on J2EE, ASP.NET or whatever)
    • data tier: a database, typically a RDBMS such as MySQL or Oracle
    0 讨论(0)
  • 2020-12-12 14:34

    Presentation layer: put everything that is related to user interface. (What the user sees)

    Business layer: everything that is related to the logic of the application (How is the information coming from presentation layer treated)

    Data layer: provide an abstraction of the underlying data source(s) (Where and how the information coming from/going to business layer is stored)

    Each layer should know as less as possible about the other and it should be a top down approach:

    • the data layer should know nothing about business and presentation
    • business layer should know about data but not about presentation
    • presentation should know about business but not about data

    Simple example:

    Website:

    • Presentation: all the graphical things, fields where user inserts data, menus, pictures, etc.
    • Business: all constraints about the data (unique name, name without symbols, valid date, etc), methods for manipulating business objects (create new user, add new order, etc)
    • Data: Methods that access the underlying database.
    0 讨论(0)
提交回复
热议问题