Skip to content

Commit a99cc47

Browse files
author
maximv
committed
fixed: #314
1 parent 23faa0b commit a99cc47

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/DryIoc/Container.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4147,7 +4147,6 @@ public static T New<T>(this IContainer container, Made.TypedMade<T> made,
41474147
/// <param name="container">Container</param> <param name="serviceType">New service type.</param>
41484148
/// <param name="registeredServiceType">Existing registered service type.</param>
41494149
/// <param name="serviceKey">(optional)</param> <param name="registeredServiceKey">(optional)</param>
4150-
/// <remarks>Does nothing if registration is already exists.</remarks>
41514150
public static void RegisterMapping(this IContainer container, Type serviceType, Type registeredServiceType,
41524151
object serviceKey = null, object registeredServiceKey = null) =>
41534152
Registrator.RegisterMapping(container,
@@ -4160,7 +4159,6 @@ public static void RegisterMapping(this IContainer container, Type serviceType,
41604159
/// <typeparam name="TService">New service type.</typeparam>
41614160
/// <typeparam name="TRegisteredService">Existing registered service type.</typeparam>
41624161
/// <param name="serviceKey">(optional)</param> <param name="registeredServiceKey">(optional)</param>
4163-
/// <remarks>Does nothing if registration is already exists.</remarks>
41644162
public static void RegisterMapping<TService, TRegisteredService>(this IContainer container,
41654163
object serviceKey = null, object registeredServiceKey = null) =>
41664164
Registrator.RegisterMapping(container,
@@ -7584,11 +7582,11 @@ public static void Unregister<TService>(this IRegistrator registrator,
75847582
/// Throw if no such registered service type in container.</summary>
75857583
/// <param name="registrator">Registrator</param> <param name="serviceType">New service type.</param>
75867584
/// <param name="registeredServiceType">Existing registered service type.</param>
7585+
/// <param name="ifAlreadyRegistered">The registration to overwrite or preserve the already registered service</param>
75877586
/// <param name="serviceKey">(optional)</param> <param name="registeredServiceKey">(optional)</param>
75887587
/// <param name="factoryType">(optional) By default is <see cref="FactoryType.Service"/></param>
7589-
/// <remarks>Does nothing if registration is already exists.</remarks>
75907588
public static void RegisterMapping(this IRegistrator registrator, Type serviceType, Type registeredServiceType,
7591-
object serviceKey = null, object registeredServiceKey = null, FactoryType factoryType = FactoryType.Service)
7589+
IfAlreadyRegistered? ifAlreadyRegistered, object serviceKey = null, object registeredServiceKey = null, FactoryType factoryType = FactoryType.Service)
75927590
{
75937591
var factories = registrator.GetRegisteredFactories(registeredServiceType, registeredServiceKey, factoryType);
75947592

@@ -7598,20 +7596,37 @@ public static void RegisterMapping(this IRegistrator registrator, Type serviceTy
75987596
if (factories.Length > 1)
75997597
Throw.It(Error.RegisterMappingUnableToSelectFromMultipleFactories, serviceType, serviceKey, factories);
76007598

7601-
registrator.Register(factories[0], serviceType, serviceKey, IfAlreadyRegistered.Keep, false);
7599+
registrator.Register(factories[0], serviceType, serviceKey, ifAlreadyRegistered, false);
76027600
}
76037601

7602+
/// <summary>Registers new service type with factory for registered service type.
7603+
/// Throw if no such registered service type in container.</summary>
7604+
public static void RegisterMapping(this IRegistrator registrator, Type serviceType, Type registeredServiceType,
7605+
object serviceKey = null, object registeredServiceKey = null, FactoryType factoryType = FactoryType.Service) =>
7606+
registrator.RegisterMapping(serviceType, registeredServiceType, null, serviceKey, registeredServiceKey, factoryType);
7607+
76047608
/// <summary>Registers new service type with factory for registered service type.
76057609
/// Throw if no such registered service type in container.</summary>
76067610
/// <param name="registrator">Registrator</param>
76077611
/// <typeparam name="TService">New service type.</typeparam>
76087612
/// <typeparam name="TRegisteredService">Existing registered service type.</typeparam>
76097613
/// <param name="serviceKey">(optional)</param> <param name="registeredServiceKey">(optional)</param>
76107614
/// <param name="factoryType">(optional) By default is <see cref="FactoryType.Service"/></param>
7611-
/// <remarks>Does nothing if registration is already exists.</remarks>
76127615
public static void RegisterMapping<TService, TRegisteredService>(this IRegistrator registrator,
76137616
object serviceKey = null, object registeredServiceKey = null, FactoryType factoryType = FactoryType.Service) =>
7614-
registrator.RegisterMapping(typeof(TService), typeof(TRegisteredService), serviceKey, registeredServiceKey);
7617+
registrator.RegisterMapping(typeof(TService), typeof(TRegisteredService), null, serviceKey, registeredServiceKey);
7618+
7619+
/// <summary>Registers new service type with factory for registered service type.
7620+
/// Throw if no such registered service type in container.</summary>
7621+
/// <param name="container">Container</param>
7622+
/// <typeparam name="TService">New service type.</typeparam>
7623+
/// <typeparam name="TRegisteredService">Existing registered service type.</typeparam>
7624+
/// <param name="ifAlreadyRegistered">The registration to overwrite or preserve the already registered service</param>
7625+
/// <param name="serviceKey">(optional)</param> <param name="registeredServiceKey">(optional)</param>
7626+
public static void RegisterMapping<TService, TRegisteredService>(this IContainer container,
7627+
IfAlreadyRegistered ifAlreadyRegistered, object serviceKey = null, object registeredServiceKey = null) =>
7628+
Registrator.RegisterMapping(container,
7629+
typeof(TService), typeof(TRegisteredService), ifAlreadyRegistered, serviceKey, registeredServiceKey);
76157630

76167631
/// <summary>Register a service without implementation which can be provided later in terms
76177632
/// of normal registration with IfAlreadyRegistered.Replace parameter.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
4+
namespace DryIoc.IssuesTests
5+
{
6+
[TestFixture]
7+
public class GHIssue314_Expose_the_usual_IfAlreadyRegistered_option_parameter_for_RegisterMapping
8+
{
9+
[Test]
10+
public void Test()
11+
{
12+
var container = new Container();
13+
14+
container.Register<LeafOne>(Reuse.Singleton);
15+
container.Register<LeafTwo>(Reuse.Singleton);
16+
17+
container.RegisterMapping<Base, LeafOne>();
18+
container.RegisterMapping<Base, LeafTwo>();
19+
20+
var bases = container.ResolveMany<Base>().ToArray();
21+
Assert.AreEqual(2, bases.Length);
22+
}
23+
24+
class Base { };
25+
class LeafOne : Base { };
26+
class LeafTwo : Base { };
27+
}
28+
}

0 commit comments

Comments
 (0)