标签:called finally eps star explain ext 分享 char accept
本文来自:http://fluentdata.codeplex.com/wikipage?title=Fluency&referringTitle=Home
$(SolutionDir)\Fluency\TableDataGateway.tt -out $(SolutionDir)/Entities.Generated.cs -a !!ns!MyDomain -a !!cs!Server=YOUR_SERVER_NAME;Database=MyDb;Trusted_Connection=true -a !!csn!MyDb
CREATE TABLE [dbo].[Product]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](256) NOT NULL, [Price] [decimal](18, 2) NOT NULL, [Sku] [nvarchar](256) NULL, [Description] [nvarchar](max) NULL, [ManufacturerId] [int] NULL, [CreatedOn] [datetime] NULL, [ModifiedOn] [datetime] NULL CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED ([Id] ASC))
/// <summary> /// Product entity class /// </summary> public partial class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public string Sku { get; set; } public string Description { get; set; } public int ManufacturerId { get; set; } public DateTime CreatedOn { get; set; } public DateTime ModifiedOn { get; set; } } /// <summary> /// Product gateway class /// </summary> public partial class ProductGateway { private static IDbContext Context() { return new DbContext().ConnectionStringName("MyDb", new SqlServerProvider()); } public static Product Select(int id) { using(var context = Context()) { return context.Sql(" SELECT * FROM Product WHERE Id = @id ") .Parameter("id", id) .QuerySingle<Product>(); } } public static List<Product> SelectAll() { return SelectAll(string.Empty); } public static List<Product> SelectAll(string sortExpression) { return SelectAll(0, 0, sortExpression); } public static List<Product> SelectAll(int startRowIndex, int maximumRows, string sortExpression) { using (var context = Context()) { var select = context.Select<Product>(" * ") .From(" Product "); if (maximumRows > 0) { if (startRowIndex == 0) startRowIndex = 1; select.Paging(startRowIndex, maximumRows); } if (!string.IsNullOrEmpty(sortExpression)) select.OrderBy(sortExpression); return select.QueryMany(); } } public static int CountAll() { using (var context = Context()) { return context.Sql(" SELECT COUNT(*) FROM Product ") .QuerySingle<int>(); } } public static List<Product> SelectByManufacturer(int manufacturerId) { return SelectByManufacturer(manufacturerId, string.Empty); } public static List<Product> SelectByManufacturer(int manufacturerId, string sortExpression) { return SelectByManufacturer(manufacturerId, 0, 0, sortExpression); } public static List<Product> SelectByManufacturer(int manufacturerId, int startRowIndex, int maximumRows, string sortExpression) { using (var context = Context()) { var select = context.Select<Product>(" * ") .From(" Product ") .Where(" ManufacturerId = @manufacturerid ") .Parameter("manufacturerid", manufacturerId); if (maximumRows > 0) { if (startRowIndex == 0) startRowIndex = 1; select.Paging(startRowIndex, maximumRows); } if (!string.IsNullOrEmpty(sortExpression)) select.OrderBy(sortExpression); return select.QueryMany(); } } public static int CountByManufacturer(int manufacturerId) { using (var context = Context()) { return context.Sql(" SELECT COUNT(*) FROM Product WHERE ManufacturerId = @manufacturerid") .Parameter("manufacturerid", manufacturerId) .QuerySingle<int>(); } } public static bool Insert(Product product) { using (var context = Context()) { int id = context.Insert<Product>("Product", product) .AutoMap(x => x.Id) .ExecuteReturnLastId<int>(); product.Id = id; return id > 0; } } public static bool Update(Product product) { using (var context = Context()) { return context.Update<Product>("Product", product) .AutoMap(x => x.Id) .Execute() > 0; } } public static bool Delete(Product product) { return Delete(product.Id); } public static bool Delete(int id) { using (var context = Context()) { return context.Sql(" DELETE FROM Product WHERE Id = @id ") .Parameter("id", id) .Execute() > 0; } } }
Product product = ProductGateway.Select(103);
List<Product> products = ProductGateway.SelectAll();
int count = ProductGateway.CountAll();
// LOAD FIRST 20 PRODUCTS ORDER BY ID List<Product> products = ProductGateway.SelectAll(1, 20, "Id ASC");
// LOAD FIRST 20 PRODUCTS ORDER BY NAME FOR MANUFACTURER ID 91 List<Product> products = ProductGateway.SelectByManufacturer(91,1, 20, "Name ASC");
Product product = new Product() { Name = "New Product", Price = 111, Sku = "SKU-111", Description = "None" }; ProductGateway.Insert(product); // IF PRIMARYKEY IS IDENTITY THEN IT WILL BE SET BACK TO THE OBJECT Console.Writeline(string.Format("Product Id = {0}", product.Id));
Product product = ProductGateway.Select(103); product.Price = 200; ProductGateway.Update(product);
Product product = ProductGateway.Select(103); ProductGateway.Delete(product);
Last edited Jun 25, 2013 at 6:55 AM by leadfoot, version 28
标签:called finally eps star explain ext 分享 char accept
原文地址:http://www.cnblogs.com/z5337/p/7062086.html