Skip to content

Commit

Permalink
added MongoIdAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggwilson committed Jul 9, 2010
1 parent 79c181a commit e1630d0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions source/MongoDB/Attributes/MongoAliasAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
17 changes: 17 additions & 0 deletions source/MongoDB/Attributes/MongoIdAttribute.cs
Original file line number Diff line number Diff line change
@@ -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()
{ }
}
}
11 changes: 10 additions & 1 deletion source/MongoDB/Configuration/Mapping/Auto/AutoMappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
12 changes: 12 additions & 0 deletions source/MongoDB/Linq/Translators/QueryBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions source/MongoDB/MongoDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down

0 comments on commit e1630d0

Please sign in to comment.