Dapper结合Repository模式的应用

Dapper结合Repository模式的应用,包括如何在数据访问层(DAL)使用Dapper组件。

Dapper在真实项目中使用,扩展IDbConnection的功能,支持Oracle、MS SQL Server 2005数据库

1)定义统一的IDbConnection访问入口

[csharp] view plain copy
 
  1. public class Database  
  2.     {  
  3.         /// 得到web.config里配置项的数据库连接字符串。  
  4.         private static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();  
  5.         /// 得到工厂提供器类型  
  6.         private static readonly string ProviderFactoryString = ConfigurationManager.AppSettings["DBProvider"].ToString();  
  7.         private static DbProviderFactory df = null;  
  8.         /// <summary>  
  9.         /// 创建工厂提供器并且  
  10.         /// </summary>  
  11.         public static IDbConnection DbService()  
  12.         {  
  13.             if (df == null)  
  14.                 df = DbProviderFactories.GetFactory(ProviderFactoryString);  
  15.             var connection = df.CreateConnection();  
  16.             connection.ConnectionString = ConnectionString;  
  17.             connection.Open();  
  18.             return connection;  
  19.         }  
  20. }  


2)app.config配置

[html] view plain copy
 
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.   <configSections>  
  4.   </configSections>  
  5.   <appSettings>  
  6.     <add key="DBProvider" value="System.Data.SqlClient"/>  
  7.   </appSettings>  
  8.   <connectionStrings>  
  9.     <add name="ConnectionString" connectionString="Data Source=.;Initial Catalog=PlanDb;User ID=sa;Password=manager;" providerName="System.Data.SqlClient" />  
  10.   </connectionStrings>  
  11.   <startup>  
  12.     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>  
  13.   </startup>  
  14. </configuration>  


 

3)Repository模式实现

[csharp] view plain copy
 
  1. /// <summary>  
  2.     /// 产品管理  
  3.     /// </summary>  
  4.     public class ProductRepository  
  5.     {  
  6.         public static Product GetById(int id)  
  7.         {  
  8.             var sqlstr = "select * from dbo.Product where Product_ID=@id";  
  9.             using (var conn = Database.DbService())  
  10.             {  
  11.                 return conn.Query<Product>(sqlstr, new { id }).Single();  
  12.             }  
  13.         }  
  14.   
  15.         public static List<Product> GetByPid(int pid)  
  16.         {  
  17.             var sqlstr = "select * from dbo.Product where Parent_ID=@pid order by Product_no";  
  18.             using (var conn = Database.DbService())  
  19.             {  
  20.                 return conn.Query<Product>(sqlstr, new { pid }).ToList();  
  21.             }  
  22.         }  
  23.   
  24.         /// <summary>  
  25.         /// 获取所有产品--机型  
  26.         /// </summary>  
  27.         /// <returns></returns>  
  28.         public static List<Product> GetAllTop()  
  29.         {  
  30.             var sqlstr = "select * from dbo.Product where Parent_ID=0 order by Product_no";  
  31.             using (var conn = Database.DbService())  
  32.             {  
  33.                 return conn.Query<Product>(sqlstr).ToList();  
  34.             }  
  35.         }  
  36.   
  37.         public static void Insert(Product model)  
  38.         {  
  39.             if (model.Product_ID == 0)  
  40.                 model.Product_ID = NextId;  
  41.             string sqlstr = "INSERT INTO dbo.Product" +  
  42.                 "(Product_ID, Parent_ID, Product_No, Product_Name, Product_Type, Remark, Creater, Create_Date, Data_Availability) " +  
  43.                 "values(@Product_ID,@Parent_ID,@Product_No,@Product_Name,@Product_Type,@Remark,@Creater,@Create_Date,@Data_Availability)";  
  44.             using (var conn = Database.DbService())  
  45.             {                 
  46.                 conn.Execute(sqlstr, model);  
  47.             }  
  48.         }  
  49.   
  50.         public static void Delete(int id)  
  51.         {  
  52.             var sqlstr = "delete from dbo.Product where Product_ID=@id";  
  53.             using (var conn = Database.DbService())  
  54.             {  
  55.                 conn.Execute(sqlstr, new { id });  
  56.             }  
  57.         }  
  58.         public static void Update(Product model)  
  59.         {  
  60.             string sqlstr = "UPDATE dbo.Product " +  
  61.                 "SET Product_No = @Product_No," +  
  62.                 "    Product_Name = @Product_Name, " +  
  63.                 "    Product_Type = @Product_Type, " +  
  64.                 "    Remark = @Remark" +  
  65.                 " WHERE Product_ID = @Product_ID";  
  66.             using (var conn = Database.DbService())  
  67.             {  
  68.                 conn.Execute(sqlstr,  model);  
  69.             }  
  70.         }  
  71.         /// <summary>  
  72.         /// 下一个ID  
  73.         /// </summary>  
  74.         public static int NextId  
  75.         {  
  76.             get  
  77.             {  
  78.                 return Database.NextId("Product");  
  79.             }  
  80.         }  
  81.         public static bool Exists(string no)  
  82.         {  
  83.             var sqlstr = "select count(*) from dbo.Product where Product_No=@no";  
  84.             using (var conn = Database.DbService())  
  85.             {  
  86.                 return conn.Query<int>(sqlstr, new { no }).Single() > 0;  
  87.             }  
  88.         }  
  89.     }  

http://blog.csdn.net/dacong 转载请注明出处

[csharp] view plain copy
 
  1. public class Product  
  2.     {  
  3.         #region Fields  
  4.         private int _product_id;  
  5.         private int _parent_id;  
  6.         private string _product_no = "";  
  7.         private string _product_name = "";  
  8.         private string _product_type = "";  
  9.         private string _remark = "";  
  10.         private string _creater = "";  
  11.         private DateTime _create_date;  
  12.         private string _data_availability = "";  
  13.         #endregion  
  14.   
  15.         public Product()  
  16.         {  
  17.             _parent_id = 0;  
  18.             _data_availability = "Y";  
  19.         }  
  20.         #region Public Properties  
  21.   
  22.         public int Product_ID  
  23.         {  
  24.             get { return _product_id; }  
  25.             set  
  26.             {  
  27.                 _product_id = value;  
  28.             }  
  29.         }  
  30.   
  31.         /// <summary>  
  32.         /// 父产品ID,0为最顶层产品  
  33.         /// </summary>  
  34.         public int Parent_ID  
  35.         {  
  36.             get { return _parent_id; }  
  37.             set  
  38.             {  
  39.                 _parent_id = value;  
  40.             }  
  41.         }  
  42.   
  43.         public string Product_No  
  44.         {  
  45.             get { return _product_no; }  
  46.             set  
  47.             {  
  48.                 _product_no = value;  
  49.             }  
  50.         }  
  51.   
  52.         public string Product_Name  
  53.         {  
  54.             get { return _product_name; }  
  55.             set  
  56.             {  
  57.                 _product_name = value;  
  58.             }  
  59.         }  
  60.   
  61.         public string Product_Type  
  62.         {  
  63.             get { return _product_type; }  
  64.             set  
  65.             {  
  66.                 _product_type = value;  
  67.             }  
  68.         }  
  69.   
  70.         public string Remark  
  71.         {  
  72.             get { return _remark; }  
  73.             set  
  74.             {  
  75.                 _remark = value;  
  76.             }  
  77.         }  
  78.   
  79.         public string Creater  
  80.         {  
  81.             get { return _creater; }  
  82.             set  
  83.             {  
  84.                 _creater = value;  
  85.             }  
  86.         }  
  87.   
  88.         public DateTime Create_Date  
  89.         {  
  90.             get { return _create_date; }  
  91.             set  
  92.             {  
  93.                 _create_date = value;  
  94.             }  
  95.         }  
  96.   
  97.         public string Data_Availability  
  98.         {  
  99.             get { return _data_availability; }  
  100.             set  
  101.             {  
  102.                 _data_availability = value;  
  103.             }  
  104.         }  
  105.  
  106.  
  107.         #endregion  
  108. }  


 

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dacong/article/details/7300046
原文地址:https://www.cnblogs.com/webenh/p/9178779.html