目前我正在使用单层架构进行工作。现在我想学习如何使用三层架构编写代码。请问能否提供一个简单的示例?
目前我正在使用单层架构进行工作。现在我想学习如何使用三层架构编写代码。请问能否提供一个简单的示例?
'三层'是一种客户端-服务器体系结构,其中用户界面、功能过程逻辑(“业务规则”)、计算机数据存储和数据访问以独立模块的形式开发和维护,通常在不同的平台上。
Web开发应用
在Web开发领域中,“三层”通常用于指建立在三个层次上的网站,通常是电子商务网站:
- 前端Web服务器提供静态内容,并可能提供一些缓存的动态内容。
- 中间动态内容处理和生成级别的应用服务器,例如Java EE、ASP.net、PHP平台。
- 后端数据库,包括数据集和管理和提供对数据访问的数据库管理系统或RDBMS软件。
这是我项目中的内容,不仅仅是传统的3层架构。
1.) Application.Infrastructure
2.) Application.DataModel
3.) Application.DataAccess
4.) Application.DomainObjects
5.) Application.BusinessLayer
6.) Application.WebClient 或 Application.WindowsClient
Application.BusinessObjects 在整个应用程序中都被使用,并且在任何数据访问操作中以业务对象的形式返回或接收 [除了 Application.DataModel 和 Application.Infrastructure]。
我的所有查询都只在 Application.DataModel 中定义。
Application.DataAccess 返回或接收业务对象作为任何数据访问操作的一部分。使用反射属性创建业务对象。每个业务对象都标记有映射到数据库目标表的属性,业务对象内的属性使用属性标记映射到相应的数据表列。
我的验证框架允许我使用指定的 ValidationAttribute 验证每个字段。
我的框架大量使用属性来自动化大多数繁琐任务,例如映射和验证。我还可以将新功能作为框架中的新方面。
在我的应用程序中,样例业务对象看起来像这样。
User.cs
[TableMapping("Users")]
public class User : EntityBase
{
#region Constructor(s)
public AppUser()
{
BookCollection = new BookCollection();
}
#endregion
#region Properties
#region Default Properties - Direct Field Mapping using DataFieldMappingAttribute
private System.Int32 _UserId;
private System.String _FirstName;
private System.String _LastName;
private System.String _UserName;
private System.Boolean _IsActive;
[DataFieldMapping("UserID")]
[DataObjectFieldAttribute(true, true, false)]
[NotNullOrEmpty(Message = "UserID From Users Table Is Required.")]
public override int Id
{
get
{
return _UserId;
}
set
{
_UserId = value;
}
}
[DataFieldMapping("UserName")]
[Searchable]
[NotNullOrEmpty(Message = "Username Is Required.")]
public string UserName
{
get
{
return _UserName;
}
set
{
_UserName = value;
}
}
[DataFieldMapping("FirstName")]
[Searchable]
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}
[DataFieldMapping("LastName")]
[Searchable]
public string LastName
{
get
{
return _LastName;
}
set
{
_LastName = value;
}
}
[DataFieldMapping("IsActive")]
public bool IsActive
{
get
{
return _IsActive;
}
set
{
_IsActive = value;
}
}
#region One-To-Many Mappings
public BookCollection Books { get; set; }
#endregion
#region Derived Properties
public string FullName { get { return this.FirstName + " " + this.LastName; } }
#endregion
#endregion
public override bool Validate()
{
bool baseValid = base.Validate();
bool localValid = Books.Validate();
return baseValid && localValid;
}
}
BookCollection.cs
/// <summary>
/// The BookCollection class is designed to work with lists of instances of Book.
/// </summary>
public class BookCollection : EntityCollectionBase<Book>
{
/// <summary>
/// Initializes a new instance of the BookCollection class.
/// </summary>
public BookCollection()
{
}
/// <summary>
/// Initializes a new instance of the BookCollection class.
/// </summary>
public BookCollection (IList<Book> initialList)
: base(initialList)
{
}
}
演示层
业务逻辑层(BLL)
数据访问层(DAL)
展示层:包括与用户界面相关的所有内容。 (用户看到了什么)
业务层:与应用程序逻辑相关的所有内容(来自展示层的信息如何处理)
数据层:提供底层数据源的抽象层(来自/去往业务层的信息存储在哪里和如何存储)
每个层次应尽可能少地了解其他层次,并且它应该是自上而下的方法:
简单的例子:
网站:
具体细节因应用而异。
维基百科有一个很好的概述:http://en.wikipedia.org/wiki/Multitier_architecture
一个简单的例子就是一个典型的业务应用:
3层架构通常包括以下组件:
因此,要回答您关于如何为3层架构编写代码的问题,您需要开发一个与数据存储通信的ASP.NET应用程序。
web.config
中传递值。 - Darin Dimitrovconnection 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>");
}
}
}
http://nerddinnerbook.s3.amazonaws.com/Intro.htm
这不是关于分层架构的教程,但它是一个写得很好的应用程序,并提供了一些洞见,让你了解为什么要考虑这种架构。三层(层)是一种客户端服务器架构,用户界面、业务流程(业务规则)和数据存储以及数据访问被开发和维护为独立的模块或者通常在单独的平台上。
基本上,有三个层次:
将代码分为三个层次的必要性是什么?将用户界面与业务逻辑和数据库访问分离具有许多优点。以下是其中一些优点:
业务逻辑组件的可重用性可以快速开发。假设我们有一个模块,处理系统中客户的添加、更新、删除和查找。由于该组件已经开发和测试完成,我们可以在任何其他涉及维护客户的项目中使用它。