Fluentdata詳解


Fluentdata 輕型orm 僅僅一個cs文件

創建並且初始化一個IDbContext. 二選一

public IDbContext Context()
{
	return new DbContext().ConnectionStringName("MyDatabase",
			new SqlServerProvider());
}
public IDbContext Context()
{
	return new DbContext().ConnectionString(
	"Server=MyServerAddress;Database=MyDatabase;Trusted_Connection=True;", new SqlServerProvider());
}

返回 dynamic類型的List 集合:

List<dynamic> products = Context.Sql("select * from Product").QueryMany<dynamic>();

返回強類型的List 集合

List<Product> products = Context.Sql("select * from Product").QueryMany<Product>();

返回一個自定義類型的集合類型:

ProductionCollection products = Context.Sql("select * from Product").QueryMany<Product, ProductionCollection>();

返回單個動態對象數據表

dynamic product = Context.Sql(@"select * from Product
				where ProductId = 1").QuerySingle<dynamic>();

返回一個強類型對象

Product product = Context.Sql(@"select * from Product
			where ProductId = 1").QuerySingle<Product>();

返回一個DataTable對象,QueryMany< DataTable > and QuerySingle 都可以返回DataTable, 只不過QuerMany返回的是 List< DataTable >

DataTable products = Context.Sql("select * from Product").QuerySingle<DataTable>();

返回一個 int 類型

int numberOfProducts = Context.Sql(@"select count(*)
			from Product").QuerySingle<int>();

返回一個List< int >

List<int> productIds = Context.Sql(@"select ProductId
				from Product").QueryMany<int>();

索引傳參SQL

dynamic products = Context.Sql(@"select * from Product
			where ProductId = @0 or ProductId = @1", 1, 2).QueryMany<dynamic>();
or:
dynamic products = Context.Sql(@"select * from Product
			where ProductId = @0 or ProductId = @1")
			.Parameters(1, 2).QueryMany<dynamic>();

參數名傳參

dynamic products = Context.Sql(@"select * from Product
			where ProductId = @ProductId1 or ProductId = @ProductId2")
			.Parameter("ProductId1", 1)
			.Parameter("ProductId2", 2)
			.QueryMany<dynamic>();

輸出參數

var command = Context.Sql(@"select @ProductName = Name from Product
			where ProductId=1")
			.ParameterOut("ProductName", DataTypes.String, 100);
    command.Execute();
string productName = command.ParameterValue<string>("ProductName");

List 類型參數,請注意,不要在(…)語法中留下任何空格

List<int> ids = new List<int>() { 1, 2, 3, 4 };
dynamic products = Context.Sql(@"select * from Product where ProductId in(@0)", ids).QueryMany<dynamic>();

like 模糊查詢:

string cens = "%abc%";
Context.Sql("select * from Product where ProductName like @0",cens);

實體集合自動映射

List<Product> products = Context.Sql(@"select *
			from Product")
			.QueryMany<Product>();

自定義映射對象

ProductionCollection products = Context.Sql("select * from Product").QueryMany<Product, ProductionCollection>();

也可以將鏈表查詢結果集映射到自定義對象集合

List<Product> products = Context.Sql(@"select p.*,
			c.CategoryId as Category_CategoryId,
			c.Name as Category_Name
			from Product p
			inner join Category c on p.CategoryId = c.CategoryId")
				.QueryMany<Product>();

使用動態的自定義映射

List<Product> products = Context.Sql(@"select * from Product")
			.QueryMany<Product>(Custom_mapper_using_dynamic);
public void Custom_mapper_using_dynamic(Product product, dynamic row)
{
	product.ProductId = row.ProductId;
	product.Name = row.Name;
}

基於 datareader 的動態的自定義映射

List<Product> products = Context.Sql(@"select * from Product")
			.QueryMany<Product>(Custom_mapper_using_datareader);
public void Custom_mapper_using_datareader(Product product, IDataReader row)
{
	product.ProductId = row.GetInt32("ProductId");
	product.Name = row.GetString("Name");
}

如果你有一個復雜的實體類型需要控制它的創建方式,那么可以使用 QueryComplexMany/QueryComplexSingle

var products = new List<Product>();
Context.Sql("select * from Product").QueryComplexMany<Product>(products, MapComplexProduct);
private void MapComplexProduct(IList<Product> products, IDataReader reader)
{
	var product = new Product();
	product.ProductId = reader.GetInt32("ProductId");
	product.Name = reader.GetString("Name");
	products.Add(product);
}

支持多個查詢結果表映射成一個實體對象集,且單次鏈接中執行多次查詢

using (var command = Context.MultiResultSql)
{
	List<Category> categories = command.Sql(
			@"select * from Category;
			  select * from Product;").QueryMany<Category>();
	List<Product> products = command.QueryMany<Product>();
}

鏈表查詢並支持分頁

List<Product> products = Context.Select<Product>("p.*, c.Name as Category_Name")
			       .From(@"Product p 
					inner join Category c on c.CategoryId = p.CategoryId")
			       .Where("p.ProductId > 0 and p.Name is not null")
			       .OrderBy("p.Name")
			       .Paging(1, 10).QueryMany();

插入數據,返回自增ID

int productId = Context.Sql(@"insert into Product(Name, CategoryId)
			values(@0, @1);")
			.Parameters("The Warren Buffet Way", 1)
			.ExecuteReturnLastId<int>();
int productId = Context.Insert("Product")
			.Column("Name", "The Warren Buffet Way")
			.Column("CategoryId", 1)
			.ExecuteReturnLastId<int>();

使用自動應用的生成器

Product product = new Product();
product.Name = "The Warren Buffet Way";
product.CategoryId = 1;
product.ProductId = Context.Insert<Product>("Product", product)
			.AutoMap(x => x.ProductId)
			.ExecuteReturnLastId<int>();

更新操作,返回受影響行數

int rowsAffected = Context.Sql(@"update Product set Name = @0
			where ProductId = @1")
			.Parameters("The Warren Buffet Way", 1)
			.Execute();
int rowsAffected = Context.Update("Product")
			.Column("Name", "The Warren Buffet Way")
			.Where("ProductId", 1)
			.Execute();

使用自定義映射來更新實體

Product product = Context.Sql(@"select * from Product
			where ProductId = 1")
			.QuerySingle<Product>();
product.Name = "The Warren Buffet Way";
int rowsAffected = Context.Update<Product>("Product", product)
			.AutoMap(x => x.ProductId)
			.Where(x => x.ProductId)
			.Execute();

自定義插入或更新列操作

var product = new Product();
product.Name = "The Warren Buffet Way";
product.CategoryId = 1;
var insertBuilder = Context.Insert<Product>("Product", product).Fill(FillBuilder);
var updateBuilder = Context.Update<Product>("Product", product).Fill(FillBuilder);
public void FillBuilder(IInsertUpdateBuilder<Product> builder)
{
	builder.Column(x => x.Name);
	builder.Column(x => x.CategoryId);
}

刪除操作

int rowsAffected = Context.Sql(@"delete from Product
			where ProductId = 1")
			.Execute();
int rowsAffected = Context.Delete("Product")
			.Where("ProductId", 1)
			.Execute();

執行存儲過程

var rowsAffected = Context.Sql("ProductUpdate")
			.CommandType(DbCommandTypes.StoredProcedure)
			.Parameter("ProductId", 1)
			.Parameter("Name", "The Warren Buffet Way")
			.Execute();
var rowsAffected = Context.StoredProcedure("ProductUpdate")
			.Parameter("Name", "The Warren Buffet Way")
			.Parameter("ProductId", 1).Execute();

騷操作1

var product = Context.Sql("select * from Product where ProductId = 1")
			.QuerySingle<Product>();
product.Name = "The Warren Buffet Way";
var rowsAffected = Context.StoredProcedure<Product>("ProductUpdate", product)
			.AutoMap(x => x.CategoryId).Execute();

騷操作2

var product = Context.Sql("select * from Product where ProductId = 1")
			.QuerySingle<Product>();
product.Name = "The Warren Buffet Way";
var rowsAffected = Context.StoredProcedure<Product>("ProductUpdate", product)
			.Parameter(x => x.ProductId)
			.Parameter(x => x.Name).Execute();

事務使用

using (var context = Context.UseTransaction(true))
{
	context.Sql("update Product set Name = @0 where ProductId = @1")
				.Parameters("The Warren Buffet Way", 1)
				.Execute();
	context.Sql("update Product set Name = @0 where ProductId = @1")
				.Parameters("Bill Gates Bio", 2)
				.Execute();
	context.Commit();
}

在查詢一個實體對象集時如果需要再創建實體時就進行一些特殊的操作可以自定義實體工廠來滿足你的需求

List<Product> products = Context.EntityFactory(new CustomEntityFactory())
			.Sql("select * from Product")
			.QueryMany<Product>();
public class CustomEntityFactory : IEntityFactory
{
	public virtual object Resolve(Type type)
	{
		return Activator.CreateInstance(type);
	}
}

留記


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM