Permalink
Browse files

added MongoIdAttribute

  • Loading branch information...
1 parent 79c181a commit e1630d029eaf9d3617d8986b436c78b7b3e75523 @craiggwilson craiggwilson committed Jul 9, 2010
View
3 source/MongoDB/Attributes/MongoAliasAttribute.cs
@@ -21,6 +21,9 @@ public sealed class MongoAliasAttribute : Attribute
public MongoAliasAttribute(string name){
if(name == null)
throw new ArgumentNullException("name");
+ if (name == "_id")
+ throw new ArgumentException("_id is a reserved alias.");
+
Name = name;
}
}
View
17 source/MongoDB/Attributes/MongoIdAttribute.cs
@@ -0,0 +1,17 @@
+using System;
+
+namespace MongoDB.Attributes
+{
+ /// <summary>
+ ///
+ /// </summary>
+ [AttributeUsage(AttributeTargets.Property)]
+ public sealed class MongoIdAttribute : Attribute
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MongoIdAttribute"/> class.
+ /// </summary>
+ public MongoIdAttribute()
+ { }
+ }
+}
View
11 source/MongoDB/Configuration/Mapping/Auto/AutoMappingProfile.cs
@@ -95,7 +95,16 @@ public MemberInfo FindExtendedPropertiesMember(Type classType)
/// <returns></returns>
public MemberInfo FindIdMember(Type classType)
{
- return _conventions.IdConvention.GetIdMember(classType);
+ var members = (from memberInfo in _memberFinder.FindMembers(classType)
+ let att = memberInfo.GetCustomAttribute<MongoIdAttribute>(true)
+ where att != null
+ select memberInfo).ToList();
+
+ if (members.Count > 1)
+ throw new InvalidOperationException("Cannot have more than 1 member marked with a MongoId Attribute.");
+ if(members.Count == 0)
+ return _conventions.IdConvention.GetIdMember(classType);
+ return members[0];
}
/// <summary>
View
12 source/MongoDB/Linq/Translators/QueryBinder.cs
@@ -273,6 +273,18 @@ private Expression BindAggregate(Expression source, MethodInfo method, LambdaExp
return subquery;
}
+ private Expression BindAny(Expression source, LambdaExpression predicate)
+ {
+ var projection = VisitSequence(source);
+ _map[predicate.Parameters[0]] = projection.Projector;
+ var where = Visit(predicate.Body);
+ var alias = new Alias();
+ var fieldProjection = _projector.ProjectFields(projection.Projector, alias, projection.Source.Alias);
+ return new ProjectionExpression(
+ new SelectExpression(alias, fieldProjection.Fields, projection.Source, where),
+ fieldProjection.Projector);
+ }
+
private Expression BindDistinct(Expression source)
{
var projection = VisitSequence(source);
View
1 source/MongoDB/MongoDB.csproj
@@ -102,6 +102,7 @@
<Compile Include="..\..\AssemblyInfoGlobal.cs">
<Link>AssemblyInfoGlobal.cs</Link>
</Compile>
+ <Compile Include="Attributes\MongoIdAttribute.cs" />
<Compile Include="BinarySubtype.cs" />
<Compile Include="Bson\BsonType.cs" />
<Compile Include="Bson\BsonInfo.cs" />

0 comments on commit e1630d0

Please sign in to comment.