Skip to content

Commit d888baf

Browse files
committed
succesfully get individual converters from container
1 parent ab0242c commit d888baf

File tree

6 files changed

+24
-13
lines changed

6 files changed

+24
-13
lines changed

RomanticWeb/ComponentModel/InternalComponentsCompositionRoot.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private void RegisterConverter(Type converterType, IServiceRegistry registry)
8686
return;
8787
}
8888

89-
registry.Register(typeof(INodeConverter), converterType, converterType.FullName, new PerContainerLifetime());
89+
registry.Register(converterType, converterType, converterType.FullName, new PerContainerLifetime());
9090
}
9191

9292
private bool IsAlreadyRegistered(Type converterType, IServiceRegistry registry)

RomanticWeb/ComponentModel/MappingCompositionRoot.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public MappingCompositionRoot()
1414
MappingModelVisitor<RdfTypeCacheBuilder>();
1515
MappingProviderVisitor<ConventionsVisitor>();
1616
MappingProviderVisitor<MappingProvidersValidator>();
17-
MappingProviderVisitor<ConvertersRegistrator>();
17+
////MappingProviderVisitor<ConvertersRegistrator>();
1818
MappingProviderVisitor<GeneratedListMappingSource>();
1919
MappingProviderVisitor<GeneratedDictionaryMappingSource>();
2020
}

RomanticWeb/Converters/ConverterCatalog.cs

+10-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ namespace RomanticWeb.Converters
1010
/// </summary>
1111
internal sealed class ConverterCatalog : IConverterCatalog
1212
{
13+
private readonly Func<Type, INodeConverter> _createConverter;
14+
1315
private readonly IDictionary<Type, INodeConverter> _converters;
1416

1517
/// <summary>
1618
/// Initializes a new instance of the <see cref="ConverterCatalog"/> class.
1719
/// </summary>
18-
public ConverterCatalog(IEnumerable<INodeConverter> converters)
20+
public ConverterCatalog(Func<Type, INodeConverter> createConverter)
21+
{
22+
_createConverter = createConverter;
23+
}
24+
25+
internal ConverterCatalog()
26+
: this(type => null)
1927
{
20-
_converters = converters.ToDictionary(c => c.GetType(), c => c);
2128
}
2229

2330
/// <inheritdoc/>
@@ -45,13 +52,8 @@ public INodeConverter GetConverter(Type converterType)
4552
{
4653
return new FallbackNodeConverter(this);
4754
}
48-
49-
if (!_converters.ContainsKey(converterType))
50-
{
51-
_converters[converterType] = (INodeConverter)Activator.CreateInstance(converterType);
52-
}
5355

54-
return _converters[converterType];
56+
return _createConverter(converterType);
5557
}
5658
}
5759
}

RomanticWeb/EntityContextFactory.cs

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ internal EntityContextFactory(IServiceContainer container)
3939
_container = container;
4040
_container.RegisterAssembly("RomanticWeb.dll");
4141
_container.Register<IEntityContextFactory>(f => this);
42+
_container.RegisterInstance<Func<Type, INodeConverter>>(type =>
43+
{
44+
if (!_container.CanGetInstance(type, string.Empty))
45+
{
46+
_container.Register(type);
47+
}
48+
49+
return (INodeConverter)_container.GetInstance(type);
50+
});
4251

4352
WithMappings(DefaultMappings);
4453

Tests/RomanticWeb.Tests/Mapping/MappingRepositoryTestBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void Setup()
3939
container.RegisterFrom<InternalComponentsCompositionRoot>();
4040
container.RegisterInstance(_ontologies.Object);
4141
var conventions = container.GetInstance<IEnumerable<IConvention>>();
42-
var mappingModelBuilder = new MappingModelBuilder(new MappingContext(_ontologies.Object, conventions), new ConverterCatalog(new INodeConverter[0]));
42+
var mappingModelBuilder = new MappingModelBuilder(new MappingContext(_ontologies.Object, conventions), new ConverterCatalog());
4343
container.RegisterInstance(mappingModelBuilder);
4444
container.Register(f => CreateMappingSources());
4545

Tests/RomanticWeb.Tests/OntologyAccessorTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void Getting_known_predicate_should_return_objects()
4949
{
5050
// given
5151
_store.Setup(g => g.GetObjectsForPredicate(_entity.Id, It.IsAny<Uri>(), It.IsAny<Uri>())).Returns(new Node[0]);
52-
dynamic accessor = new OntologyAccessor(_entity, _ontology, new FallbackNodeConverter(new ConverterCatalog(new INodeConverter[0])), new TestTransformerCatalog());
52+
dynamic accessor = new OntologyAccessor(_entity, _ontology, new FallbackNodeConverter(new ConverterCatalog()), new TestTransformerCatalog());
5353

5454
// when
5555
var givenName = accessor.givenName;
@@ -63,7 +63,7 @@ public void Getting_unknown_predicate_should_use_the_property_name()
6363
{
6464
// given
6565
_store.Setup(g => g.GetObjectsForPredicate(_entity.Id, It.IsAny<Uri>(), It.IsAny<Uri>())).Returns(new Node[0]);
66-
dynamic accessor = new OntologyAccessor(_entity, _ontology, new FallbackNodeConverter(new ConverterCatalog(new INodeConverter[0])), new TestTransformerCatalog());
66+
dynamic accessor = new OntologyAccessor(_entity, _ontology, new FallbackNodeConverter(new ConverterCatalog()), new TestTransformerCatalog());
6767

6868
// when
6969
var givenName = accessor.fullName;

0 commit comments

Comments
 (0)