V0.2版的開源距離今天(05年03月)已有近3年的時間。能夠說這個版本已經有點落伍的感受了,呵呵。html
V0.2版至今一直處於BUG的修復及一些細小功能的增長,因此版本號上一直沒有變化。數據庫
其實在這一、2年中,我一直在想着Farseer.Net 的將來發展情況。有嘗試用EF的核心、也有想過用NHibernate的核心。僅僅是在這些核心的基礎下作二次開發,以我的編碼的經驗從客戶端調用角度進行「優化」,但老是感受缺乏點什麼?沒錯,就是缺乏研發精神,缺乏屬於Farseer.Net獨特的一面,有種寄人(第三方框架)籬下的感受。因此決定仍是徹底採用本身的編碼吧。固然在一些處理手法上,也會嘗試去學習他們的優勢。設計模式
本次V1.0的升級,是一個質的改變,(儘量不改變客戶端調用)框架
這10條中的每一條要實現都是比較困難的,暫時計劃是2015年4月初有一個比較完整的版本出來。ide
不瞭解Farseer.Net 的朋友,能夠看看V0.2的教程。其實目前優秀的ORM框架不勝其數,Farseer.Net開源僅是爲了讓你們學習瞭解通常的ORM框架的實現。從中但願你們對裏在面代碼不科學地方,多提出來。讓你們一同進步。學習
下面,放上目前框架中片段代碼,有興趣的朋友,能夠到GitHub處下載。目前框架徹底託管在GitHub中。而且我將天天進行不間斷的更新。優化
1 using System; 2 using FS.Configs; 3 using FS.Core.Data; 4 5 namespace FS.Core.Context 6 { 7 /// <summary> 8 /// 表上下文 9 /// </summary> 10 public class TableContext : IDisposable 11 { 12 /// <summary> 13 /// 經過數據庫配置,鏈接數據庫 14 /// </summary> 15 /// <param name="dbIndex">數據庫選項</param> 16 /// <param name="tableName">表名稱</param> 17 protected internal TableContext(int dbIndex = 0, string tableName = null) : this(DbFactory.CreateConnString(dbIndex), DbConfigs.ConfigInfo.DbList[dbIndex].DataType, DbConfigs.ConfigInfo.DbList[dbIndex].CommandTimeout, tableName) { } 18 19 /// <summary> 20 /// 經過自定義數據連接符,鏈接數據庫 21 /// </summary> 22 /// <param name="connectionString">數據庫鏈接字符串</param> 23 /// <param name="dbType">數據庫類型</param> 24 /// <param name="commandTimeout">SQL執行超時時間</param> 25 /// <param name="tableName">表名稱</param> 26 protected internal TableContext(string connectionString, DataBaseType dbType = DataBaseType.SqlServer, int commandTimeout = 30, string tableName = null) : this(new DbExecutor(connectionString, dbType, commandTimeout), tableName) { } 27 28 /// <summary> 29 /// 事務 30 /// </summary> 31 /// <param name="database">數據庫執行</param> 32 /// <param name="tableName">表名稱</param> 33 protected internal TableContext(DbExecutor database, string tableName = null) 34 { 35 Database = database; 36 TableName = tableName; 37 IsMergeCommand = true; 38 } 39 40 /// <summary> 41 /// 數據庫 42 /// </summary> 43 internal protected DbExecutor Database { get; private set; } 44 45 /// <summary> 46 /// 合併執行命令 47 /// </summary> 48 internal protected bool IsMergeCommand { get; set; } 49 50 /// <summary> 51 /// 表名 52 /// </summary> 53 internal protected string TableName { get; protected set; } 54 55 /// <summary> 56 /// 保存修改 57 /// IsMergeCommand=true時:只提交一次SQL到數據庫 58 /// </summary> 59 public int SaveChanges() 60 { 61 return -1; 62 } 63 64 /// <summary> 65 /// 釋放資源 66 /// </summary> 67 public void Dispose() 68 { 69 Database.Dispose(); 70 Database = null; 71 } 72 } 73 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq.Expressions; 4 using FS.Core.Infrastructure; 5 6 namespace FS.Core.Context 7 { 8 public class TableSet<TEntity> : IDisposable where TEntity : class, new() 9 { 10 /// <summary> 11 /// 數據庫上下文 12 /// </summary> 13 private TableContext<TEntity> _dbContext; 14 15 /// <summary> 16 /// 禁止外部實例化 17 /// </summary> 18 private TableSet() { } 19 20 internal TableSet(TableContext<TEntity> dbContext) : this() 21 { 22 _dbContext = dbContext; 23 QueryProvider = DbFactory.CreateQuery(_dbContext); 24 } 25 26 /// <summary> 27 /// 數據庫查詢支持 28 /// </summary> 29 private IQuery QueryProvider { get; set; } 30 31 /// <summary> 32 /// 字段選擇器 33 /// </summary> 34 /// <param name="select">字段選擇器</param> 35 public TableSet<TEntity> Select<T>(Expression<Func<TEntity, T>> select) 36 { 37 //QueryProvider.QueryQueue.ExpSelect = QueryProvider.QueryQueue.ExpSelect == null ? QueryProvider.QueryQueue.ExpSelect = select : Expression.Add(QueryProvider.QueryQueue.ExpSelect, select); 38 return this; 39 } 40 41 /// <summary> 42 /// 查詢條件 43 /// </summary> 44 /// <param name="where">查詢條件</param> 45 public TableSet<TEntity> Where(Expression<Func<TEntity, bool>> where) 46 { 47 //QueryProvider.QueryQueue.ExpWhere = QueryProvider.QueryQueue.ExpWhere == null ? QueryProvider.QueryQueue.ExpWhere = where : Expression.Add(QueryProvider.QueryQueue.ExpWhere, where); 48 return this; 49 } 50 51 public TableSet<TEntity> Desc<TKey>(Expression<Func<TEntity, TKey>> desc) 52 { 53 //QueryProvider.QueryQueue.ExpOrderBy = QueryProvider.QueryQueue.ExpOrderBy == null ? QueryProvider.QueryQueue.ExpOrderBy = desc : Expression.Add(QueryProvider.QueryQueue.ExpOrderBy, desc); 54 return this; 55 } 56 57 public TableSet<TEntity> Asc<TKey>(Expression<Func<TEntity, TKey>> asc) 58 { 59 //QueryProvider.QueryQueue.ExpOrderBy = QueryProvider.QueryQueue.ExpOrderBy == null ? QueryProvider.QueryQueue.ExpOrderBy = asc : Expression.Add(QueryProvider.QueryQueue.ExpOrderBy, asc); 60 return this; 61 } 62 public List<TEntity> ToList() 63 { 64 return QueryProvider.QueryQueue.List.Query<TEntity>(); 65 } 66 67 public TEntity ToInfo() 68 { 69 return QueryProvider.QueryQueue.Info.Query<TEntity>(); 70 } 71 72 public TEntity Update(TEntity entity) 73 { 74 QueryProvider.QueryQueue.Update.Query(entity); 75 return entity; 76 } 77 78 public int Delete() 79 { 80 return QueryProvider.QueryQueue.Delete.Query<TEntity>(); 81 } 82 83 public TEntity Insert(TEntity entity) 84 { 85 QueryProvider.QueryQueue.Insert.Query(entity); 86 return entity; 87 } 88 89 public void Dispose() 90 { 91 throw new NotImplementedException(); 92 } 93 } 94 }
1 using System.Collections.Generic; 2 using FS.Core.Context; 3 using FS.Core.Infrastructure; 4 5 namespace FS.Core.Client.SqlServer 6 { 7 public class SqlServerQuery : IQuery 8 { 9 /// <summary> 10 /// 組列表 11 /// </summary> 12 private List<IQueryQueue> GroupQueryQueueList { get; set; } 13 14 public SqlServerQuery(TableContext tableContext) 15 { 16 TableContext = tableContext; 17 Init(); 18 } 19 20 public TableContext TableContext { get; private set; } 21 public IQueryQueue QueryQueue { get; set; } 22 23 public void Execute() 24 { 25 GroupQueryQueueList.Add(QueryQueue); 26 Init(); 27 } 28 29 public void Init() 30 { 31 QueryQueue = new SqlServerQueryQueue(this); 32 if (GroupQueryQueueList == null) { GroupQueryQueueList = new List<IQueryQueue>(); } 33 } 34 } 35 }
1 using System.Linq.Expressions; 2 using System.Text; 3 using FS.Core.Client.SqlServer.Query; 4 using FS.Core.Infrastructure; 5 using FS.Core.Infrastructure.Query; 6 7 namespace FS.Core.Client.SqlServer 8 { 9 public class SqlServerQueryQueue : IQueryQueue 10 { 11 private readonly IQuery _queryProvider; 12 public Expression ExpOrderBy { get; set; } 13 public Expression ExpSelect { get; set; } 14 public Expression ExpWhere { get; set; } 15 public StringBuilder Sql { get; set; } 16 public SqlServerQueryQueue(IQuery queryProvider) 17 { 18 _queryProvider = queryProvider; 19 } 20 21 private IQueryQueueList _list; 22 public IQueryQueueList List { get { return _list ?? (_list = new SqlServerQueryList(_queryProvider)); } } 23 24 25 private IQueryQueueInfo _info; 26 public IQueryQueueInfo Info { get { return _info ?? (_info = new SqlServerQueryInfo(_queryProvider)); } } 27 28 29 private IQueryQueueInsert _insert; 30 public IQueryQueueInsert Insert { get { return _insert ?? (_insert = new SqlServerQueryInsert(_queryProvider)); } } 31 32 33 private IQueryQueueUpdate _update; 34 public IQueryQueueUpdate Update { get { return _update ?? (_update = new SqlServerQueryUpdate(_queryProvider)); } } 35 36 37 private IQueryQueueDelete _delete; 38 public IQueryQueueDelete Delete { get { return _delete ?? (_delete = new SqlServerQueryDelete(_queryProvider)); } } 39 40 public void Dispose() 41 { 42 Sql.Clear(); 43 ExpOrderBy = null; 44 ExpSelect = null; 45 ExpWhere = null; 46 Sql = null; 47 _list = null; 48 _info = null; 49 _insert = null; 50 _update = null; 51 _delete = null; 52 } 53 } 54 }
可能當你看到以上代碼時,已是陳舊了。關注最新代碼,請到GitHub中。ui
1 using (var context = new TableContext<UserPO>()) 2 { 3 var info = context.TableSet.Where(o => o.ID > 0).Desc(o => new { o.ID, o.LoginCount }).Asc(o => o.GenderType).ToInfo(); 4 info.PassWord = "123456"; 5 6 context.TableSet.Update(info); 7 context.TableSet.Insert(info); 8 9 10 var lst = context.TableSet.Where(o => o.ID > 0).Desc(o => new { o.ID, o.LoginCount }).Asc(o => o.GenderType).ToList(); 11 12 context.SaveChanges(); 13 }
1 TableContext<UserPO>.Data.Select(o => o.ID).Where(o => o.ID > 0).ToList(); this
QQ羣:116228666 (Farseer.net開源框架交流) 請註明:Farseer.Net編碼