小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

SubSonic 3.0新特性

 Richard118 2014-04-18

1.    引言

大多數(shù)數(shù)據(jù)訪問程序包提供從“數(shù)據(jù)庫外”訪問,意思是這種方式將您的數(shù)據(jù)庫的表在應用程序中用對象表示。這種方式可以在大多數(shù)案例中工作,但是關系理論與面向對象編程將變的不一致,即“錯誤的匹配”。

許多人喜歡只使用類工作,但不想關心數(shù)據(jù)庫的具體實現(xiàn)。為了釋放他們的工作,同時又不開放數(shù)據(jù)庫設計內(nèi)部實現(xiàn)。SubSonic創(chuàng)建了一個免費基類且單獨存在的,作為純粹的CLR對象(也稱為POCO)。

如果你是這些人之一,你不需要特別關心的數(shù)據(jù)庫結構的,那么SimpleRepository是給你準備的。

 

2.    使用SimpleRepository

使用Rails的有關工作的有趣的事情之一就是你可以建立從代碼和專注于您的應用程序的數(shù)據(jù)庫。許多開發(fā)者發(fā)現(xiàn)這種非常自由(包括我自己在內(nèi))。它的一個缺點,就是你需要學習的遷移代碼,你需要知道它是如何工作的。這對我們來說,不是一個好的辦法,而是一直希望Rails將“只知道”我們要遷移的東西。

這就是為什么我們想使用SubSonic的自動遷移。這里的目標是,如果您在構造SimpleRepository對象時設置一個標志,告訴它遷移模式:自動創(chuàng)建和同步數(shù)據(jù)庫,其關鍵是在構造函數(shù)中設置適當?shù)倪x項,如:

var repository = new SimpleRepository(SimpleRepositoryOptions.RunMigrations);

設置選項RunMigrations告訴SubSonic在更新或保存數(shù)據(jù)時同步你的模型對象到數(shù)據(jù)庫。

添加對SubSonic的引用,這部分非常簡單,右鍵單擊您的項目,添加引用,瀏覽以查找SubSonic3.0DLL(SubSonic.Core.dll)。

本例子使用的是SQLite數(shù)據(jù)庫,添加對System.Data.SQLite的引用

設置數(shù)據(jù)庫連接字符串

<add name="Northwind" connectionString="server=.\SQLExpress;database=SubSonic;integrated security=true;" providerName="System.Data.SqlClient"/>
 

<connectionStrings>

    <add name="NorthwindSQLite"

         connectionString="Data Source=D:\Program Files\Sqliteman\test.db"

         providerName="System.Data.SQLite"/> 
 
<add name="NorthwindMySql" connectionString="server=localhost;database=northwind;user id=root; password=" providerName="MySql.Data.MySqlClient"/>

 

 

2.1 自動遷移

例如有以下post對象:

    public class Post

    {

        public Guid ID { get; set; }

        public string Title { get; set; }

        public string Body { get; set; }

 

    }

 

當使用SimpleRepository對象時,設置構造函數(shù)選項RunMigrations,他將會自動創(chuàng)建你需要的表。

//基于NorthwindSQLite數(shù)據(jù)庫創(chuàng)建一個repository對象并自動遷移

var repo = new SubSonic.Repository.SimpleRepository("NorthwindSQLite", SimpleRepositoryOptions.RunMigrations);

            //var post = repo.Single<Post>(x => x.Title == "My Title");

            Post post = new Post();

            post.ID = Guid.NewGuid().ToString();

            post.Title = "My Title";

            post.Body = "My Titlessssssssssssss";

            var obj = repo.Add<Post>(post);

在這個例子中是不能查詢Title” My Title”的記錄,因為沒有Post表。執(zhí)行Single()方法后,SubSonic將運行遷移,并創(chuàng)建表,這樣就不會有錯誤。 當?shù)谝淮芜\行,下面的SQL將被執(zhí)行之前選擇:

CREATE TABLE [Posts] (
 [ID] uniqueidentifier NOT NULL PRIMARY KEY,
 [Title] nvarchar(255) NOT NULL,
 [Body] nvarchar(255) NOT NULL,
);
ALTER TABLE [Posts]
ADD CONSTRAINT PK_Posts_Key PRIMARY KEY([ID])";

 

請注意,表名是復數(shù)(默認),“ID”屬性選定為主鍵(默認)和字符串長度默認為255。

你也可以改變SubSonic生成方法,并使用小的屬性設置

 主鍵:如果你調用一列“ID”或“Key”或“[ClassName]ID”,無論它的類型都將是表的主鍵。如果你想到其他的屬性列,你可以使用一個主鍵屬性(“SubSonicPrimaryKey”),SubSonic將使用該列作為主鍵。

 字符串長度:有兩種方法告訴SubSonic的如何處理這一點,包括使用屬性。第一類是“SubSonicStringLengthint length)”,第二是“SubSonicLongString”為nvarcharmax)或LONGTEXT設置,依賴于您的提供程序。

 空:默認為不為null,但你可以修改屬性為空類型的。

 數(shù)值精度:默認值是10精度和2位小數(shù)點,但你可以改變,隨著“SubSonicNumericPrecision(int precision, int scale)”屬性。

 忽略屬性:你可以使用“SubSonicIgnore”忽略生成的屬性。

 

2.2 更新模型

開始部分都是容易的,但是隨著你的開發(fā)將會更改或移除類的屬性,SubSonic將會很好的配合你的改變。

例如,假設你已經(jīng)添加了PublishDate到您的Post類:

    public class Post

    {

        public string ID { get; set; }

        public string Title { get; set; }

 

        public string Body { get; set; }

        public DateTime PublishDate { get; set; }

    }

 

當下次使用SimpleRepository進行(Save/Find/Get)數(shù)據(jù)操作時,下面的SQL命令將在此之前執(zhí)行:

ALTER TABLE [Posts] ADD DateTime datetime NOT NULL CONSTRAINT DF_Posts_PublishDate DEFAULT ('01/01/0001');
UPDATE SubSonicTests SET PublishDate ='01/01/0001';

 

在這里發(fā)生有如下幾件事:

第一,   增加的列(不可為空,可以通過使用DateTime?)。

第二,   默認修改的地方(使用DateTime.MinValue,一個非空列應在生成的時候約定默認值。

UPDATE語句依據(jù)設置的默認值處理現(xiàn)有的數(shù)據(jù)庫設置(像SQLiteMySQL或其他數(shù)據(jù)庫)。 另刪除的屬性將導致列從數(shù)據(jù)庫中刪除。

 

2.3 查詢

可以通過SimpleRepository對象從數(shù)據(jù)庫中查詢數(shù)據(jù),甚至于使用LINQ:

var repo=new SimpleRepository(SimpleRepositoryOptions.RunMigrations);
 
//see if a record exists
bool exists=repo.Exists<Post>(x=>x.Title=="My Title");
 
//use IQueryable
var qry=from p in repo.All<Post>()
        where p.Title=="My Title"
        select p;
 
//get a post
var post=repo.Single<Post>(x=>x.Title=="My Title");
var post=repo.Single<Post>(key);
 
//a lot of posts
var posts=repo.Find<Post>(x=>x.Title.StartsWith("M"));
 
//a PagedList of posts - using 10 per page
var posts=repo.GetPaged<Post>(0,10);
//sort by title
var posts=repo.GetPaged<Post>("Title",0,10);
 
//add a post
var newKey=repo.Add(post);
 
//add a lot of posts - using a transaction
IEnumerable<Post> posts=GetABunchOfNewPosts();
repo.AddMany(posts);
 
//update a post
repo.Update(post);
 
//update a bunch of posts in a transaction
IEnumerable<Post> posts=GetABunchOfNewPosts();
repo.UpdateMany(posts);
 
//delete a post
repo.Delete<Post>(key);
 
//delete a lot of posts
repo.DeleteMany <Post>(x=>x.Title.StartsWith("M"));
 
//delete posts using a transaction
IEnumerable<Post> posts=GetABunchOfNewPosts();
repo.DeleteMany(posts);

 

 

 

3.    使用ActiveRecord

針對您的數(shù)據(jù)和數(shù)據(jù)庫ActiveRecord的是最簡單,最容易的方法。該模式是,每個實例對應的是一個數(shù)據(jù)庫中的數(shù)據(jù)行,這意味著每個對象類型可以被認為是你的數(shù)據(jù)庫表。

許多開發(fā)人員不喜歡與他們的數(shù)據(jù)庫緊耦合工作,但其他人認為這是非常理想方式,消除了與數(shù)據(jù)庫抽象工作有利于面向對象的“阻抗不匹配”。

SubSonicActiveRecord是給你需要的,然后自己決定如何做。你會發(fā)現(xiàn)你有能力使用LINQ是如此易用的代碼,快速和簡單的依據(jù)數(shù)據(jù)庫生成代碼。

3.1設置模板連接

T4模板創(chuàng)造類文件,需要知道連接字符串。要設置:打開文件名為“Settings.ttinclude”,在@ import語句下面,您會看到三個設置,如下:

const string Namespace = "Northwind.Data";
const string ConnectionStringName = "Northwind";
 
const string DatabaseName = "Northwind";

主要設置是“ConnectionStringName”, 這告訴SubSonic的使用的哪個數(shù)據(jù)庫連接。Namespace常量用來SubSonic生成代碼的命名空間。 注意包含的所有*.tt文件需要匹配您的數(shù)據(jù)庫,如下:

//其他文件: MySQL.ttinclude, SQLite.ttinclude
<#@ include file="SQLServer.ttinclude" #>

3.2項目中增加T4模板

上述設置完成后,只要將T4模板拖入到您的項目。因為Visual Studio 2008中看到一個“*.tt”文件,它會自動執(zhí)行它。所以你不必做任何事情,只需要運行程序。

文件列表如下:

ActiveRecord.tt

Context.tt

Settings.ttinclude

SQLServer.ttinclude OR MySQL.ttinclude OR SQLite.ttinclude

StoredProcedures.tt (可選項,如果執(zhí)行存儲過程需要添加此文件)

Structs.tt

如果更改了數(shù)據(jù)庫,只需右鍵單擊ActiveRecord.tt,Context.ttStructs.tt文件并選擇“運行自定義工具”,將會自動執(zhí)行它們。

 

3.3查詢

使用ActiveRecord的查詢非常方便,下面是單元測試的一些例子:

//差錢ID=1的默認單條記錄
var product = Product.SingleOrDefault(x => x.ProductID == 1);
 
//查詢ProductID <= 10的記錄
var products = Product.Find(x => x.ProductID <= 10);
 
//獲取服務器端的分頁列表
var products = Product.GetPaged(0,10);
 
//使用Linq查詢
var products = from p in Product.All()
          join od in OrderDetail.All() on p.ProductID equals od.ProductID
          select p;

 

從上面的代碼可以中注意與SubSonic2.0的一些不同。

4.    結論

SimpleRepository使用和測試很簡單,因為它從SubSonic.Repository.IRepository繼承。如果按照注入模式,即傳遞一個IRepository,那么你可以使用你最喜歡的模擬工具或創(chuàng)建一個假的Repository查詢。

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多