本次改动的主要内容是实现MongoDB.Repository对MongoDBRef的支持。

MongoDB对一对一,一对多,多对多关系的维护,官方推荐文档嵌入方式,反映到模型的设计如下:

    public class Student : Entity
{
public string Name { get; set; }
public int Age { get; set; }
} public class Teacher : Entity
{
public string Name { get; set; }
public int Age { get; set; }
} public class Grade : RefEntity
{
public string Name { get; set; }
}
public class School : Entity
{
public string Name { get; set; }
public List<MongoDBRef> Students { get; set; }
public List<MongoDBRef> Teachers { get; set; }
public MongoDBRef Master { get; set; }
}

该种设计在数据库中的存储格式如下图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAn8AAADiCAIAAACwSzYYAAAgAElEQVR4nO2dW5LkOK5Etf9VcWd9P8Yqr4IAHA8+I9OPlbUxKMjpAEFpsjqy53kIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCHkYlprrbXaXT83dh9TCpG7CvpT1nVXL2ftSs3KlxBCyKWMvIBrIvLNnV1ukLjaT6R6S8FVXIcvYEII+eWMv0V+/dt33dJgCb6ACSHkl5N6C/7gzmfXskR+ZsC6ER3XQDAyWwRct4IBQgghv4TUmwC8RSIvDPyKtfTb5wvYinfNBF9p4G0KTKb8pGpOCCHktxF/Zarx7gsvsii4UdVf/fZ1I/n2JYQQUif4iJ/19k29KUHYL3j74iX46iWEkF9L7VUUf/u2f7jBWf25b1/p04q0rq7wYy1NCCHki8k+4t+vKGugvsCsych8NyPHNX28ChCxdCyTVt2koPWREEIIIYSQ389//PM8/53eBUIIIX8LvngeFoEQQshm+OJ5WARCCPnddN97KNw1Hb54HhaBEEJ+N3z73gmLQAghvxm+fe+ERSCEkN+M9fbF71e+fVfDIqzF+q1WK1KO43fhdWv6cVR99Zd3O4fWpDQZL6alL5ew7orMu/qRvICZd4CcBCm7fgr6kXqq4lZwsA7vGZCFi6qjeqjpp9bdrN/lKCct5cF1AT8vHksH91WWiVKpFeX4zeDbd26J8BKL9BeS8l3rZhAJnizbngLgcQCOHNaPmA/qq1IRz5F7I3m5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Ho4w/jnx2S8PWXXKMIw9i1T873kO8+a1+G1xlSjwhZD7yHHZPbXlK45PWVfmG2IDMq3soq5OP8fbd7x/j2rCstu98+4L9Usen9guUvWnnrmuzkSVmxRNClmA9BayrqUlXc+eDQH0iR7w17e0719WIYGdMBdwLPoLg1CpzifSSNZ61elxQDQaTWdvZ7HbuFCEEIU8jfgRYpzeuU3syjj/iu9t/PrreurCaf+xq+gMxXtXUx64mtRUn7qO6p90SZ/dLBge9BVfJZrSi2QghadSnVTevxgORrE7qKTPy4ABPN9VPJP6qB1nnVh1b8erMuw7qJTxjLTpxHy2pYB8upQnwvLwU1M/6SWdCCPmz8Knhgt+OwZeldVfk9sgGLd3HyFuZEEIImUYTP+nityN4MwV//JI/wN3wtrvKDCGEkL8FfvdMeTPVfvYlhBDyPM//AXHA4suO2wkdAAAAAElFTkSuQmCC" alt="" />

其中的Students, Teachers, Master都以嵌入在School集合中,而在相应的Student,Teacher集合中,并没有相关的数据。

如果是多学校,我们要查询系统中所有的学生,或老师,那将是一件非常费力的工作。而我们最终目的应该是在School中存储键值,应该使用MongoDBRef实现,如下:

    public class School : Entity
{
public string Name { get; set; }
public List<MongoDBRef> Students { get; set; }
public List<MongoDBRef> Teachers { get; set; }
public MongoDBRef Master { get; set; }
}

而这种设计,MongoDBRef的数据结构则过于简单,只有Id, CollectionName,DatabaseName三个字段,如果要得到Master,只能去Teacher集合中再次执行查询操作。

而本次提交,解决的正是这个问题,使代码可以写成如school.Pick<Student>(student.Id).Name的形式。

主要接口:IRefEntity,IDBRefContainer。

IRefEntity:为减少数据冗余,并提供一个Update方法。Update方法的逻辑是先保存当前实体,即执行IEntity.Save(),然后对DBRefs中的数据进行保存,接口定义如下:

    public interface IRefEntity : IEntity
{
/// <summary>
/// list of MongoDBRef
/// </summary>
List<MongoDBRef> DBRefs { get; set; } /// <summary>
/// save IEntity first, then save list of MongoDBRef
/// </summary>
void Update();
}

IDBRefContainer接口,为IRefEntity.DBRefs的数据提供一个对应的实体容器,已完成相应的查询、添加、删除操作,接口定义如下:

    public interface IDBRefContainer
{
bool Exists(string id);
bool Exists<T>() where T : IEntity;
bool Exists<T>(Predicate<T> match) where T : IEntity; T Pick<T>(string id) where T : IEntity;
T Pick<T>(Expression<Func<T, bool>> where) where T : IEntity; List<T> GetAll<T>() where T : IEntity;
List<T> GetMany<T>(Expression<Func<T, bool>> where) where T : IEntity; void Add<T>(T entity) where T : IEntity;
void Add<T>(List<T> entities) where T : IEntity; int Remove<T>(Expression<Func<T, bool>> where) where T : IEntity;
void Remove<T>(T entity) where T : IEntity;
void Remove<T>() where T : IEntity; int Count<T>() where T : IEntity;
int Count<T>(Expression<Func<T, bool>> where) where T : IEntity; List<IEntity> GetAll();
}

需要注意的是,尽量使用IEntity.Save()进行保存操作,而减少使用IDBRefEntity.Update()进行更新保存 操作,因为IDBRefEntity.Update()针对的是所有IDBRefContainer中的数据,其执行效率还有待改善和提高。本人也一直在纠结,这个IDBRefEntity.Update()是否需要或者是否应该提供。

下面给出一个具体的测试用例:

        [TestCase]
public void test()
{
grade = new Grade();
grade.Name = "No1";
foreach (Student student in students)
grade.Add<Student>(student);
foreach (Teacher teacher in teachers)
grade.Add<Teacher>(teacher);
grade.Update(); students[].Name = "NameChanged";
students[].Save(); var g = MongoEntity.Get<Grade>(grade.Id); Assert.AreSame(students[].Name, grade.Pick<Student>(students[].Id).Name);
Assert.AreNotSame(grade.Pick<Student>(students[].Id).Name, g.Pick<Student>(students[].Id).Name);
Assert.AreEqual(grade.Count<Student>(), g.Count<Student>());
Assert.AreEqual(grade.Count<Teacher>(), g.Count<Teacher>());
}

具体请参考源码

本人深感能力不足,欢迎大家指正、指教。

MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 二的更多相关文章

  1. MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 一

    本人系新接触MongoDB不久,属于MongoDB的菜鸟范畴.在使用MongoDB的过程中,总结了一些认识,在此总结跟大家分享.欢迎拍砖. 关于MongoDB的内容,在此就不做介绍了,网上有浩如烟海的 ...

  2. MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 三

    本次改动的主要内容是实现MongoDB.Repository在MongoDB中建立索引. 建立索引主要使用MongoDB的官方驱动中EnsureIndex方法. 在MongoDB.Repository ...

  3. MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 六:支持多数据库操作

    本次主要内容:修正MongoDB.Repository框架对多数据库的支持. 在之前的五篇文章中对MongoDB.Repository框架做了简单的介绍是实现思路.之前是考虑MongoDB.Repos ...

  4. MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 五 --- 为List<MongoDBRef>增加扩展方法

    本次改动主要内容:为List<MongoDBRef>增加扩展方法 在MongoDB.Repository的使用过程中,发现在一个类中只定义一个List<MongoDBRef>是 ...

  5. MongoDB:利用官方驱动改装为EF代码风格的MongoDB.Repository框架 四

    本次改动主要实现MongoGridFS功能.实现方式主要使用了MongoGridFS和MongoGridFSFileInfo两个类. 设计思路:定义一个IMongoFile接口并继承IEntity,以 ...

  6. MongoDB Python官方驱动 PyMongo 的简单封装

    最近,需要使用 Python 对 MongodB 做一些简单的操作,不想使用各种繁重的框架.出于可重用性的考虑,想对 MongoDB Python 官方驱动 PyMongo 做下简单封装,百度一如既往 ...

  7. 在.net下打造mongoDb基于官方驱动最新版本

    还是一如既往先把结构图放出来,上上个版本添加了redis的缓存,但是不满足我的需求,因为公司有项目要求是分布式所以呢,这里我就增加了mongoDb进行缓存分布式,好了先看结构图. 总的来说比较蛋疼,因 ...

  8. 使用MongoDB C#官方驱动操作MongoDB

    想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动有很多种,如官方提供的,samus. 实现思路大都类似.这里我们先用官方提供的mongo-csharp-dri ...

  9. 在C#中使用官方驱动操作MongoDB

    MongoDB的官方驱动下载地址:https://github.com/mongodb/mongo-csharp-driver/releases 目前最新的版本是2.10,支持.NET 4.5以上.由 ...

随机推荐

  1. 第20章 状态模式(State Pattern)

    原文 第20章 状态模式(State Pattern) 状态模式  概述:   当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式主要解决的是当控制一个对象状态的条件表 ...

  2. Android入门——电话拨号器和4种点击事件

    关于HelloWorld为,电话拨号程序还AndroidA入门demo,从这个样例我们要理清楚做安卓项目的思路. 大体分为三步: 1.理解需求,理清思路 2.设计UI 3.代码实现 电话拨号器 1.  ...

  3. 【BZOJ3450】【Tyvj1952】Easy 可能DP

    联系: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  4. PsExec.exe执行远程程序

    PsExec.exe \\192.168.1.1 -u username -p password -i -d -s c:\Windows\system32\NETSTAT.exe -a

  5. Oracle 11g password过期被锁定报道 ORA-28000 the account is locked

    一.触发这个错误的原因及相关因素 是因为oracle11g中默认在default概要文件里设置了"PASSWORD_LIFE_TIME=180天"所导致.在Oracle 11g中是 ...

  6. 用HMM(隐马)图解三国杀的于吉“质疑”

    ·背景 最近乘闲暇之余初探了HMM(隐马尔科夫模型),觉得还有点意思,但是网上的教程都超级枯草,可读性很差,抄来抄去的,一堆公式仍在你面前,谁能搞的懂(但园内的两篇写的还算不错.真才实学).在熬制3天 ...

  7. Quartz使用-入门使用(java定时任务实现)

    注:这里使用的是Quartz1.6.5版本号(包:quartz-1.6.5.jar) //測试main函数 //QuartzTest.java package quartzPackage; impor ...

  8. 关于phpcms中模块_tag.class.php中的pc_tag()方法的含义

    关于phpcms中模块_tag.class.php中的pc_tag()方法的含义:        在phpcms系统中自己写后台模块,要在前台模板中显示出来须要在\phpcms\modules\自定义 ...

  9. 运维自动化之SALTSTACK简单入门

    运维自动化之SaltStack简单入门 饱食终日而无所事事,是颓也,废也.但看昨日,费九牛二虎之力除一BUG便流连于新番之中,不知东方之既黑,实乃颓颓然而荒废矣.故今日来缀一文以忏昨日之悔. Salt ...

  10. DDD分层架构之值对象(介绍篇)

    DDD分层架构之值对象(介绍篇) 前面介绍了DDD分层架构的实体,并完成了实体层超类型的开发,同时提供了验证方面的支持.本篇将介绍另一个重要的构造块——值对象,它是聚合中的主要成分. 如果说你已经在使 ...