C # Development MONGO note Eighth

Source: Internet
Author: User

Until today, MONGO development of the Rights management system function is done, the rest is to improve the structure, optimize the construction, but now C # operation MONGO all kinds of operations basically have mastered the

When it comes to the rights management system, in fact, in the era of relational data, we usually build a menu table, the menu is built to manage the submenu, and then build a role table (or user table), and then build a corresponding relationship table of roles and menus, save the menu ID and role ID corresponding relationship. Of course, the method is many, I just

A relatively common design approach is presented.

After using the MONGO I copied the previous design ideas, completed the Authority management, after the completion of the sudden feel that this is not very good. Comparing MONGO is a loose document structure, and the idea of object-oriented is similar, so the design looks too disappointments. So I decided to try to redesign it in the MONGO format.

First, the first step is to change the menu, the menu is no longer used in the built-in, but the handle menu as a field in the parent menu data. Then the character is changed, the table of the character and menu is removed, and the menu is directly present in the character list.

After the completion of the transformation has been some of the non-adaptation, when the query is indeed a lot of convenience.

 Public   classMenu { PublicObjectId _id {Get;Set; }  Public stringName {Get;Set; }  Public stringURL {Get;Set; }  Public stringIcon {Get;Set; }  Public intOrderNo {Get;Set; }  Public stringDESCR {Get;Set; }  Publiclist<menu> Child =NewList<menu>(); }
     Public classRole { PublicObjectId _id {Get;Set; }  Public stringName {Get;Set; }  Public stringRootmenu {Get;Set; }  Public stringChildmenu {Get;Set; }  PublicList<menu> Menus =NewList<menu>(); }

After this transformation, the addition of methods and query methods did not change much, at first do not know how to add and delete children.

Later found that the official driver is still very powerful, we can directly modify the menu or Role object, and then save the object's modification structure on it, feeling and ef very similar to.

And now click on the menu to send back an ID I don't know if it's a one-level menu or a level two menu, because the level two menu is not a data, so it can't be queried directly. My current approach is to check whether it is a first-level menu, if not, then query the level two menu.

This method is very easy to use, of course, we certainly have a better way, I hope everyone is not hesitate to enlighten ah.

Put a way to modify the menu.

ObjectId MenuID = Objectid.parse (request.form["MenuID"]); varquery = Query.and (Query.eq ("child._id", MenuID)); Menu Menu=DAL.                Dalmenu.getmenu (query); if(Menu = =NULL) {Query= Query.and (Query.eq ("_id", MenuID)); Menu=DAL.                Dalmenu.getmenu (query); }                if(menu. Child.count (m = m._id = = MenuID) >0) {Menu child= menu. Child.firstordefault (m = m._id = =menuid); Child. Name= request.form["name"]; Child. Icon= request.form["icon"]; Child. URL= request.form["URL"]; Child. OrderNo= Convert.ToInt32 (request.form["OrderNo"]); Child. DESCR= request.form["Descr"];                              Dalmenu.update (menu); }                Else{menu. Name= request.form["name"]; Menu. Icon= request.form["icon"]; Menu. URL= request.form["URL"]; Menu. OrderNo= Convert.ToInt32 (request.form["OrderNo"]); Menu. DESCR= request.form["Descr"]; DAL.                Dalmenu.update (menu); }

The final effect is as follows

The final storage structure of the database, such as the database Chinese display garbled problem I also solved, as long as the command window to change the code, the default is GBK, changed to Utf-8

Although the role of the table is a bit bloated, but every time the user login must be to display the menu, the data are to be queried, so bloated also does not matter.

If you need to find a submenu under role, I'll read its first-level menu collection and then use LINQ to query its level two and level three menus.

Role role=Dalrole.getbyid (Roleid); List<Menu> Rolemenus = role. Menus;//Next-level menu collection for this role            foreach(varRolemenuinchRolemenus)//Cycle One-level menu{CStr+="<div id=\ "M"+ rolemenu._id +"\ "style=\" width:150px;\ ">"; List<Menu> Childmenus =Rolemenu.                child; if(Childmenus. Count >0)//There's a level two menu .{str+="<a href=\ "#\" class=\ "easyui-menubutton\" data-options=\ "menu: ' #m"+ rolemenu._id +"', Iconcls: '"+ Rolemenu. Icon +"' \ ' >"+ Rolemenu. Name +"</a>"; foreach(varMinchChildmenus) {CStr+=Getchildmenus (M.child, Roleid, m._id, M.name, M.url); }                }                Else{str+="<a href=\ "#\" onclick=\ "AddTab ('"+ Rolemenu. name+"', '"+ Rolemenu. url+"') \ "class=\" easyui-linkbutton\ "data-options=\" plain:true,iconcls: '"+ Rolemenu. Icon +"' \ ' >"+ Rolemenu. name+"</a>"; } CStr+="</div>"; }

Paste it out to play a role in it.

In fact, MONGO's official drive itself is very strong, more in line with everyone's C # development habits, so the operation is relatively simple. By the way, the time to find out will be smaller than the actual time, need to add the attribute to mark

  /// <summary>      /// default time is 8 hours      less than real time /// </summary>      [Bsondatetimeoptions (Kind = datetimekind.local)]           Public Get set; }

I have basically summed up the problems I encountered, I think we may be in the first contact when the same problem will be encountered, so sort out to share, but also as one of their own study notes.

The next step is to organize the framework, if you have any experience after the reorganization, I will also take out to share

C # Development MONGO note Eighth

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.