当前位置:Gxlcms > 数据库问题 > 本人为巨衫数据库(开源NoSQL)写的C#驱动,支持Linq,全部开源,已提交github

本人为巨衫数据库(开源NoSQL)写的C#驱动,支持Linq,全部开源,已提交github

时间:2021-07-01 10:21:17 帮助过:5人阅读

Insert BsonDocument insertor = new BsonDocument(); insertor.Add("Last Name", "Lin"); insertor.Add("First Name", "Hetiu"); insertor.Add("Address", "SYSU"); BsonDocument sInsertor = new BsonDocument(); sInsertor.Add("Phone", "10086"); sInsertor.Add("EMail", "hetiu@yahoo.com.cn"); insertor.Add("Contact", sInsertor); ObjectId insertID = (ObjectId)coll.Insert(insertor); Assert.IsNotNull(insertID); // Update DBQuery query = new DBQuery(); BsonDocument updater = new BsonDocument(); BsonDocument matcher = new BsonDocument(); BsonDocument modifier = new BsonDocument(); updater.Add("Age", 25); modifier.Add("$set", updater); matcher.Add("First Name", "Hetiu"); query.Matcher = matcher; query.Modifier = modifier; coll.Update(query); // Query DBCursor cursor = coll.Query(query); Assert.IsNotNull(cursor); BsonDocument bson = cursor.Next(); Assert.IsNotNull(bson); Assert.IsTrue(bson["First Name"].AsString.Equals("Hetiu")); Assert.IsTrue(bson["Age"].AsInt32.Equals(25)); // Delete BsonDocument drop = new BsonDocument(); drop.Add("Last Name", "Lin"); coll.Delete(drop); query.Matcher = drop; cursor = coll.Query(query); Assert.IsNotNull(cursor); bson = cursor.Next(); Assert.IsNull(bson);

     集合查询:

for (int i = 0; i < 10; ++i)
            {
                string date = DateTime.Now.ToString();
                BsonDocument insertor = new BsonDocument();
                insertor.Add("operation", "Query");
                insertor.Add("date", date);
                coll.Insert(insertor);
            }
            BsonDocument matcher = new BsonDocument();
            DBQuery query = new DBQuery();
            matcher.Add("operation", "Query");
            query.Matcher = matcher;
            query.ReturnRowsCount = 5;
            query.SkipRowsCount = 5;
            DBCursor cursor = coll.Query(query);
            Assert.IsNotNull(cursor);
            int count = 0;
            while (cursor.Next() != null)
            {
                ++count;
                BsonDocument bson = cursor.Current();
                Assert.IsNotNull(bson);
            }
            Assert.IsTrue(count == 5);

     官方的代码有点简单,这不符合我们写代码的风格,目前业务系统大量的使用对象操作和Linq处理,原始的Bson接口,这个不科学。

五、完善改造SequoiaDB的C#驱动

     即然官方的驱动太简单,不支持对象处理,也不支持Linq,很不科学,那么应该怎么办呢,其实第一个观点当然是放弃,我们原本使用MongoDB跑的好好的,为什么要给自己找事呢,但是出于项目运维的观点,以及支持国人产品的想法,最终决定自己完善和写一个。

     那么如何来写呢,当然是他山之石,可以攻玉,因为之前做MongoDB开发,原始的驱动配置我们的ORM跑起来也有一些问题,最早我们使用的非MongoDB的官方驱动,而是第三方驱动samus,不支持Decimal类型,但是我们项目之中有大量的Decimal类型,那么办呢,修改驱动,后来我们又换成了MongoDB的官方驱动,因为XmlIgnore标签和Id映射的问题也认真的读过MongoDB的官方驱动,对MongoDB的C#驱动比较熟悉。

     所以完善SequoiaDB的C#的思路就变成了结合SequoiaDB的原始驱动和MongoDB的官方驱动,提供一个类似于MongoDB驱动的操作风格的驱动,在SequoiaDB驱动的基础上提供了,直接操作C#对象的方案和支持Linq进行查询、修改、删除的功能。

     经本人完善修改之后的驱动的操作风格如下:

Sequoiadb sdb = new Sequoiadb("192.168.23.57:50000");
            sdb.Connect("", "");

            //求集合空间。
            var cs = sdb.GetCollecitonSpace("dbo");

            //求集合。
            var coll = cs.GetCollection<HFareDetail>();

            //执行数据插入。
            List<HFareDetail> vList =null;
            using (AgileHIS.Entities.DbEntities db = new AgileHIS.Entities.DbEntities())
            {
                vList = db.HFareDetails.ToList();
                //插入。
                foreach (var item in vList)
                {
                    coll.Insert(item);
                }
                System.Console.WriteLine(string.Format("insert {0} records", vList.Count));
                System.Console.ReadLine();
            }

            //按条件修改某一条数据的几个属性值。
            var v1 = vList.FirstOrDefault();
            v1.Name = string.Empty;
            v1.Cash = decimal.Zero;
            coll.Update(v1, p => p.ID == v1.ID);
            //按条件指量修改,指定某几个必,其他属性全部置空。
            coll.Update(p => new HFareDetail { Cash = decimal.Zero, Name = string.Empty, Price = decimal.Zero }, p => p.ChargeTime >DateTime.Now.AddDays(-1));
            //依据条件删除
            coll.Delete(p => p.ChargeTime > DateTime.Now.AddDays(-1));

            //求Count
            int count = coll.AsQueryable<HFareDetail>()
                .Where(p => p.SourceID==0)
                .Count();

            //Linq查询Take\Skip。
            var vList2 = coll.AsQueryable<HFareDetail>()
                .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                .Skip(10).Take(1000)
                .ToList();
            System.Console.WriteLine(string.Format("query {0} records", vList.Count));

            //Linq查询过。
            var vFare = coll.AsQueryable<HFareDetail>()
                .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                .FirstOrDefault();
            System.Console.WriteLine(vFare);

            //Linq\聚合运算,目前因为测试驱动报错,暂未实现
            var sum = coll.AsQueryable<HFareDetail>()
                .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                .Sum(p => p.Cash);

            System.Console.ReadLine();

     看看,代码是不是很清爽,很方便了呢,没有了bson,只有对象,Linq。

六、SequoiaDB、MongoDB与AgileEAS.NET SOA整合

     AgileEAS.NET SOA之前只支持MongoDB,最近要支持SequoiaDB,我们就得考虑对原有代码的兼容,或者说,更希望自己的医疗系统能够在业务上同时支持MongoDB和SequoiaDB,达到使用环境之中不管是选择MongoDB还是选择SequoiaDB都是同样的代码,为此,我们在AgileEAS.NET SOA中间件之中定义了一个IStructDbProvider接口:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace EAS.Data
{
    /// <summary>
    /// 结构化数据库提供者接口定义。
    /// </summary>
    /// <remarks>
    /// 为AgileEAS.NET SOA 中间件NoSQL数据访问提供标准接口定义。
    /// </remarks>
    public interface IStructDbProvider
    {
        /// <summary>
        /// 打开连接。
        /// </summary>
        void Connect();

        /// <summary>
        /// 关闭连接。
        /// </summary>
        void Close();

        /// <summary>
        /// 连接是否打开。
        /// </summary>
        bool IsOpen { get; }

        /// <summary>
        /// 对象插入。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="item">对象实例。</param>
        void Insert<T>(T item) where T : class;

        /// <summary>
        /// 对象批量插入。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="items">对象实例。</param>
        void InsertBatch<T>(System.Collections.Generic.IEnumerable<T> items) where T : class;

        /// <summary>
        /// 根据条件执行更新操作。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="updater">更新表达式。</param>
        /// <param name="func">查询条件。</param>
        void Update<T>(Expression<Func<T, T>> updater, Expression<Func<T, bool>> func) where T : class;

        /// <summary>
        /// 根据条件执行更新操作。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="item">更新对象。</param>
        /// <param name="func">查询条件。</param>
        void Update<T>(T item, System.Linq.Expressions.Expression<Func<T, bool>> func) where T : class;

        /// <summary>
        /// 根据条件删除对象。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="func">条件表达式。</param>
        void Delete<T>(Expression<Func<T, bool>> func) where T : class;
        
        /// <summary>
        /// 求出Linq查询表达式。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <returns>对象表达式包装。</returns>
        IQueryableWarp<T> Linq<T>() where T : class;

        /// <summary>
        /// 根据条件查询数制。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="where">条件。</param>
        /// <param name="skip">跳过记录数。</param>
        /// <param name="take">取记录数。</param>
        /// <returns>查询结构。</returns>
        List<T> List<T>(Expression<Func<T, bool>> where, int skip, int take) where T : class;

        /// <summary>
        /// 根据条件求单条记录。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="where">条件。</param>
        /// <returns>对象实例。</returns>
        T Single<T>(Expression<Func<T, bool>> where) where T : class;
    }
}

     IStructDbProvider字面意思即为结构化数据访问提供者接口,本接口定义在EAS.MicroKernel.dll程序集之中,AgileEAS.NET SOA中间件同时提供了针对SequoiaDB和MongoDB数据库的IStructDbProvider实现,EAS.Data.MongoDbProvider和EAS.Data.SequoiaDbProvider,这两个实现类定义在EAS.Data.NoSQL.dll程序集之中。

     因为统计使用了IStructDbProvider接口,我们针对SequoiaDB和MongoDB的操作处理就统计成了如下代码:

var vContainer = EAS.Context.ContextHelper.GetContext().Container;
            var dbProvider = vContainer.GetComponentInstance("StructDbProvider") as IStructDbProvider;

            //执行数据插入。
            List<HFareDetail> vList = null;
            using (AgileHIS.Entities.DbEntities db = new AgileHIS.Entities.DbEntities())
            {
                vList = db.HFareDetails.ToList();
                //插入。
                foreach (var item in vList)
                {
                    dbProvider.Insert<HFareDetail>(item);
                }
                System.Console.WriteLine(string.Format("insert {0} records", vList.Count));
                System.Console.ReadLine();
            }

            //按条件修改某一条数据的几个属性值。
            var v1 = vList.FirstOrDefault();
            v1.Name = string.Empty;
            v1.Cash = decimal.Zero;
            dbProvider.Update<HFareDetail>(v1, p => p.ID == v1.ID);
            //按条件指量修改,指定某几个必,其他属性全部置空。
            dbProvider.Update<HFareDetail>(p => new HFareDetail { Cash = decimal.Zero, Name = string.Empty, Price = decimal.Zero }, p => p.ChargeTime > DateTime.Now.AddDays(-1));
            //依据条件删除
            dbProvider.Delete<HFareDetail>(p => p.ChargeTime > DateTime.Now.AddDays(-1));

            //求Count
            using (var queryWarp = dbProvider.Linq<HFareDetail>())
            {
                int count = queryWarp.Queryable
                .Where(p => p.SourceID == 0)
                .Count();

                //Linq查询Take\Skip。
                var vList2 = queryWarp.Queryable
                    .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                    .Skip(10).Take(1000)
                    .ToList();
                System.Console.WriteLine(string.Format("query {0} records", vList.Count));

                //Linq查询过。
                var vFare = queryWarp.Queryable
                    .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                    .FirstOrDefault();
                System.Console.WriteLine(vFare);

                //Linq\聚合运算,目前因为测试驱动报错,暂未实现
                var sum = queryWarp.Queryable
                    .Where(p => p.CreateTime > DateTime.Now.Date.AddMonths(-12))
                    .Sum(p => p.Cash);
            }

            System.Console.ReadLine();

     具体是使用SequoiaDB还是使用MongoDB由系统配置文件来决定,使用SequoiaDB:

<!--StructDb/SequoiaDb-->
      <object name="StructDbProvider" assembly="EAS.Data.NoSQL" type="EAS.Data.SequoiaDbProvider" LifestyleType="Thread">
        <property name="ConnectionString" type="string" value="192.168.23.57:50000"/>
        <property name="UserName" type="string" value=""/>
        <property name="Password" type="string" value=""/>
        <property name="CollectionSpace" type="string" value="his"/>
      </object>

     使用MongoDB。

<!--StructDb/MongoDb-->
      <object name="StructDbProvider" assembly="EAS.Data.NoSQL" type="EAS.Data.MongoDbProvider" LifestyleType="Thread">
        <property name="ConnectionString" type="string" value="mongodb://sa:sa@127.0.0.1:2222/his"/>
        <property name="DbName" type="string" value="his"/>
      </object>

七、SequoiaDB的C#驱动源代码托管、下载

     本人为SequoiaDB所写的C#驱动,已提交托管到github,项目地址https://github.com/agilelab/SequoiaDB.Charp,欢迎大家下载,也欢迎大家和本人一道完善本驱动。

八、联系我们

     敏捷软件工程实验室,是一家研究、推广和发展新技术,并致力于提供具有自主知识产权的业务基础平台软件,以及基于业务基础平台开发的管理软件的专业软件提供商。主要业务是为客户提供软件企业研发管理解决方案、企业管理软件开发,以及相关的技术支持,管理及技术咨询与培训业务。

     AgileEAS.NET SOA中间件平台自2004年秋呱呱落地一来,我就一直在逐步完善和改进,也被应用于保险、医疗、电子商务、房地产、铁路、教育等多个应用,但一直都是以我个人在推广,2010年因为我辞职休息,我就想到把AgileEAS.NET推向市场,让更多的人使用。

     我的技术团队成员都是合作多年的老朋友,因为这个平台是免费的,所以也没有什么收入,都是由程序员的那种理想与信念坚持,在此我感谢一起奋斗的朋友。

团队网站:http://www.agilelab.cn,

AgileEAS.NET网站:http://www.smarteas.net

官方博客:http://eastjade.cnblogs.com

QQ:47920381,AgileEAS.NET

QQ群:113723486(AgileEAS SOA 平台)/上限1000人

199463175(AgileEAS SOA 交流)/上限1000人

212867943(AgileEAS.NET研究)/上限500人

147168308(AgileEAS.NET应用)/上限500人

172060626(深度AgileEAS.NET平台)/上限500人

116773358(AgileEAS.NET 平台)/上限500人

125643764(AgileEAS.NET探讨)/上限500人

193486983(AgileEAS.NET 平台)/上限500人

邮件:james@agilelab.cn,mail.james@qq.com,

电话:18629261335。

本人为巨衫数据库(开源NoSQL)写的C#驱动,支持Linq,全部开源,已提交github

标签:

人气教程排行