三個文件夾,第一個是放置前端部分,第二個是各種支持的類文件,第三個是單元測試文件。 Core文件類庫 放置的是與資料庫做交互的文件,以及一些第三方類庫,還有與資料庫連接的文件 1.Lasy.Validator是一個基於Attribute驗證器,我覺得這種驗證方式在挺便捷的,具體可以在這裡查看到htt ...
三個文件夾,第一個是放置前端部分,第二個是各種支持的類文件,第三個是單元測試文件。
Core文件類庫
放置的是與資料庫做交互的文件,以及一些第三方類庫,還有與資料庫連接的文件
1.Lasy.Validator是一個基於Attribute驗證器,我覺得這種驗證方式在挺便捷的,具體可以在這裡查看到http://www.cnblogs.com/over140/archive/2009/07/04/1515892.html
2.ToJson是一個將數據轉換成json格式的字元串的一個類庫,很便捷
3.NPinyin是一個將中文轉換成拼音的一個類別
app.config是一個連接資料庫的配置文件,兩個資料庫一個是測試用的,一個是調試用的
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> </configSections> <connectionStrings> <add name="QuyouCore.Properties.Settings.ClubConnectionString" connectionString="Data Source=192.168.1.214;Initial Catalog=Club;User ID=sa;Password=123456" providerName="System.Data.SqlClient" /> <add name="QuyouCore.Properties.Settings.ClubTestConnectionString" connectionString="Data Source=192.168.1.214;Initial Catalog=Club_Test;User ID=sa;Password=123456" /> </connectionStrings> </configuration>
點擊core文件夾進入資料庫操作
1.DataAccess
DataAccess中主要就是一些資料庫的增刪改查操作了
Connection.cs是連接數據的文件,主要根據前端文件夾下麵的bin文件中的XML文件來操作是連接哪個資料庫,用XML就可以很方便的不重新編譯直接換資料庫
<?xml version="1.0" encoding="utf-8" ?> <connectionStringToUse> <!-- this value can be Local (pc), Production (server), or Test (test)--> <environment>Production</environment> </connectionStringToUse>
1 using System; 2 using System.Web; 3 using System.Xml; 4 using QuyouCore.Core.Domain; 5 using QuyouCore.Properties; 6 7 namespace QuyouCore.Core.DataAccess.Impl 8 { 9 public class Connection 10 { 11 public Connection() 12 { 13 ConnString = GetCurrentConnectionString(); 14 } 15 public ClubDataContext GetClubContext() 16 { 17 var vcd = new ClubDataContext(ConnString); 18 //vcd.Log = new DebuggerWriter(); 19 return vcd; 20 } 21 public string GetCurrentConnectionString() 22 { 23 var connString = string.Empty; 24 try 25 { 26 var doc = new XmlDocument(); 27 doc.Load(HttpContext.Current.Request.PhysicalApplicationPath + "bin/ConnectionStringToUse.xml"); 28 var xnl = doc.GetElementsByTagName("environment"); 29 var xe = (XmlElement)xnl[0]; 30 switch (xe.InnerText.ToLower()) 31 { 32 case "local": 33 connString = Settings.Default.ClubConnectionString; 34 break; 35 case "production": 36 connString = Settings.Default.ClubServerConnectionString; 37 break; 38 case "test": 39 connString = Settings.Default.ClubTestConnectionString; 40 break; 41 } 42 } 43 catch (Exception e) 44 { 45 connString = Settings.Default.ClubTestConnectionString; 46 } 47 return connString; 48 } 49 50 public string ConnString 51 { 52 get; 53 set; 54 } 55 } 56 }
BaseRepository.cs文件是做一些通用的Linq查詢語句,還有就是寫Log操作,將各個查詢語句記錄下來,便於調試,這裡用到的Log工具是log4net
using System.Collections.Generic; using System.Data.Linq; using System.Linq; using QuyouCore.Core.Domain; using QuyouCore.Core.Enum; using QuyouCore.Core.Util; using System; using System.Linq.Expressions; namespace QuyouCore.Core.DataAccess.Impl { public class BaseRepository { protected BaseRepository() { Conn = new Connection(); DataContext = new RepositoryContext(Conn.ConnString); } #region Properties /// <summary> /// Gets or sets the data context. /// </summary> /// <value>The data context.</value> private RepositoryContext DataContext { get; set; } protected Connection Conn { get; set; } #endregion #region Entity and metadata functions /// <summary> /// 分頁 /// </summary> protected DataPageList<TEntity> GetEntitiesByPage<TEntity>(IQueryable query, int page, int size, DataContext context) { context.Log = FileLog.Out; var data = DataPageList<TEntity>.Create<TEntity>(query, page, size); WriteLog(FileLog.FileInfo.ToString()); return data; } /// <summary> /// 根據條件獲取一條數據 /// </summary> protected TEntity GetEntity<TEntity>(Expression<Func<TEntity, bool>> queryFunc) where TEntity : class //Entity { DataContext.Log = FileLog.Out; var results = from entity in DataContext.GetTable<TEntity>() select entity; results = results.Where(queryFunc); var result = results.FirstOrDefault(); WriteLog(FileLog.FileInfo.ToString()); return result; } /// <summary> /// 根據條件獲取數據信息列 /// </summary> protected List<TEntity> GetEntities<TEntity>(Expression<Func<TEntity, bool>> queryFunc) where TEntity : class //Entity { DataContext.Log = FileLog.Out; var query = from entity in DataContext.GetTable<TEntity>() select entity; query = query.Where(queryFunc); var results = query.ToList(); WriteLog(FileLog.FileInfo.ToString()); return results; } protected List<TEntity> GetEntities<TEntity>(IQueryable<TEntity> query) where TEntity : class //Entity { DataContext.Log = FileLog.Out; var results = query.ToList(); WriteLog(FileLog.FileInfo.ToString()); return results; } protected IQueryable<TEntity> GetQueryEntities<TEntity>(Expression<Func<TEntity, bool>> queryFunc) where TEntity : class //Entity { var query = from