瑞雪年

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: 订阅 订阅 :: 管理 ::
  261 随笔 :: 0 文章 :: 432 评论 :: 0 引用

DAC 4.0 是一个全新的版本, 几乎全部重写了以前的代码,设计的结构也简化很多,基于.net4,使用lambda表达式代替反射,去除大量过时的功能(如DataTable和DataSet的相关功能等),去除自定义表达式部分,通过解析lambda表达式实现自定义条件查询。 使用.net自带Configuration功能,只要web.config或app.config,不需要额外的配置文件,方便配置使用;去除xml mapping方式,支持DLinq属性。支持多种数据库(已测试sql server, mysql, sqlite及oracle), 支持mono(ubuntu 11.10 +  mono 2.10.5 + mysql 5.1.58测试通过)。

代码示例:

初启化,通过配置文件或connection string构造DataContext:

var dc = new DataContext("RaisingStudio.Data.TestProject.Properties.Settings.DefaultConnectionString");

var dc = new DataContext("server=localhost;User Id=root;password=root;database=test;");

lambda表达式查询:

var q = this.dc.Query<Item>(i => i.Name == "test" && i.Status == "ok");

或linq查询:

var q = from s in dc.GetQuery<Supplier>() 
where s.SuppId > 0 && s.Name == "for Test3"
select s;

增删改:

this.dc.Insert<Product>(new Product
{
ProductId = Guid.NewGuid().ToString(),
CategoryId = categoryId,
Name = "for Test4"
});


this.dc.Delete<Supplier>(suppId);

this.dc.Update<Product>(product);

this.dc.Save<Category>(category);

通过主键获取单个实例:

var product = this.dc.GetEntity<Product>(productId);

支持排序和分页:

List<Item> items = this.dc.Query<Item>(ExpressionExtension.Empty<Item>().Take(3)).ToList();
var q = (from i in this.dc.GetQuery<Item>()
select i).Skip(1).Take(2);
int count = this.dc.Query<Item>(ExpressionExtension.Empty<Item>().Skip(1)).Count();

部分列:

this.dc.Save<Item>(item, new string[] { "UnitCost" });

this.dc.Insert<Item>(item, new Expression<Func<Item, object>>[]
{
i => i.ItemId,
i => i.Name,
i => i.ListPrice,
i => i.ProductId,
i => i.Status
});

无实例类查询:

Command command = new Command("SELECT * FROM Product WHERE CategoryId = @p1");
command.AddParameter("@p1", categoryId);
var q = this.dc.Query(command);
int count = 0;
foreach (dynamic p in q)
{
Assert.IsNotNull(p);
Assert.AreEqual(p.CategoryId, categoryId);
count++;
}

事务处理:

            this.dc.BeginTransaction();
try
{
int result = this.dc.Insert<Supplier>(new Supplier
{
Name = "s for Test6",
Status = "done",
City = "dalian"
});

int suppId = Convert.ToInt32(this.dc.GetIdentity<Supplier>());
result = this.dc.Delete<Supplier>(suppId);

Category category = new Category
{
CategoryId = Guid.NewGuid().ToString(),
Name = "c for Test6",
Descn = "d for Test6"
};
Product product = new Product
{
ProductId = Guid.NewGuid().ToString(),
Name = "p for Test6",
Descn = "d for Test6",
CategoryId = category.CategoryId
};
result = this.dc.Insert<Category>(category);
result = this.dc.Insert<Product>(product);

this.dc.CommitTransaction();
}
catch (Exception ex)
{
this.dc.RollbackTransaction();
throw ex;
}

配置文件示例:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="raisingstudio.data" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="CommandConverter.Settings" type="RaisingStudio.Data.CommandConverterSettings, RaisingStudio.Data" />
<section name="CommandBuilder.Settings" type="RaisingStudio.Data.CommandBuilderSettings, RaisingStudio.Data" />
</sectionGroup>
</configSections>
<system.data>
<DbProviderFactories>
<add name="System.Data.SQLite" invariant="System.Data.SQLite" description="ADO.NET Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
<add name="Oracle.DataAccess" invariant="Oracle.DataAccess" description="Oracle.DataAccess" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess" />
</DbProviderFactories>
</system.data>
<raisingstudio.data>
<CommandConverter.Settings>
<add name="System.Data.OracleClient" providerName="System.Data.OracleClient" usePositionalParameters="false" parameterPrefix=":" useParameterPrefixInParameter="false" useParameterPrefixInSql="true" />
<add name="Oracle.DataAccess" providerName="Oracle.DataAccess" usePositionalParameters="false" parameterPrefix=":" useParameterPrefixInParameter="false" useParameterPrefixInSql="true" />
</CommandConverter.Settings>
<CommandBuilder.Settings>
<add name="MySql.Data.MySqlClient" providerName="MySql.Data.MySqlClient" pagingMethod="LIMIT" identityMethod="LAST_INSERT_ID" useBrackets="false" />
<add name="System.Data.SQLite" providerName="System.Data.SQLite" pagingMethod="LIMIT" identityMethod="LAST_INSERT_ROWID"/>
<add name="System.Data.OracleClient" providerName="System.Data.OracleClient" pagingMethod="ROWNUM" identityMethod="CURRVAL" />
<add name="Oracle.DataAccess" providerName="Oracle.DataAccess" pagingMethod="ROWNUM" identityMethod="CURRVAL" useBrackets="false" />
</CommandBuilder.Settings>
</raisingstudio.data>
<connectionStrings>
<add name="RaisingStudio.Data.TestProject.Properties.Settings.DefaultConnectionString"
connectionString
="Data Source=localhost;Initial Catalog=test;Integrated Security=True"
providerName
="System.Data.SqlClient" />
<add name="RaisingStudio.Data.TestProject.Properties.Settings.MySqlConnectionString"
connectionString
="server=localhost;User Id=root;password=root;Persist Security Info=True;database=test"
providerName
="MySql.Data.MySqlClient" />
<add name="RaisingStudio.Data.TestProject.Properties.Settings.SqliteConnectionString"
connectionString
="Data Source=test.db" providerName="System.Data.SQLite" />
<add name="RaisingStudio.Data.TestProject.Properties.Settings.OracleConnectionString"
connectionString
="Data Source= (DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = zhongzf-PC)(PORT = 1522)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE) ) );User Id=demo;Password=demo;"
providerName
="Oracle.DataAccess" />
</connectionStrings>
</configuration>

 

下载地址:http://dac.codeplex.com/

相关阅读:

http://www.cnblogs.com/zhongzf/archive/2009/04/05/1429780.html (实体生成工具使用RazorEngine,模版可轻松实制)

 

 

posted on 2012-01-07 19:25 瑞雪年 阅读(...) 评论(...) 编辑 收藏