pushrbx icon

Spring.NET - Merge two application contexts.

pushrbx | PRO | 05/29/15 03:15:42 PM UTC | 0 ⭐ | 579 👁️ | Never ⏰ | []
C# |

1.68 KB

|

None

|

0 👍

/

0 👎

public static void MergeWith(this AbstractApplicationContext targetContext,
    AbstractApplicationContext sourceContext)
{
    Check.NotNull(sourceContext, "sourceContext");
 
    targetContext.Stop();
    foreach (var definitionName in sourceContext.GetObjectDefinitionNames())
    {
        var definition = sourceContext.GetObjectDefinition(definitionName);
        targetContext.RegisterObjectDefinition(definitionName, definition);
 
        var obj = targetContext.CreateObject(definitionName, definition.ObjectType,
            (from holder in
                (from DictionaryEntry val in definition.ConstructorArgumentValues.IndexedArgumentValues
                    select val.Value).OfType<ConstructorArgumentValues.ValueHolder>()
                let typedStr = new TypedStringValue((string) holder.Value, holder.Type)
                let type = typedStr.ResolveTargetType()
                let typeConverter = TypeConverterRegistry.GetConverter(type)
                select typeConverter.ConvertFrom(holder.Value)).ToArray());
 
        targetContext.ConfigureObject(obj, definitionName);
        targetContext.GetObject(definitionName);
    }
    targetContext.Start();
}
// after a merge it's recommended to dispose the old/source context
 
public static void MergeWith(this IApplicationContext targetContext, IApplicationContext sourceContext)
{
    Check.NotNull(sourceContext, "sourceContext");
 
    var absSourceContext = sourceContext as AbstractApplicationContext;
    var absTargetContext = targetContext as AbstractApplicationContext;
 
    if (absSourceContext == null || targetContext == null)
        return;
 
    absTargetContext.MergeWith(absSourceContext);
}

Comments