在.NET3.5平台上使用LinQ to SQL + NBear 创建三层WEB应用
看了《一步一步學(xué)Linq to sql》和《在.NET 3.5 平臺上使用LINQ to SQL創(chuàng)建三層/多層Web應(yīng)用系統(tǒng)》
這兩個(gè)系列文章后,因?yàn)橐恢笔褂肗Bear,所以試著綜合了一下主要用NBear.IOC其它的NBear.data里的就沒有會了,相應(yīng)的使用LINQ來做數(shù)據(jù)處理.
之前一直使用NBear做些WEB應(yīng)用主要感覺NBear分層清晰,最重要的開發(fā)速度快,很少有重復(fù)的代碼.代碼很簡潔.相關(guān)的實(shí)體和數(shù)據(jù)增刪改查的代碼都能自動(dòng)生成.整個(gè)應(yīng)用,只需寫aspx.cs里的代碼和業(yè)務(wù)代碼.具說NBear的數(shù)據(jù)處理有損耗, 而Linq又是建立在ado.net之上,這們結(jié)合一下,用NBear的快捷+Linq的高性能,關(guān)于NBear的使用可以看一下我的Blog中的如何利用NB代碼生成工具和NBear框架,快速開發(fā)WEB項(xiàng)目,有點(diǎn)偏題了.呵..
接下來要轉(zhuǎn)入正題說說使用NBear+LINQ的多層構(gòu)架Demo。我們將建立以下項(xiàng)目:
A,網(wǎng)站項(xiàng)目 WebSite:表現(xiàn)層
B,類庫項(xiàng)目 ns.ServiceInterfaces? :定義數(shù)據(jù)訪問服務(wù)接口
C,類庫項(xiàng)目 ns.ServiceImpls:定義數(shù)據(jù)訪問服務(wù)的實(shí)現(xiàn)
D,類庫項(xiàng)目 ns.Entities:實(shí)體(LINQ)
項(xiàng)目之間的引用如下:
A引用B和D;
B引用D和NBear.IoC,System.Data.Linq程序集
C引用B、D、NBear.IoC,System.Data.Linq程序集
項(xiàng)目都建好后,第一步就是在ns.Entities項(xiàng)目中用vs創(chuàng)建
?創(chuàng)建Linq to sql后,在VS鏈接上SQL,然后把拖上去.實(shí)體就生成OK了 這樣ns.Entities就OK了
第二步 ns.ServiceInterfaces? :定義數(shù)據(jù)訪問服務(wù)接口
這一步主要定義好表示層要用的數(shù)據(jù)操作的方式.表示層將直接調(diào)用接口.cs里不會有數(shù)據(jù)處理的代碼. 實(shí)現(xiàn)分層
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBear.IoC.Service;
using System.Data.Linq;
using ns.Entities;
namespace ns.ServiceInterfaces
{
??? public interface IArticleServices : IServiceInterface
??? {
???????? int InsertArticle();
???????? Table<Article> GetArticleAll();? //取所有文章數(shù)據(jù)
??? }
}
第三步就是 ns.ServiceImpls:定義數(shù)據(jù)訪問服務(wù)的實(shí)現(xiàn)
主要是接口的實(shí)現(xiàn)層,數(shù)據(jù)處理的代碼將在這個(gè)層時(shí)來完成
using System.Text;
using ns.ServiceInterfaces;
using ns.Entities;
using System.Data.Linq;
namespace XRW.ServiceImpls
{
??? public class ArticleService : IArticleServices
??? {
??????? private DataClassesDataContext odbc = new DataClassesDataContext(); //
???????
??????? public int InsertArticle()
??????? {
??????????? return 1 + 1;
??????? }
??????? public Table<Article> GetArticleAll()
??????? {
??????????? try
??????????? {
?????????????? return odbc.Article;
??????????? }
??????????? catch (Exception exp)
??????????? {
???????????????? throw exp;
??????????? }
??????? }
??? }
}
在數(shù)據(jù)訪問層創(chuàng)建的DataContext實(shí)例負(fù)責(zé)訪問數(shù)據(jù)庫操作相關(guān)的方法和屬性。
DataContext類包含了數(shù)據(jù)庫表的屬性和產(chǎn)生動(dòng)態(tài)SQL腳本執(zhí)行數(shù)據(jù)庫操作的相關(guān)方法。
對于每一個(gè)數(shù)據(jù)表,均有一個(gè)對應(yīng)的實(shí)體類,實(shí)體類的屬性映射到實(shí)際的數(shù)據(jù)表。
訪問DataContext類的屬性,可以返回一個(gè)Table實(shí)體集合。
例如:return odbc.Article;
第三步就是在aspx.cs 使用了.
通過前面的實(shí)體生成,接口的定義和接口的實(shí)現(xiàn),表示層完全不用和數(shù)據(jù)庫打交道了.
??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? if (!IsPostBack)
??????? {
??????????? int i = ServiceFactory.Create().GetService<IArticleServices>().InsertArticle();
??????????? int d = i;
??????????? Table<Article> articleTab = ServiceFactory.Create().GetService<IArticleServices>().GetArticleAll();
??????????? GridView1.DataSource = from tab in articleTab select new { tab.ArticleID, tab.Author, tab.CreateDate };
???????????????
??????????? GridView1.DataBind();
??????? }
??? }
另外NBear.ioc要使用起來,還需要在web.config 里配置
<!-- 設(shè)置 castle,entityConfig,log4net? -->
<configSections>
?<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/>
?<section name="entityConfig" type="NBear.Common.EntityConfigurationSection, NBear.Common"/>
</configSections>
<castle>
<components>
?<component id="Article" service="ns.ServiceInterfaces.IArticleServices, ns.ServiceInterfaces" type="ns.ServiceImpls.ArticleService, ns.ServiceImpls"/>
?</components>
</castle>
如果沒有配置好會提示找不到相應(yīng)的接口實(shí)現(xiàn)
以上代碼是不是很簡潔,每層負(fù)責(zé)每層的工作.
當(dāng)然以上只是個(gè)人觀點(diǎn),也只是做了一個(gè)小例子,還沒有實(shí)際在項(xiàng)目中使用,拿出來跟大家討論交流.
以上需要了解的知識點(diǎn):NBear使用和配置,LINQ的使用,Ioc容器,以及多層的理解.歡迎交流NBear的QQ群:7731621
作者:王林 稻草人.net 'Blogs
出處:
轉(zhuǎn)載請注明此處,謝謝!
?
總結(jié)
以上是生活随笔為你收集整理的在.NET3.5平台上使用LinQ to SQL + NBear 创建三层WEB应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 经常梦到地震是什么意思
- 下一篇: 张雨生《口是心非》