
7 changed files with 115 additions and 22 deletions
-
1Bson/Bson.csproj
-
19Bson/DefaultSerializer/BsonClassMap.cs
-
32Bson/DefaultSerializer/Conventions/ConventionProfile.cs
-
28Bson/DefaultSerializer/Conventions/IdPropertyConventions.cs
-
1BsonUnitTests/BsonUnitTests.csproj
-
2BsonUnitTests/DefaultSerializer/BsonClassMapTests.cs
-
54BsonUnitTests/DefaultSerializer/Conventions/IdPropertyConventionsTests.cs
@ -0,0 +1,28 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Reflection; |
||||
|
|
||||
|
namespace MongoDB.Bson.DefaultSerializer.Conventions |
||||
|
{ |
||||
|
public interface IIdPropertyConvention { |
||||
|
BsonPropertyMap FindIdPropertyMap(IEnumerable<BsonPropertyMap> propertyMaps); |
||||
|
} |
||||
|
|
||||
|
public class NamedIdPropertyConvention : IIdPropertyConvention { |
||||
|
public string Name { get; private set; } |
||||
|
|
||||
|
public NamedIdPropertyConvention( |
||||
|
string name |
||||
|
) { |
||||
|
Name = name; |
||||
|
} |
||||
|
|
||||
|
public BsonPropertyMap FindIdPropertyMap( |
||||
|
IEnumerable<BsonPropertyMap> propertyMaps |
||||
|
) { |
||||
|
return propertyMaps.FirstOrDefault(x => x.PropertyName == Name); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,54 @@ |
|||||
|
/* Copyright 2010 10gen Inc. |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.IO; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using NUnit.Framework; |
||||
|
|
||||
|
using MongoDB.Bson.DefaultSerializer.Conventions; |
||||
|
using MongoDB.Bson; |
||||
|
using MongoDB.Bson.DefaultSerializer; |
||||
|
|
||||
|
namespace MongoDB.BsonUnitTests.DefaultSerializer.Conventions |
||||
|
{ |
||||
|
[TestFixture] |
||||
|
public class IdPropertyConventionsTests { |
||||
|
private class TestClassA { |
||||
|
public Guid Id { get; set; } |
||||
|
public ObjectId OtherId { get; set; } |
||||
|
} |
||||
|
|
||||
|
private class TestClassB { |
||||
|
public ObjectId OtherId { get; set; } |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
public void TestIdPropertyConvention() { |
||||
|
var convention = new NamedIdPropertyConvention("Id"); |
||||
|
|
||||
|
var classAMap = BsonClassMap.RegisterClassMap<TestClassA>(); |
||||
|
var idMap = convention.FindIdPropertyMap(classAMap.PropertyMaps); |
||||
|
Assert.IsNotNull(idMap); |
||||
|
Assert.AreEqual("Id", idMap.PropertyName); |
||||
|
|
||||
|
var classBMap = BsonClassMap.RegisterClassMap<TestClassB>(); |
||||
|
idMap = convention.FindIdPropertyMap(classBMap.PropertyMaps); |
||||
|
Assert.IsNull(idMap); |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue