Para usar IMemeberMapConvention, debe asegurarse de declarar sus convenciones antes de que se lleve a cabo el proceso de mapeo. O, opcionalmente, elimine las asignaciones existentes y cree otras nuevas.
Por ejemplo, el siguiente es el orden correcto para aplicar una convención:
// first: create the conventions
var myConventions = new ConventionPack();
myConventions.Add(new FooConvention());
ConventionRegistry.Register(
"My Custom Conventions",
myConventions,
t => true);
// only then apply the mapping
BsonClassMap.RegisterClassMap<Foo>(cm =>
{
cm.AutoMap();
});
// finally save
collection.RemoveAll();
collection.InsertBatch(new Foo[]
{
new Foo() {Text = "Hello world!"},
new Foo() {Text = "Hello world!"},
new Foo() {Text = "Hello world!"},
});
Así es como se definió esta convención de muestra:
public class FooConvention : IMemberMapConvention
private string _name = "FooConvention";
#region Implementation of IConvention
public string Name
{
get { return _name; }
private set { _name = value; }
}
public void Apply(BsonMemberMap memberMap)
{
if (memberMap.MemberName == "Text")
{
memberMap.SetElementName("NotText");
}
}
#endregion
}
Estos son los resultados que surgieron cuando ejecuté esta muestra. Podría ver que la propiedad Text terminó siendo guardada como "NotText":