Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added MongoIdAttribute

  • Loading branch information...
commit e1630d029eaf9d3617d8986b436c78b7b3e75523 1 parent 79c181a
Craig Wilson authored July 09, 2010
3  source/MongoDB/Attributes/MongoAliasAttribute.cs
@@ -21,6 +21,9 @@ public sealed class MongoAliasAttribute : Attribute
21 21
         public MongoAliasAttribute(string name){
22 22
             if(name == null)
23 23
                 throw new ArgumentNullException("name");
  24
+            if (name == "_id")
  25
+                throw new ArgumentException("_id is a reserved alias.");
  26
+
24 27
             Name = name;
25 28
         }
26 29
     }
17  source/MongoDB/Attributes/MongoIdAttribute.cs
... ...
@@ -0,0 +1,17 @@
  1
+using System;
  2
+
  3
+namespace MongoDB.Attributes
  4
+{
  5
+    /// <summary>
  6
+    /// 
  7
+    /// </summary>
  8
+    [AttributeUsage(AttributeTargets.Property)]
  9
+    public sealed class MongoIdAttribute : Attribute
  10
+    {
  11
+        /// <summary>
  12
+        /// Initializes a new instance of the <see cref="MongoIdAttribute"/> class.
  13
+        /// </summary>
  14
+        public MongoIdAttribute()
  15
+        { }
  16
+    }
  17
+}
11  source/MongoDB/Configuration/Mapping/Auto/AutoMappingProfile.cs
@@ -95,7 +95,16 @@ public MemberInfo FindExtendedPropertiesMember(Type classType)
95 95
         /// <returns></returns>
96 96
         public MemberInfo FindIdMember(Type classType)
97 97
         {
98  
-            return _conventions.IdConvention.GetIdMember(classType);
  98
+            var members = (from memberInfo in _memberFinder.FindMembers(classType)
  99
+                          let att = memberInfo.GetCustomAttribute<MongoIdAttribute>(true)
  100
+                          where att != null
  101
+                          select memberInfo).ToList();
  102
+
  103
+            if (members.Count > 1)
  104
+                throw new InvalidOperationException("Cannot have more than 1 member marked with a MongoId Attribute.");
  105
+            if(members.Count == 0)
  106
+                return _conventions.IdConvention.GetIdMember(classType);
  107
+            return members[0];
99 108
         }
100 109
 
101 110
         /// <summary>
12  source/MongoDB/Linq/Translators/QueryBinder.cs
@@ -273,6 +273,18 @@ private Expression BindAggregate(Expression source, MethodInfo method, LambdaExp
273 273
             return subquery;
274 274
         }
275 275
 
  276
+        private Expression BindAny(Expression source, LambdaExpression predicate)
  277
+        {
  278
+            var projection = VisitSequence(source);
  279
+            _map[predicate.Parameters[0]] = projection.Projector;
  280
+            var where = Visit(predicate.Body);
  281
+            var alias = new Alias();
  282
+            var fieldProjection = _projector.ProjectFields(projection.Projector, alias, projection.Source.Alias);
  283
+            return new ProjectionExpression(
  284
+                new SelectExpression(alias, fieldProjection.Fields, projection.Source, where),
  285
+                fieldProjection.Projector);
  286
+        }
  287
+
276 288
         private Expression BindDistinct(Expression source)
277 289
         {
278 290
             var projection = VisitSequence(source);
1  source/MongoDB/MongoDB.csproj
@@ -102,6 +102,7 @@
102 102
     <Compile Include="..\..\AssemblyInfoGlobal.cs">
103 103
       <Link>AssemblyInfoGlobal.cs</Link>
104 104
     </Compile>
  105
+    <Compile Include="Attributes\MongoIdAttribute.cs" />
105 106
     <Compile Include="BinarySubtype.cs" />
106 107
     <Compile Include="Bson\BsonType.cs" />
107 108
     <Compile Include="Bson\BsonInfo.cs" />

0 notes on commit e1630d0

Please sign in to comment.
Something went wrong with that request. Please try again.