Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping DynamicComponent to generic IDictionary<string,object> #496

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using FluentNHibernate.Mapping;
using FluentNHibernate.Mapping.Providers;
using FluentNHibernate.MappingModel.ClassBased;
Expand Down Expand Up @@ -86,6 +87,36 @@ public class when_class_map_is_told_to_map_a_dynamic_component_using_reveal : Pr
protected static ClassMapping mapping;
}

public class when_class_map_is_told_to_map_a_generic_dynamic_component : ProviderSpec
{
Because of = () =>
mapping = map_as_class<EntityWithComponent>(m => m.DynamicComponent(x => x.GenericDynamicComponent, c => { }));

Behaves_like<ClasslikeComponentBehaviour> a_component_in_a_classlike;

protected static ClassMapping mapping;
}

public class when_class_map_is_told_to_map_a_generic_dynamic_component_from_a_field : ProviderSpec
{
Because of = () =>
mapping = map_as_class<EntityWithFieldComponent>(m => m.DynamicComponent(x => x.GenericDynamicComponent, c => { }));

Behaves_like<ClasslikeComponentBehaviour> a_component_in_a_classlike;

protected static ClassMapping mapping;
}

public class when_class_map_is_told_to_map_a_generic_dynamic_component_using_reveal : ProviderSpec
{
Because of = () =>
mapping = map_as_class<EntityWithComponent>(m => m.DynamicComponent(Reveal.Member<EntityWithComponent, IDictionary<string, object>>("GenericDynamicComponent"), c => { }));

Behaves_like<ClasslikeComponentBehaviour> a_component_in_a_classlike;

protected static ClassMapping mapping;
}

public class when_class_map_is_told_to_map_a_reference_component : ProviderSpec
{
Because of = () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace FluentNHibernate.Specs.FluentInterface.Fixtures
{
class EntityWithComponent
{
public ComponentTarget Component { get; set; }
public IDictionary DynamicComponent { get; set; }
public IDictionary<string, object> GenericDynamicComponent { get; set; }
}

class EntityWithFieldComponent
{
public ComponentTarget Component;
public IDictionary DynamicComponent;
public IDictionary<string, object> GenericDynamicComponent;
}

class ComponentTarget
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Collections.Generic;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.Specs.FluentInterface.Fixtures;
using Machine.Specifications;
Expand Down Expand Up @@ -65,6 +66,36 @@ public class when_subclass_map_is_told_to_map_a_dynamic_component_using_reveal :
protected static SubclassMapping mapping;
}

public class when_subclass_map_is_told_to_map_a_generic_dynamic_component : ProviderSpec
{
Because of = () =>
mapping = map_as_subclass<EntityWithComponent>(m => m.DynamicComponent(x => x.GenericDynamicComponent, c => { }));

Behaves_like<ClasslikeComponentBehaviour> a_component_in_a_classlike;

protected static SubclassMapping mapping;
}

public class when_subclass_map_is_told_to_map_a_generic_dynamic_component_from_a_field : ProviderSpec
{
Because of = () =>
mapping = map_as_subclass<EntityWithFieldComponent>(m => m.DynamicComponent(x => x.GenericDynamicComponent, c => { }));

Behaves_like<ClasslikeComponentBehaviour> a_component_in_a_classlike;

protected static SubclassMapping mapping;
}

public class when_subclass_map_is_told_to_map_a_generic_dynamic_component_using_reveal : ProviderSpec
{
Because of = () =>
mapping = map_as_subclass<EntityWithComponent>(m => m.DynamicComponent(Reveal.Member<EntityWithComponent, IDictionary<string, object>>("GenericDynamicComponent"), c => { }));

Behaves_like<ClasslikeComponentBehaviour> a_component_in_a_classlike;

protected static SubclassMapping mapping;
}

public class when_subclass_map_is_told_to_map_a_reference_component : ProviderSpec
{
Because of = () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Linq;
using FluentNHibernate.Conventions.Helpers.Builders;
using FluentNHibernate.Conventions.Instances;
using FluentNHibernate.Mapping;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.Testing.DomainModel.Mapping;
using NUnit.Framework;

namespace FluentNHibernate.Testing.ConventionsTests.ApplyingToModel
{
[TestFixture]
public class GenericDynamicComponentConventionTests
{
private PersistenceModel model;

[SetUp]
public void CreatePersistenceModel()
{
model = new PersistenceModel();
}

[Test]
public void ShouldSetAccessProperty()
{
Convention(x => x.Access.Property());

VerifyModel(x => x.Access.ShouldEqual("property"));
}

[Test]
public void ShouldSetInsertProperty()
{
Convention(x => x.Insert());

VerifyModel(x => x.Insert.ShouldBeTrue());
}

[Test]
public void ShouldSetUpdateProperty()
{
Convention(x => x.Update());

VerifyModel(x => x.Update.ShouldBeTrue());
}

[Test]
public void ShouldSetUniqueProperty()
{
Convention(x => x.Unique());

VerifyModel(x => x.Unique.ShouldBeTrue());
}

[Test]
public void ShouldSetOptimisticLockProperty()
{
Convention(x => x.OptimisticLock());

VerifyModel(x => x.OptimisticLock.ShouldBeTrue());
}

#region Helpers

private void Convention(Action<IDynamicComponentInstance> convention)
{
model.Conventions.Add(new DynamicComponentConventionBuilder().Always(convention));
}

private void VerifyModel(Action<ComponentMapping> modelVerification)
{
var classMap = new ClassMap<PropertyTarget>();
classMap.Id(x => x.Id);
var map = classMap.DynamicComponent(x => x.GenericExtensionData, m =>
{
m.Map(x => (string)x["Name"]);
m.Map(x => (int)x["Age"]);
m.Map(x => (string)x["Profession"]);
});

model.Add(classMap);

var generatedModels = model.BuildMappings();
var modelInstance = (ComponentMapping)generatedModels
.First(x => x.Classes.FirstOrDefault(c => c.Type == typeof(PropertyTarget)) != null)
.Classes.First()
.Components.Where(x => x is ComponentMapping).First();

modelVerification(modelInstance);
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentNHibernate.Conventions.Helpers.Builders;
using FluentNHibernate.Conventions.Instances;
using FluentNHibernate.Mapping;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.Testing.DomainModel.Mapping;
using NUnit.Framework;

namespace FluentNHibernate.Testing.ConventionsTests.OverridingFluentInterface
{
[TestFixture]
public class GenericDynamicComponentConventionTests
{
private PersistenceModel model;
private IMappingProvider mapping;
private Type mappingType;

[SetUp]
public void CreatePersistenceModel()
{
model = new PersistenceModel();
}

[Test]
public void AccessShouldntBeOverwritten()
{
Mapping(x => x.Access.Field());

Convention(x => x.Access.Property());

VerifyModel(x => x.Access.ShouldEqual("field"));
}

[Test]
public void InsertShouldntBeOverwritten()
{
Mapping(x => x.Insert());

Convention(x => x.Not.Insert());

VerifyModel(x => x.Insert.ShouldBeTrue());
}

[Test]
public void UpdateShouldntBeOverwritten()
{
Mapping(x => x.Update());

Convention(x => x.Not.Update());

VerifyModel(x => x.Update.ShouldBeTrue());
}

[Test]
public void UniqueShouldntBeOverwritten()
{
Mapping(x => x.Unique());

Convention(x => x.Not.Unique());

VerifyModel(x => x.Unique.ShouldBeTrue());
}

[Test]
public void OptimisticLockShouldntBeOverwritten()
{
Mapping(x => x.OptimisticLock());

Convention(x => x.Not.OptimisticLock());

VerifyModel(x => x.OptimisticLock.ShouldBeTrue());
}

#region Helpers

private void Convention(Action<IDynamicComponentInstance> convention)
{
model.Conventions.Add(new DynamicComponentConventionBuilder().Always(convention));
}

private void Mapping(Action<DynamicComponentPart<IDictionary<string, object>>> mappingDefinition)
{
var classMap = new ClassMap<PropertyTarget>();
classMap.Id(x => x.Id);
var map = classMap.DynamicComponent(x => x.GenericExtensionData, m =>
{
m.Map(x => (string)x["Name"]);
m.Map(x => (int)x["Age"]);
m.Map(x => (string)x["Profession"]);
});

mappingDefinition(map);

mapping = classMap;
mappingType = typeof(PropertyTarget);
}

private void VerifyModel(Action<ComponentMapping> modelVerification)
{
model.Add(mapping);

var generatedModels = model.BuildMappings();
var modelInstance = (ComponentMapping)generatedModels
.First(x => x.Classes.FirstOrDefault(c => c.Type == mappingType) != null)
.Classes.First()
.Components.First();

modelVerification(modelInstance);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ public void ShouldApplyIDynamicComponentConvention()
.Access.ShouldEqual("field");
}

[Test]
public void ShouldApplyGenericIDynamicComponentConvention()
{
TestConvention(new DynamicComponentConvention(), () =>
{
var map = new ClassMap<Target>();

map.Id(x => x.Id);
map.DynamicComponent(x => x.GenericDynamicComponent, c => { });

return map;
})
.Components.First()
.Access.ShouldEqual("field");
}

[Test]
public void ShouldApplyIHasManyConvention()
{
Expand Down Expand Up @@ -738,6 +754,7 @@ private class Target
public string Property { get; set; }
public OtherObject Component { get; set; }
public IDictionary DynamicComponent { get; set; }
public IDictionary<string, object> GenericDynamicComponent { get; set; }
public OtherObject Other { get; set; }
public int Id { get; set; }
public IDictionary<string, OtherObject> DictionaryBag { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ParentModelMapping()
.IdentityType(typeof(int));

DynamicComponent(x => x.Dynamic, x => { });
DynamicComponent(x => x.GenericDynamic, x => { });

HasMany(x => x.MapOne).AsMap("type");
HasMany(x => x.SetOne).AsSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ParentModel
public virtual OneToOneModel One { get; private set; }
public virtual object Any { get; private set; }
public virtual IDictionary Dynamic { get; private set; }

public virtual IDictionary<string, object> GenericDynamic { get; private set; }
public virtual IDictionary<char, ManyToOneModel> MapOne { get; private set; }
public virtual ISet<ManyToOneModel> SetOne { get; private set; }
public virtual IList<ManyToOneModel> ListOne { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ public class MappedObject

public IList<ChildObject> Children { get; set; }
public IDictionary Dictionary { get; set; }

public IDictionary<string, object> GenericDictionary { get; set; }
public long Id { get; set; }

public int Version { get; set; }
Expand Down
Loading