zoukankan      html  css  js  c++  java
  • 关于Farseer.net轻量级ORM开源框架 V1.0 概念版本开发的消息

    V0.2版的开源距离今天(05年03月)已有近3年的时间。可以说这个版本已经有点落伍的感觉了,呵呵。

    V0.2版至今一直处于BUG的修复及一些细小功能的增加,所以版本号上一直没有变化。

    其实在这1、2年中,我一直在想着Farseer.Net 的未来发展状况。有尝试用EF的核心、也有想过用NHibernate的核心。仅仅是在这些核心的基础下做二次开发,以个人编码的经验从客户端调用角度进行“优化”,但总是感觉缺少点什么?没错,就是缺少研发精神,缺少属于Farseer.Net独特的一面,有种寄人(第三方框架)篱下的感觉。所以决定还是完全采用自己的编码吧。当然在一些处理手法上,也会尝试去学习他们的优点。

    V1.0目标

    本次V1.0的升级,是一个质的改变,(尽可能不改变客户端调用)

    1. 完全重写内核代码(FS.Core);
    2. 以面向接口的设计模式进行编写。
    3. 实现批量SQL的传输(相对V0.2,是每一条SQL就与数据库交互的)。
    4. 实现延迟加载。
    5. 内置内存数据库。(有时候为了做一些小尝试,没必要去专门创建一个数据库,或者无法联网的情况下)
    6. 考虑到可能有些人只用到本框架ORM部份,因此非必要的代码,独立为一个类库,按实现需要附加。
    7. 分离表、存储过程、视图。
    8. Farseer.Net未提供到的一些SQL高级运用,支持自定义SQL。
    9. 更易于扩展新的SQL的方法。(比如V0.2中,GroupBy/Join是未支持的。需要支持的话,对于V0.2来说,是比较困难的(维护))。
    10. 提供与V0.2方式一样的数据访问以外,增加数据仓库的方式。第三条说到的批量SQL,是将执行中的SQL,加到组队列中(SQL被暂存到每个队列中,来等待被执行)

    这10条中的每一条要实现都是比较困难的,暂时计划是2015年4月初有一个比较完整的版本出来。

    不了解Farseer.Net 的朋友,可以看看V0.2的教程。其实目前优秀的ORM框架不胜其数,Farseer.Net开源仅是为了让大家学习了解一般的ORM框架的实现。从中希望大家对里在面代码不科学地方,多提出来。让大家一同进步。

    开源地址:GitHub

    下面,放上目前框架中片断代码,有兴趣的朋友,可以到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中。

    客户端调用
     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(); 

    基地交流

    QQ群:116228666 (Farseer.net开源框架交流) 请注明:Farseer.Net

  • 相关阅读:
    第十九章:UTC time和local time的互换
    第二十章:安全性
    第十三章:基于socket.io实现即时通信
    第三章:ionic环境搭建之windows篇
    第十八章:自定义splash screen、app icon和tab icon
    第十七章:使用media插件来播放声音
    第一章:hybrid app开发之技术选型
    第十六章:自定义push notification sound
    第十五章:集成JPUSH
    ionic resources
  • 原文地址:https://www.cnblogs.com/steden/p/4321773.html
Copyright © 2011-2022 走看看