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

 


免責聲明!

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



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