Permalink
Browse files

added configuration support for Id.

  • Loading branch information...
1 parent bb7b492 commit ce841dadbbb5479d26f4186dc436763a0f5c5654 @craiggwilson craiggwilson committed Jun 20, 2010
View
2 source/MongoDB.Tests/UnitTests/Configuration/MongoConfigurationBuilderTests.cs
@@ -36,7 +36,7 @@ public void Test()
mapping.Map<Person>(m =>
{
- m.CollectionName("people");
+ m.CollectionName("people");
m.Member(x => x.Age).Alias("age");
m.Member(x => x.Name).Alias("name").DefaultValue("something").Ignore();
});
View
67 source/MongoDB/Configuration/Builders/ClassOverridesBuilder.cs
@@ -32,6 +32,44 @@ internal ClassOverridesBuilder(ClassOverrides overrides)
public void CollectionName(string name)
{
_overrides.CollectionName = name;
+ }
+
+ /// <summary>
+ /// Ids the specified member.
+ /// </summary>
+ /// <param name="member">The member.</param>
+ /// <returns></returns>
+ public IdOverridesBuilder Id(MemberInfo member)
+ {
+ var overrides = new IdOverrides { Member = member };
+ return new IdOverridesBuilder(overrides);
+ }
+
+ /// <summary>
+ /// Ids the specified name.
+ /// </summary>
+ /// <param name="name">The name.</param>
+ /// <returns></returns>
+ public IdOverridesBuilder Id(string name)
+ {
+ var members = typeof(T).GetMember(name, MemberTypes.Field | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
+ if (members == null || members.Length == 0)
+ throw new InvalidOperationException("No member was found.");
+ if (members.Length > 1)
+ throw new InvalidOperationException("More than one member matched the specified name.");
+
+ return Id(members[0]);
+ }
+
+ /// <summary>
+ /// Ids the specified member.
+ /// </summary>
+ /// <param name="member">The member.</param>
+ /// <returns></returns>
+ public IdOverridesBuilder Id(Expression<Func<T, object>> member)
+ {
+ var mex = GetMemberExpression(member);
+ return Id(mex.Member.Name);
}
/// <summary>
@@ -67,15 +105,26 @@ public MemberOverridesBuilder Member(string name)
/// <param name="member">The member.</param>
/// <returns></returns>
public MemberOverridesBuilder Member(Expression<Func<T, object>> member)
- {
- var memberExpression = member.Body as MemberExpression;
- if (memberExpression == null)
- {
- var unaryExpression = member.Body as UnaryExpression;
- memberExpression = unaryExpression.Operand as MemberExpression;
- }
-
- return Member(memberExpression.Member.Name);
+ {
+ var mex = GetMemberExpression(member);
+ return Member(mex.Member.Name);
+ }
+
+ /// <summary>
+ /// Gets the member expression.
+ /// </summary>
+ /// <param name="member">The member.</param>
+ /// <returns></returns>
+ private MemberExpression GetMemberExpression(Expression<Func<T, object>> member)
+ {
+ var memberExpression = member.Body as MemberExpression;
+ if (memberExpression == null)
+ {
+ var unaryExpression = member.Body as UnaryExpression;
+ memberExpression = unaryExpression.Operand as MemberExpression;
+ }
+
+ return memberExpression;
}
}
}
View
43 source/MongoDB/Configuration/Builders/IdOverridesBuilder.cs
@@ -0,0 +1,43 @@
+using System;
+using MongoDB.Configuration.Mapping.Auto;
+using MongoDB.Configuration.IdGenerators;
+
+namespace MongoDB.Configuration.Builders
+{
+ /// <summary>
+ ///
+ /// </summary>
+ public class IdOverridesBuilder
+ {
+ private readonly IdOverrides _overrides;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MemberOverridesBuilder"/> class.
+ /// </summary>
+ /// <param name="overrides">The overrides.</param>
+ internal IdOverridesBuilder(IdOverrides overrides)
+ {
+ if (overrides == null)
+ throw new ArgumentNullException("overrides");
+
+ _overrides = overrides;
+ }
+
+ public IdOverridesBuilder GeneratedBy<T>() where T : IIdGenerator, new()
+ {
+ return GeneratedBy(new T());
+ }
+
+ public IdOverridesBuilder GeneratedBy(IIdGenerator generator)
+ {
+ _overrides.Generator = generator;
+ return this;
+ }
+
+ public IdOverridesBuilder UnsavedValue(object unsavedValue)
+ {
+ _overrides.UnsavedValue = unsavedValue;
+ return this;
+ }
+ }
+}
View
12 source/MongoDB/Configuration/Mapping/Auto/ClassOverrides.cs
@@ -8,7 +8,8 @@ namespace MongoDB.Configuration.Mapping.Auto
/// </summary>
public class ClassOverrides
{
- readonly Dictionary<MemberInfo, MemberOverrides> _memberOverrides;
+ private IdOverrides _idOverrides;
+ private readonly Dictionary<MemberInfo, MemberOverrides> _memberOverrides;
/// <summary>
/// Gets or sets the name of the collection.
@@ -25,6 +26,15 @@ public ClassOverrides()
}
/// <summary>
+ /// Gets the id overrides.
+ /// </summary>
+ /// <returns></returns>
+ public IdOverrides GetIdOverrides()
+ {
+ return _idOverrides;
+ }
+
+ /// <summary>
/// Gets the overrides for.
/// </summary>
/// <param name="memberInfo">The member info.</param>
View
30 source/MongoDB/Configuration/Mapping/Auto/IdOverrides.cs
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Reflection;
+using MongoDB.Configuration.IdGenerators;
+
+namespace MongoDB.Configuration.Mapping.Auto
+{
+ public class IdOverrides
+ {
+ /// <summary>
+ /// Gets or sets the member.
+ /// </summary>
+ /// <value>The member.</value>
+ public MemberInfo Member { get; set; }
+
+ /// <summary>
+ /// Gets or sets the generator.
+ /// </summary>
+ /// <value>The generator.</value>
+ public IIdGenerator Generator { get; set; }
+
+ /// <summary>
+ /// Gets or sets the unsaved value.
+ /// </summary>
+ /// <value>The unsaved value.</value>
+ public object UnsavedValue { get; set; }
+ }
+}
View
45 source/MongoDB/Configuration/Mapping/Auto/OverridableAutoMappingProfile.cs
@@ -48,7 +48,11 @@ public MemberInfo FindExtendedPropertiesMember(Type classType)
/// <returns></returns>
public MemberInfo FindIdMember(Type classType)
{
- return _profile.FindIdMember(classType);
+ return GetIdOverrideValue(
+ classType,
+ o => o.Member,
+ m => m != null,
+ _profile.FindIdMember(classType));
}
/// <summary>
@@ -173,7 +177,11 @@ public string GetDiscriminatorAlias(Type classType)
/// <returns></returns>
public IIdGenerator GetIdGenerator(Type classType, MemberInfo member)
{
- return _profile.GetIdGenerator(classType, member);
+ return GetIdOverrideValue(
+ classType,
+ o => o.Generator,
+ g => g != null,
+ _profile.GetIdGenerator(classType, member));
}
/// <summary>
@@ -184,7 +192,11 @@ public IIdGenerator GetIdGenerator(Type classType, MemberInfo member)
/// <returns></returns>
public object GetIdUnsavedValue(Type classType, MemberInfo member)
{
- return _profile.GetIdUnsavedValue(classType, member);
+ return GetIdOverrideValue(
+ classType,
+ o => o.UnsavedValue,
+ v => v != null,
+ _profile.GetIdUnsavedValue(classType, member));
}
/// <summary>
@@ -233,16 +245,23 @@ private T GetClassOverrideValue<T>(Type classType, Func<ClassOverrides, T> overr
return !accept(value) ? defaultValue : value;
}
- /// <summary>
- /// Gets the member override value.
- /// </summary>
- /// <typeparam name = "T"></typeparam>
- /// <param name = "classType">Type of the class.</param>
- /// <param name = "member">The member.</param>
- /// <param name = "overrides">The overrides.</param>
- /// <param name = "accept">The accept.</param>
- /// <param name = "defaultValue">The default value.</param>
- /// <returns></returns>
+ private T GetIdOverrideValue<T>(Type classType,
+ Func<IdOverrides, T> overrides,
+ Func<T, bool> accept,
+ T defaultValue)
+ {
+ if (!_overrides.HasOverridesForType(classType))
+ return defaultValue;
+
+ var idOverrides = _overrides.GetOverridesForType(classType).GetIdOverrides();
+ if (idOverrides == null)
+ return defaultValue;
+
+ var value = overrides(idOverrides);
+
+ return !accept(value) ? defaultValue : value;
+ }
+
private T GetMemberOverrideValue<T>(Type classType,
MemberInfo member,
Func<MemberOverrides, T> overrides,
View
2 source/MongoDB/MongoDB.csproj
@@ -111,12 +111,14 @@
<Compile Include="Bson\BsonWriterSettings.cs" />
<Compile Include="Bson\IBsonObjectBuilder.cs" />
<Compile Include="Bson\IBsonObjectDescriptor.cs" />
+ <Compile Include="Configuration\Builders\IdOverridesBuilder.cs" />
<Compile Include="Configuration\CollectionAdapters\ArrayCollectionAdapter.cs" />
<Compile Include="Configuration\Builders\MappingStoreBuilder.cs" />
<Compile Include="Configuration\DictionaryAdapters\GenericDictionaryDictionaryAdapter.cs" />
<Compile Include="Configuration\DictionaryAdapters\HashtableDictionaryAdapter.cs" />
<Compile Include="Configuration\DictionaryAdapters\DocumentDictionaryAdapter.cs" />
<Compile Include="Configuration\DictionaryAdapters\IDictionaryAdapter.cs" />
+ <Compile Include="Configuration\Mapping\Auto\IdOverrides.cs" />
<Compile Include="Configuration\Mapping\Conventions\DefaultDictionaryAdapterConvention.cs" />
<Compile Include="Configuration\Mapping\Conventions\IDictionaryAdapterConvention.cs" />
<Compile Include="Configuration\Mapping\Util\ValueConverter.cs" />

0 comments on commit ce841da

Please sign in to comment.