MongoDB layer-3 operations

Source: Internet
Author: User
Tags findone mongodb driver dota
Today, I don't know which tendon is wrong. I want to write an article without Dota. In fact, I don't know what to write, but I really want to write it, I am not good enough talent, so I can only write technical articles! The half bottle can still be swaying. Haha ~ NOSQL has gained a good momentum recently. MongoDB is one of the most elegant ones. They learn nosql on their own.

Today, I don't know which tendon is wrong. I want to write an article without Dota. In fact, I don't know what to write, but I really want to write it, I am not good enough talent, so I can only write technical articles! The half bottle can still be swaying. Haha ~ NOSQL has gained a good momentum recently. MongoDB is one of the most elegant ones. They learn nosql on their own.

Today, I don't know which tendon is wrong. I want to write an article without Dota. In fact, I don't know what to write, but I really want to write it, I am not good enough talent, so I can only write technical articles! The half bottle can still be swaying. Haha ~

NOSQL has gained a good momentum recently. MongoDB is also the best among them. When I learned nosql, I also referred to a large amount of data. I decided to start with MongoDB for two important reasons: 1. I am a simple enthusiast. I am wondering if there are any simple solutions to all the problems. I 'd rather stop and think about it for a long time than do it in a stupid way, most MongoDB operations are simple. 2. I am a JS enthusiast. I like to read a js book from the beginning to the end, no matter how much I remember, no matter how much I don't need to use it, I like it, mongoDB is stored in the BSON format, so it is easy to operate! Now we are working on a project that uses MongoDB as the database. Without DAL at first, BLL directly accesses the database and then goes to the UI, Hong Kong Space, and BLL is completely static (I like the simple call of static methods, but it cannot be inherited by the malicious static class !), At that time, it was too straightforward to use the MongoDB driver! You don't need to write another DAL !, Later I knew that my thoughts were still naive, haha! Let's talk about the current operation method ~

I. Module layer

[Serializable] user {public ObjectId id; public string n; public int age; public Birthday birth; Birthday {public int y; public int m; public int d ;}}

At first glance, there are several irregularities in the VM. The first and public fields of Class 1 are not capitalized, and 2 are public fields, without attributes, 3. The field name does not indicate its meaning. Now let's explain one by one. If the class name and field do not have an upper-case letter, the names in the database follow Javascript. When using js, we generally write the following:

Var user = {id: ObjectId ("123456"), n: "loogn", age: 23, birth: {y: 1989, m: 7, d: 7 }}

However, it may be said that you can use an Attribute association such as MongoDB. Bson. Serialization. Attributes. BsonElement! But I won't do that, because it is too troublesome! There may still be questions here, so we will leave it to 3rd questions.

It is unreasonable to publish fields without using attributes. school teachers have handed in fields. Whether you can understand them or not, private fields are required. If you want to publish fields, use attributes. Haha! However, after thinking for a long time, I still don't take the ordinary path. So far, no defect problem has occurred in the field. I don't guarantee it will happen in the future, but I feel very low, even if you have any questions, you must use the attributes. Adding {get; set;} to the backend is not troublesome! After all, the attribute is still a method. There is overhead for calling the method with the attribute, and the object class is originally an unusual class. Generally, it only indicates the object state (using fields ), if there is logic in the attribute (as the teacher often said, the age cannot be less than 0 and cannot be greater than 150, etc !), Will you do it here? Obviously, you are doing it in BLL! The field name is too short to express its meaning. In fact, this can be used together with the first one. MongoDB has no mode. The same collection can save multiple irregular documents, for example, the user set:

{Id: 1, n: "user1", desc: "My description"} {id: 2, n: "user2 "}

Therefore, the database must store the name (id, name, desc, id, name) of each element in the Document. Therefore, the shorter the element name, the more space it will be, and the more expressive it is with name, n is used here. In fact, most people can accept common conventions.

There is also an embedded class Birthday in the user, and this class has an upper-case letter. I think so. All the embedded class names follow the C # naming rules, because the container class has a field of the embedded class type, which is birth, but what if the appropriate abbreviation cannot be found? simply use the embedded class name in lower case, such as the built-in City class, if the field name is city, it will not be repeated.

Ii. DAL Layer

At this layer, We need to write a base class. After this base class is completed, all other DAL classes are cloudization ~, Before writing the base class, there is a helper, which is very simple. The code is provided directly without explanation:

MongoServer

Public class extends helper {static readonly extends ConnectionStringBuilder; static extends helper () {]! = Null) {ConnectionStringBuilder =]. ConnectionString) ;}]! = Null) {ConnectionStringBuilder =]. connectionString) ;}else {) ;}} public static Login server GetServer (string connectionString) {var server = Login server. create (connectionString); return server;} public static Login server GetServer () {var server = Login server. create (ConnectionStringBuilder); return server;} public static login database GetDatabase (string databaseName) {return GetServer (). getDatabase (databaseName);} public static relational database GetDatabase () {return GetDatabase (ConnectionStringBuilder. databaseName );}}

After completion, you can write BaseDAL. If there is no generic model, the DAL work is really tasteless, but now it is much more interesting to use the generic DAL, first with the code:

Data access layer BaseDAL {CollectionName {set; get;} sets the collection name SetCollectionName (); private collection M_collection; according to CollectionName, you can get the collect collection object. Collection {get {if (m_collection = null) {CollectionName = SetCollectionName (); m_collection = collect helper. GetDatabase (). GetCollection (CollectionName);} return m_collection;} Get A Document Object TDocument FindOne (IMongoQuery query, PreprocessHandler Preprocess) {var document = Collection. FindOne (query); if (preprocess! = Null) {preprocess (ref document);} return document;} converts cursor to IList type IList CursorToList (cursor Cursor, PreprocessHandler Preprocess) {IList List = new List (30); bool isPreprocess = preprocess! = Null; foreach (TDocument document in cursor) {var doc = document; if (isPreprocess) preprocess (ref doc); list. add (doc);} return list;} query the query set IList Find (IMongoQuery query, descricursorsettings cursorSettings, PreprocessHandler Preprocess) {var cursor = Collection. Find (query); if (cursorSettings! = Null) {cursorSettings. Set (cursor);} var list = CursorToList (cursor, preprocess); return list ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.