For the MongoDB package is still continuing, for the continuous pursuit of simple programming continues, for like the Code of the feeling, still continue ...
When your MONGO data table has sub-objects, especially the list objects, the use of the official driver is very uncomfortable, to remember a lot of new object types, trouble, so, to encapsulate it, let Getmodel support anonymous parameters!
The table structure may be this way
The statement that you want to query becomes this
Look at the above statement feel pretty cool, hehe, look at the implementation of the code below, written this afternoon, hehe!
PublicIenumerable<tentity> getmodel<u>(U template) {return_table. Find (generatormongoquery (template)). Tolistasync (). Result; } PublicPagedresult<tentity> getmodel<u> (intPageIndex,intpageSize) { return This. Getmodel (New{}, PageIndex, pageSize); } PublicPagedresult<tentity> getmodel<u> (U template,intPageIndex,intpageSize) { return This. Getmodel (Template,New{}, PageIndex, pageSize); } PublicPagedresult<tentity> Getmodel<u, o> (U template, O by,intPageIndex,intpageSize) { #regionConditional filteringbsondocumentfilterdefinition<TEntity> filterdefinition =generatormongoquery (template); #endregion #regionSorting processingsortdefinition<TEntity> sorts =NewObjectsortdefinition<tentity> (New { }); foreach(varIteminch typeof(O). GetProperties ()) {if((OrderType) item. GetValue ( by) ==ordertype.asc) Sorts=sorts. Ascending (item. Name); ElseSorts=sorts. Descending (item. Name); } #endregion #regionPagination processingvarSkip = (PageIndex-1) *pageSize; varRecordCount = _table. Find (filterdefinition). Countasync (NewCancellationToken ()). Result; varLimit =pageSize; return NewPagedresult<tentity>(RecordCount, (int) (RecordCount + pageSize-1) /pageSize, PageSize, PageIndex, _table. Find (filterdefinition). Sort (sorts). Skip (Skip). Limit (limit). Tolistasync (). Result); #endregion }
A private method of conditional filtering is proposed, because its logic is used in two methods, so it is extracted
/// <summary> ///constructs a query expression for MONGO, via an anonymous object/// </summary> /// <typeparam name= "U" ></typeparam> /// <param name= "template" ></param> /// <returns></returns> PrivateBsondocumentfilterdefinition<tentity> generatormongoquery<u>(U template) {varQtype =typeof(U); varOutter =Newbsondocument (); varSimpleQuery =Newbsondocument (); foreach(varIteminchqtype.getproperties ()) { if(item. Propertytype.isclass && Item. PropertyType! =typeof(string)) { //complex types, navigation properties, class objects, and collection objects foreach(varSubinchitem. Propertytype.getproperties ()) {Simplequery.add (NewBsonelement (item. Name +"."+Sub. Name, Bsonvalue.create (sub. GetValue (item. GetValue (template)))); } } Else { //simple types, valuetype and stringSimplequery.add (Newbsonelement (item. Name, Bsonvalue.create (item. GetValue (template))); } } return NewBsondocumentfilterdefinition<tentity>(SimpleQuery); }
The result is that we want to be good, how to use it very friendly
MongoDB Learning Notes ~ Anonymous objects as query parameters, easy to query sub-objects