Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. foreach (var instance in instances)
  2. {
  3. builder.RegisterInstance(instance).Keyed<IInstance>(InstanceType.A);
  4. }
  5. IContainer container = builder.Build();
  6. DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  7.  
  8. //...dispose old instances, obtain new instances
  9.  
  10. var builder = new ContainerBuilder();
  11. foreach (var c in _componentContext.ComponentRegistry.Registrations)
  12. {
  13. builder.RegisterComponent(c);
  14. }
  15. foreach (var instance in newInstances)
  16. {
  17. builder.RegisterInstance(instance).Keyed<IInstance>(InstanceType.A);
  18. }
  19. builder.Update(_componentContext.ComponentRegistry);
  20.  
  21. class Program
  22. {
  23. static void Main(string[] args)
  24. {
  25. ContainerBuilder builder = new ContainerBuilder();
  26. builder.RegisterInstance(new FooProvider(new Foo("a")))
  27. .As<FooProvider>();
  28. builder.Register(c => c.Resolve<FooProvider>().Value)
  29. .ExternallyOwned()
  30. .Keyed<Foo>(1);
  31.  
  32. IContainer container = builder.Build();
  33. using (ILifetimeScope scope = container.BeginLifetimeScope())
  34. {
  35. Do(scope);
  36. }
  37.  
  38. using (ILifetimeScope scope = container.BeginLifetimeScope())
  39. {
  40. IComponentContext context = scope.Resolve<IComponentContext>();
  41. container.Resolve<FooProvider>().Value = new Foo("b");
  42. Do(scope);
  43. }
  44.  
  45. using (ILifetimeScope scope = container.BeginLifetimeScope())
  46. {
  47. Do(scope);
  48. }
  49. }
  50.  
  51. static void Do(ILifetimeScope scope)
  52. {
  53. IIndex<Int32, Foo> index = scope.Resolve<IIndex<Int32, Foo>>();
  54. Foo foo = index[1];
  55. Console.WriteLine(foo.Value);
  56. }
  57. }
  58.  
  59. public class FooProvider
  60. {
  61. public FooProvider(Foo value)
  62. {
  63. this._value = value;
  64. }
  65.  
  66. private Foo _value;
  67. public Foo Value
  68. {
  69. get
  70. {
  71. return this._value;
  72. }
  73. set
  74. {
  75. this._value = value;
  76. }
  77. }
  78. }
  79. public class Foo
  80. {
  81. public Foo(String value)
  82. {
  83. this._value = value;
  84. }
  85.  
  86. private readonly String _value;
  87. public String Value
  88. {
  89. get
  90. {
  91. return this._value;
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement