Skip to content

Commit

Permalink
Allow to read and write mapped protected object property.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 8, 2010
1 parent 44fc7b2 commit 262f39c
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,23 @@ public void CanSetProtectedProperty()
Assert.IsNotNull(prop);
Assert.AreEqual(4, prop.GetProperty());
}

public class SetPrivatePropertys
{
private double Property { get; set; }

public double GetProperty() { return Property; }
}

[Test]
public void CanNotSetPrivatePropertys()
{
var bson = Serialize(new Document("Property", 4));

var prop = Deserialize<SetPrivatePropertys>(bson);

Assert.IsNotNull(prop);
Assert.AreEqual(0, prop.GetProperty());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public AutoMappingProfile()
{
_conventions = new ConventionProfile();
_isSubClass = t => false;
_memberFinder = PublicMemberFinder.Instance;
_memberFinder = PublicOrProtectedMemberFinder.Instance;
}

/// <summary>
Expand Down
42 changes: 0 additions & 42 deletions source/MongoDB/Configuration/Mapping/Auto/PublicMemberFinder.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace MongoDB.Configuration.Mapping.Auto
{
/// <summary>
///
/// </summary>
public class PublicOrProtectedMemberFinder : IMemberFinder
{
///<summary>
///</summary>
public static readonly PublicOrProtectedMemberFinder Instance = new PublicOrProtectedMemberFinder();

/// <summary>
/// Initializes a new instance of the <see cref="PublicOrProtectedMemberFinder"/> class.
/// </summary>
private PublicOrProtectedMemberFinder()
{
}

/// <summary>
/// Finds the members.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public IEnumerable<MemberInfo> FindMembers(Type type)
{
foreach (var prop in type.GetProperties(BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public))
{
var getMethod = prop.GetGetMethod(true);
var setMethod = prop.GetSetMethod(true);

if(getMethod==null||setMethod==null)
continue;

if(getMethod.IsPrivate||setMethod.IsPrivate)
continue;

yield return prop;
}

foreach (var field in type.GetFields())
{
if (!field.IsInitOnly && !field.IsLiteral)
yield return field;
}
}
}
}
4 changes: 3 additions & 1 deletion source/MongoDB/Configuration/Mapping/AutoMappingStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public AutoMappingStore(IAutoMapper autoMapper, IMappingStore mappingStore)
/// <returns></returns>
public IClassMap GetClassMap(Type classType)
{
IClassMap classMap = null;
IClassMap classMap;

if (_autoMaps.TryGetValue(classType, out classMap))
return classMap;

Expand All @@ -80,6 +81,7 @@ public IClassMap GetClassMap(Type classType)
classMap = _autoMapper.CreateClassMap(classType, GetClassMap);

_autoMaps.Add(classType, classMap);

return classMap;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public ConventionProfile()
_collectionAdapterConvention = DefaultCollectionAdapterConvention.Instance;
_defaultValueConvention = DefaultDefaultValueConvention.Instance;
_discriminatorConvention = new DelegateDiscriminatorConvention(t => t.Name);
_discriminatorAliasConvention = new DelegateDiscriminatorAliasConvention(delegate(Type t) { return "_t"; });
_discriminatorAliasConvention = new DelegateDiscriminatorAliasConvention(t => "_t");
_extendedPropertiesConvention = new DelegateExtendedPropertiesConvention(m => m.Name == "ExtendedProperties");
_idConvention = new DelegateIdConvention(m => m.Name == "Id");
_idGeneratorConvention = DefaultIdGeneratorConvention.Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ public class DefaultDefaultValueConvention : IDefaultValueConvention
/// </summary>
public static readonly DefaultDefaultValueConvention Instance = new DefaultDefaultValueConvention();

/// <summary>
/// Initializes a new instance of the <see cref="DefaultDefaultValueConvention"/> class.
/// </summary>
private DefaultDefaultValueConvention()
{ }
{
}

/// <summary>
/// Gets the default value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class DelegateIdConvention : MemberFinderBase, IIdConvention
/// <param name="predicate">The predicate.</param>
public DelegateIdConvention(Func<MemberInfo, bool> predicate)
: base(predicate)
{ }
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DelegateIdConvention"/> class.
Expand All @@ -24,7 +25,8 @@ public DelegateIdConvention(Func<MemberInfo, bool> predicate)
/// <param name="bindingFlags">The binding flags.</param>
public DelegateIdConvention(Func<MemberInfo, bool> predicate, MemberTypes memberTypes, BindingFlags bindingFlags)
: base(predicate, memberTypes, bindingFlags)
{ }
{
}

/// <summary>
/// Gets the member representing the id if one exists.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace MongoDB.Configuration.Mapping.Conventions
Expand All @@ -19,8 +16,8 @@ public abstract class MemberFinderBase
/// Initializes a new instance of the <see cref="MemberFinderBase"/> class.
/// </summary>
/// <param name="predicate">The predicate.</param>
public MemberFinderBase(Func<MemberInfo, bool> predicate)
: this(predicate, MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public)
protected MemberFinderBase(Func<MemberInfo, bool> predicate)
: this(predicate, MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
{ }

/// <summary>
Expand All @@ -29,7 +26,7 @@ public MemberFinderBase(Func<MemberInfo, bool> predicate)
/// <param name="predicate">The predicate.</param>
/// <param name="memberTypes">The member types.</param>
/// <param name="bindingFlags">The binding flags.</param>
public MemberFinderBase(Func<MemberInfo, bool> predicate, MemberTypes memberTypes, BindingFlags bindingFlags)
protected MemberFinderBase(Func<MemberInfo, bool> predicate, MemberTypes memberTypes, BindingFlags bindingFlags)
{
_bindingFlags = bindingFlags;
_memberTypes = memberTypes;
Expand All @@ -44,6 +41,7 @@ public MemberFinderBase(Func<MemberInfo, bool> predicate, MemberTypes memberType
protected MemberInfo GetMember(Type type)
{
var foundMembers = type.FindMembers(_memberTypes, _bindingFlags, IsMatch, null);

if (foundMembers.Length == 0)
return null;
if (foundMembers.Length == 1)
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/MongoDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
<Compile Include="Configuration\Mapping\Auto\IAutoMappingProfile.cs" />
<Compile Include="Configuration\Mapping\Auto\IMemberFinder.cs" />
<Compile Include="Configuration\Mapping\Auto\MemberOverrides.cs" />
<Compile Include="Configuration\Mapping\Auto\PublicMemberFinder.cs" />
<Compile Include="Configuration\Mapping\Auto\PublicOrProtectedMemberFinder.cs" />
<Compile Include="Configuration\Mapping\Conventions\ConventionProfile.cs" />
<Compile Include="Configuration\Mapping\Conventions\DefaultCollectionAdapterConvention.cs" />
<Compile Include="Configuration\Mapping\Conventions\DefaultDefaultValueConvention.cs" />
Expand Down
3 changes: 0 additions & 3 deletions source/MongoDB/Serialization/Descriptors/BsonPropertyValue.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MongoDB.Serialization.Descriptors
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public override IEnumerable<BsonProperty> GetProperties()
foreach (var memberMap in ClassMap.MemberMaps)
yield return CreateProperty(memberMap.Alias, GetValue(memberMap.MemberName));

if (_extendedProperties != null)
{
foreach (string propertyName in _extendedProperties.Keys)
yield return CreateProperty(propertyName, GetValue(propertyName));
}
if (_extendedProperties == null)
yield break;

foreach(var propertyName in _extendedProperties.Keys)
yield return CreateProperty(propertyName, GetValue(propertyName));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson;

namespace MongoDB.Serialization.Descriptors
Expand All @@ -25,10 +26,17 @@ public DocumentPropertyDescriptor(Document document)
/// <returns></returns>
public IEnumerable<BsonProperty> GetProperties()
{
foreach (var pair in _document)
yield return new BsonProperty(pair.Key) { Value = GetValue(pair.Value) };
return _document.Select(pair => new BsonProperty(pair.Key)
{
Value = GetValue(pair.Value)
});
}

/// <summary>
/// Gets the value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
private BsonPropertyValue GetValue(object value)
{
var valueType = value == null ? typeof(Document) : value.GetType();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using MongoDB.Bson;

namespace MongoDB.Serialization.Descriptors
Expand Down

0 comments on commit 262f39c

Please sign in to comment.