zoukankan      html  css  js  c++  java
  • 第三节 MongoDB下samus源码初探

    第三节 MongoDB下samus源码初探

    上一节我们在samus的simple例子简单的入门了,这一节将要探讨的问题写个简要

    • 对象存储
    • 继续关系对象的存储
    • LINQ体现
    • 类关系分析

    一、对象存储

    继续在samus源码上分析,依然是simple的例子。Demo中.net 3.5特性基本上都涉及了。

    View Code
    public static void Main(string[] args)
    {
    #region 以下为Mongo配置及关系映射部分
    //var config = new MongoConfigurationBuilder();
    //COMMENT OUT FROM HERE
    //config.Mapping(mapping =>
    //{
    // mapping.DefaultProfile(profile =>
    // {
    // profile.SubClassesAre(t => t.IsSubclassOf(typeof(MyClass)));
    // });
    // mapping.Map<MyClass>();
    // mapping.Map<SubClass>();
    //});
    // TO HERE
    #endregion

    //config.ConnectionString("Server=127.0.0.1");
    //using (Mongo mongo = new Mongo(config.BuildConfiguration()))
    using (Mongo mongo = new Mongo("Server=127.0.0.1"))
    {
    mongo.Connect();
    try
    {
    //索引器方式mogo["TestDb"]
    var db = mongo.GetDatabase("TestDb");
    //老版不支持泛型的时候写法是这样的db.GetCollection("Name") ->看源码重构了一个MongoCollection_1类
    //将老版方法重构为db.GetCollection<Documnet>("Name");
    //此方法其实将类名反射为集合 db.GetCollection<MyClass>("MyClass")
    var collection = db.GetCollection<MyClass>();

    //.net 3.5集合初始化特性 创建3个对象(2个MyClass,1个子类SubClass)
    MyClass square = new MyClass()
    {
    Corners
    = 4,
    Name
    = "Square"
    };

    MyClass circle
    = new MyClass()
    {
    Corners
    = 0,
    Name
    = "Circle"
    };

    SubClass sub
    = new SubClass()
    {
    Name
    = "SubClass",
    Corners
    = 6,
    Ratio
    = 3.43
    };

    //保存对象
    collection.Save(square);
    collection.Save(circle);
    collection.Save(sub);

    #region LINQ写法读取数据
    //var superclass = (from item in db.GetCollection<MyClass>("MyClass").Linq()
    // where item.Corners > 1
    // select item.Corners).ToList();

    //var subclass = (from item in db.GetCollection<SubClass>("MyClass").Linq()
    // where item.Ratio > 1
    // select item.Corners).ToList();
    #endregion

    //Lambda写法
    //读取集合MyClass所有对象
    Console.WriteLine("Count by LINQ on typed collection: {0}", collection.Linq().Count(x => x.Corners > 1));
    //读取集合为SubClass的对象(由于SubClass被保存到MyClass集合中了,故结果为0)
    Console.WriteLine("Count by LINQ on typed collection2: {0}", db.GetCollection<SubClass>().Linq().Count(x => x.Corners > 1));
    //Console.WriteLine("Count by LINQ on typed collection3: {0}", db.GetCollection<SubClass>().Count(new { Corners = Op.GreaterThan(1) }));

    //注:op 是Document的子类 虽然我们存的是其它对象,但所有的对象最终都是以Document类型存储的,
    //也就意味检索数据也是Document方式查找或统计了,以下Lambda方式的统计就体现了
    //在基类中大家可以看到都是count(new Document())方式及条件查询的,这里介绍完了,下面的代码就容易理解了
    Console.WriteLine("Count on typed collection: {0}", collection.Count(new { Corners = Op.GreaterThan(1) }));
    var count
    = collection.Count(new Document("Corners", Op.GreaterThan(1)));
    Console.WriteLine(
    "Count: {0}", count);
    Console.ReadKey();
    }
    finally
    {
    mongo.Disconnect();
    }
    }

    //var main = new MainClass();
    //main.Setup();
    //main.Run();

    Console.ReadLine();
    }

    看一下结果:

    在打开mongo.exe查看一下结果

    是不是很奇怪,MyClass,SubClass对象又没有标记为可序列化,如何就被保存下来了?难道是通过反射对实现的,看看源码

    二、继续关系对象的存储

    当数据保存为文档记录时,MongoDB是如何识别这个两类是有继承关系的呢?我们接着下面的代码修改一下继续

    先删除上次的记录,在mongo.exe中运行db.MyClass.drop() 删除集合中的数据,查看更多操作集合的命令可执行db.MyClass.help

    mongo.exe脚本命令官方介绍用的是Javascript脚本,从命名可以看出是它的影子。mongo.exe的脚本学习可参照这个链接

    http://special.csdn.net/mongodb/index.html 

    View Code
    public static void Main(string[] args)
    {
    #region 以下为Mongo配置及关系映射部分
    var config
    = new MongoConfigurationBuilder();
    //COMMENT OUT FROM HERE 建立两者的关系
    config.Mapping(mapping =>
    {
    mapping.DefaultProfile(profile
    =>
    {
    profile.SubClassesAre(t
    => t.IsSubclassOf(typeof(MyClass)));
    });
    mapping.Map
    <MyClass>();
    mapping.Map
    <SubClass>();
    });
    // TO HERE
    #endregion

    config.ConnectionString(
    "Server=127.0.0.1");
    //将配置注入Mongo类中
    using (Mongo mongo = new Mongo(config.BuildConfiguration()))
    {
    mongo.Connect();
    try
    {
    //索引器方式mogo["TestDb"]
    var db = mongo.GetDatabase("TestDb");
    //老版不支持泛型的时候写法是这样的db.GetCollection("Name") ->看源码重构了一个MongoCollection_1类
    //将老版方法重构为db.GetCollection<Documnet>("Name");
    //此方法其实将类名反射为集合 db.GetCollection<MyClass>("MyClass")
    var collection = db.GetCollection<MyClass>();

    //.net 3.5集合初始化特性 创建3个对象(2个MyClass,1个子类SubClass)
    MyClass square = new MyClass()
    {
    Corners
    = 4,
    Name
    = "Square"
    };

    MyClass circle
    = new MyClass()
    {
    Corners
    = 0,
    Name
    = "Circle"
    };

    SubClass sub
    = new SubClass()
    {
    Name
    = "SubClass",
    Corners
    = 6,
    Ratio
    = 3.43
    };

    //保存对象
    collection.Save(square);
    collection.Save(circle);
    collection.Save(sub);

    #region LINQ写法读取数据
    //var superclass = (from item in db.GetCollection<MyClass>("MyClass").Linq()
    // where item.Corners > 1
    // select item.Corners).ToList();

    //var subclass = (from item in db.GetCollection<SubClass>("MyClass").Linq()
    // where item.Ratio > 1
    // select item.Corners).ToList();
    #endregion

    //Lambda写法
    //读取集合MyClass所有对象
    Console.WriteLine("Count by LINQ on typed collection: {0}", collection.Linq().Count(x => x.Corners > 1));
    //读取集合为SubClass的对象(由于SubClass被保存到MyClass集合中了,故结果为0)
    Console.WriteLine("Count by LINQ on typed collection2: {0}", db.GetCollection<SubClass>().Linq().Count(x => x.Corners > 1));
    //Console.WriteLine("Count by LINQ on typed collection3: {0}", db.GetCollection<SubClass>().Count(new { Corners = Op.GreaterThan(1) }));

    //注:op 是Document的子类 虽然我们存的是其它对象,但所有的对象最终都是以Document类型存储的,
    //也就意味检索数据也是Document方式查找或统计了,以下Lambda方式的统计就体现了
    //在基类中大家可以看到都是count(new Document())方式及条件查询的,这里介绍完了,下面的代码就容易理解了
    Console.WriteLine("Count on typed collection: {0}", collection.Count(new { Corners = Op.GreaterThan(1) }));
    var count
    = collection.Count(new Document("Corners", Op.GreaterThan(1)));
    Console.WriteLine(
    "Count: {0}", count);
    Console.ReadKey();
    }
    finally
    {
    mongo.Disconnect();
    }
    }

    //var main = new MainClass();
    //main.Setup();
    //main.Run();

    Console.ReadLine();
    }

    结果:

    再看mongo.exe中的记录:

     

    三、LINQ的体现

    View Code
    #region LINQ写法读取数据
    var myCollection
    = (from item in db.GetCollection<MyClass>("MyClass").Linq()
    where item.Corners > 1
    select item).ToList();
    foreach (MyClass my in myCollection)
    {
    Console.WriteLine(my.Name
    +":"+my.Corners.ToString());
    }

    var subCollection
    = (from item in db.GetCollection<SubClass>("MyClass").Linq()
    where item.Ratio > 1
    select item).ToList();

    foreach (SubClass subClass in subCollection)
    {
    Console.WriteLine(subClass.Name
    +":"+subClass.Ratio.ToString());
    }

    var superclass
    = (from item in db.GetCollection<MyClass>("MyClass").Linq()
    where item.Corners > 1
    select item.Corners).ToList();

    var subclass
    = (from item in db.GetCollection<SubClass>("MyClass").Linq()
    where item.Ratio > 1
    select item.Corners).ToList();


    #endregion

    结果:

    以上就是LINQ的查找数据的方式,更多的方法还要研究一下源码的MongDB下的LINQ文件夹。

    四、类关系分析

    以上四个类是MongoDB的基本业务类,从数据库连接,到实例,集合的CRUD操作。

    我们在看一下配置映射类图:

    配置类组合的类比较多,具体还有哪些功能,在后面的学习过程在继续挖掘了。

    好了这一节就到这里了,继续探讨复杂对象的储存及多组服务器配置。

  • 相关阅读:
    02-自定义CALayer
    01-CALayer的基本操作
    抽屉效果
    手势识别
    事件响应
    寻找最合适的view
    hitTest方法与PointInside方法
    02-事件的产生与传递
    OC图标+启动图
    OC多线程操作
  • 原文地址:https://www.cnblogs.com/dajiang02/p/2079354.html
Copyright © 2011-2022 走看看