Advertisement
pushrbx

Spring.NET - Merge two application contexts.

May 29th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. public static void MergeWith(this AbstractApplicationContext targetContext,
  2.     AbstractApplicationContext sourceContext)
  3. {
  4.     Check.NotNull(sourceContext, "sourceContext");
  5.  
  6.     targetContext.Stop();
  7.     foreach (var definitionName in sourceContext.GetObjectDefinitionNames())
  8.     {
  9.         var definition = sourceContext.GetObjectDefinition(definitionName);
  10.         targetContext.RegisterObjectDefinition(definitionName, definition);
  11.  
  12.         var obj = targetContext.CreateObject(definitionName, definition.ObjectType,
  13.             (from holder in
  14.                 (from DictionaryEntry val in definition.ConstructorArgumentValues.IndexedArgumentValues
  15.                     select val.Value).OfType<ConstructorArgumentValues.ValueHolder>()
  16.                 let typedStr = new TypedStringValue((string) holder.Value, holder.Type)
  17.                 let type = typedStr.ResolveTargetType()
  18.                 let typeConverter = TypeConverterRegistry.GetConverter(type)
  19.                 select typeConverter.ConvertFrom(holder.Value)).ToArray());
  20.  
  21.         targetContext.ConfigureObject(obj, definitionName);
  22.         targetContext.GetObject(definitionName);
  23.     }
  24.     targetContext.Start();
  25. }
  26. // after a merge it's recommended to dispose the old/source context
  27.  
  28. public static void MergeWith(this IApplicationContext targetContext, IApplicationContext sourceContext)
  29. {
  30.     Check.NotNull(sourceContext, "sourceContext");
  31.  
  32.     var absSourceContext = sourceContext as AbstractApplicationContext;
  33.     var absTargetContext = targetContext as AbstractApplicationContext;
  34.  
  35.     if (absSourceContext == null || targetContext == null)
  36.         return;
  37.  
  38.     absTargetContext.MergeWith(absSourceContext);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement