设为首页 加入收藏

TOP

扩展MongoDB C# Driver的QueryBuilder(一)
2015-11-21 00:57:45 来源: 作者: 【 】 浏览:5
Tags:扩展 MongoDB Driver QueryBuilder
扩展MongoDB C# Driver的QueryBuilder


由于不想直接hardcode ClassA.MemberA.MemberB 这样的字符串 ,写了以下几个类,用于以下常用的场景:
1. 表达式转换成字符串函数: ExpToStr()
2. Collection函数:当有集合成员时,可以使用此类,将返回QueryCollection对象,这个类的代码之后附上
3. CollectionAs函数:当使用了继承,希望将基类转换为子类并返回子类的QueryCollection


使用示例:


//获得表达式的字符串形式
1. QueryEx
  
   .ExpToStr ((ClassA m)=> m.MemberA.MemberB.MemberC)


//集合.成员.字段
//PoppedSegments为集合,AssignedNetwork.Name为成员
//将返回PoppedSegments.AssignedNetwork.Name
2. QueryEx
   
    .Collection(x => x.PoppedSegments).Matches(p => p.AssignedNetwork.Name, bsonRegex), //子类集合.成员.字段 //STPaymentTransaction为基类,STPaymentCompanyCredit为子类,Company字段在子类中 //将返回Payments.Company.Name 3. QueryEx
    
     .CollectionAs
     
      (x=>x.Payments).Matches(p=>p.Company.Name, bsonRegex) //集合.集合.成员.字段 //Parcels为集合,STCustomPropertyRuntime为基类,STNumericPropertyRuntime为子类,CustomProps为STNumericPropertyRuntime中成员,Value为CustomProp中成员 //将返回Parcels.CustomProps.Value 4. QueryEx
      
       .Collection(x=>x.Parcels).CollectionMemberAs
       
        (p=>p.CustomProps).Matches(p=>p.Value, bsonRegex),
       
      
     
    
   
  




实现代码:


public class QueryEx
  
   
    {
        public static QueryCollection
   
     Collection
    
     ( Expression
     
      >> collectionExpression) { return new QueryCollection
      
       (collectionExpression); } //for those cases using inheritance //e.g STPaymentTransaction //Payments will return STPaymentTransaction //need to cast to sub classes(STPaymentCompanyCredit) so that be able to filter by child members (e.g. Company) public static QueryCollection
       
         CollectionAs
        
         ( Expression
         
          >> collectionExpression) where TSub : TCollection { var argParam = Expression.Parameter(typeof (TDocument), x); var memberStr = ExpToStr(collectionExpression); MemberExpression nameProperty = Expression.Property(argParam, memberStr); var subExp = Expression.Convert(nameProperty, typeof(IEnumerable
          
           )); var exp = Expression.Lambda
           
            >>( subExp, argParam); return new QueryCollection
            
             (exp); } /// 
              /// return string value for a expression: /// for s.Name.Val1.Val2 will return Name.Val1.Val2 ///  /// 
              /// 
              ///
              /// 
              public static string ExpToStr
             
              (Expression
              
               > exp) { return new QueryExpressionHelper().MemberExpression(exp); } } public class QueryCollection
               
                 { private readonly QueryExpressionHelper _queryExpression; private string _collectionName; public string Context { get { return _collectionName; } } public QueryCollection(Expression
                
                 >> collectionExpression) { _queryExpression = new QueryExpressionHelper(); _collectionName = _queryExpression.MemberExpression(collectionExpression); } public QueryMember
                 
                   Member
                  
                   (Expression
                   
                    > exp) { var expStr = QueryEx
                    
                     .ExpToStr(exp); var context = string.Format({0}.{1}, _collectionName, expStr); var obj = new QueryMember
                     
                      (context); return obj; } public QueryCollection
                      
                        CollectionMember
                       
                        ( Expression
                        
                         >> exp) { var expStr = QueryEx
                         
                          .ExpToStr(exp); var obj = new QueryCollection
                          
                           (exp) { _collectionName = string.Format({0}.{1}, _collectionName, expStr) }; return obj; } /// 
                            /// this method only support 1 layer nested(not for Query Collection.Collection , but for Collection.Member) /// if member is collection and need convert to sub class ///  /// 
                           
                            Base Type
                            /// 
                           
                            Child Class Type
                            ///
                            /// 
                            public QueryCollection
                           
                             CollectionMemberAs
                            
                             ( Expression
                             
                              >> collectionExpression) where TMemberSub : TMember { var obj = QueryEx
                              
                               .CollectionAs
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 2488 A Knight's Journey.. 下一篇leetCode 32.Longest Valid Paren..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: