Skip to content

Commit 8a89e94

Browse files
author
Travis Illig
committed
Container.Update marked obsolete. Containers should be considered immutable.
1 parent bbea0e0 commit 8a89e94

File tree

7 files changed

+32
-4
lines changed

7 files changed

+32
-4
lines changed

src/Autofac/ContainerBuilder.cs

+18
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ private static void StartStartableComponents(IComponentContext componentContext)
172172
/// </remarks>
173173
/// <param name="container">An existing container to make the registrations in.</param>
174174
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "You can't update any arbitrary context, only containers.")]
175+
[Obsolete("Containers should generally be considered immutable. Register all of your dependencies before building/resolving. If you need to change the contents of a container, you technically should rebuild the container. This method may be removed in a future major release.")]
175176
public void Update(IContainer container)
176177
{
177178
Update(container, ContainerBuildOptions.None);
@@ -188,6 +189,7 @@ public void Update(IContainer container)
188189
/// <param name="container">An existing container to make the registrations in.</param>
189190
/// <param name="options">Options that influence the way the container is updated.</param>
190191
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "You can't update any arbitrary context, only containers.")]
192+
[Obsolete("Containers should generally be considered immutable. Register all of your dependencies before building/resolving. If you need to change the contents of a container, you technically should rebuild the container. This method may be removed in a future major release.")]
191193
public void Update(IContainer container, ContainerBuildOptions options)
192194
{
193195
// Issue #462: The ContainerBuildOptions parameter is added here as an overload
@@ -208,7 +210,23 @@ public void Update(IContainer container, ContainerBuildOptions options)
208210
/// - this prevents ownership issues for provided instances.
209211
/// </remarks>
210212
/// <param name="componentRegistry">An existing registry to make the registrations in.</param>
213+
[Obsolete("Containers should generally be considered immutable. Register all of your dependencies before building/resolving. If you need to change the contents of a container, you technically should rebuild the container. This method may be removed in a future major release.")]
211214
public void Update(IComponentRegistry componentRegistry)
215+
{
216+
this.UpdateRegistry(componentRegistry);
217+
}
218+
219+
/// <summary>
220+
/// Configure an existing registry with the component registrations
221+
/// that have been made. Primarily useful in dynamically adding registrations
222+
/// to a child lifetime scope.
223+
/// </summary>
224+
/// <remarks>
225+
/// Update can only be called once per <see cref="ContainerBuilder"/>
226+
/// - this prevents ownership issues for provided instances.
227+
/// </remarks>
228+
/// <param name="componentRegistry">An existing registry to make the registrations in.</param>
229+
internal void UpdateRegistry(IComponentRegistry componentRegistry)
212230
{
213231
if (componentRegistry == null) throw new ArgumentNullException(nameof(componentRegistry));
214232
Build(componentRegistry, true);

src/Autofac/Core/Lifetime/LifetimeScope.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private ScopeRestrictedRegistry CreateScopeRestrictedRegistry(object tag, Action
217217
configurationAction(builder);
218218

219219
var locals = new ScopeRestrictedRegistry(tag, builder.Properties);
220-
builder.Update(locals);
220+
builder.UpdateRegistry(locals);
221221
return locals;
222222
}
223223

src/Autofac/Module.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void Configure(IComponentRegistry componentRegistry)
7979
var moduleBuilder = new ContainerBuilder(componentRegistry.Properties);
8080

8181
Load(moduleBuilder);
82-
moduleBuilder.Update(componentRegistry);
82+
moduleBuilder.UpdateRegistry(componentRegistry);
8383
AttachToRegistrations(componentRegistry);
8484
AttachToSources(componentRegistry);
8585
}

test/Autofac.Test/ContainerBuilderTests.cs

+6
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ public void WhenUpdating_DefaultModulesAreExcluded()
361361
{
362362
var builder = new ContainerBuilder();
363363
var container = new Container();
364+
#pragma warning disable CS0618
364365
builder.Update(container);
366+
#pragma warning restore CS0618
365367
Assert.False(container.IsRegistered<IEnumerable<object>>());
366368
}
367369

@@ -445,7 +447,9 @@ public void WhenTheContainerIsUpdated_ExistingStartableComponentsAreNotReStarted
445447

446448
var builder2 = new ContainerBuilder();
447449
builder2.RegisterInstance(startable2).As<IStartable>();
450+
#pragma warning disable CS0618
448451
builder2.Update(container);
452+
#pragma warning restore CS0618
449453

450454
Assert.Equal(1, startable1.StartCount);
451455
Assert.Equal(1, startable2.StartCount);
@@ -461,7 +465,9 @@ public void WhenTheContainerIsUpdated_NewStartableComponentsAreStarted()
461465

462466
var builder = new ContainerBuilder();
463467
builder.RegisterInstance(startable).As<IStartable>();
468+
#pragma warning disable CS0618
464469
builder.Update(container);
470+
#pragma warning restore CS0618
465471

466472
Assert.Equal(1, startable.StartCount);
467473
}

test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ internal void UpdateRegistry(object instance)
418418
{
419419
var builder = new ContainerBuilder();
420420
builder.RegisterInstance(instance);
421-
builder.Update(_registerContext.ComponentRegistry);
421+
builder.UpdateRegistry(_registerContext.ComponentRegistry);
422422
}
423423
}
424424

test/Autofac.Test/Core/Registration/CopyOnWriteRegistryTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void RegistrationsMadeByUpdatingAChildScopeDoNotAppearInTheParentScope()
4444
var childScope = container.BeginLifetimeScope();
4545
var updater = new ContainerBuilder();
4646
updater.RegisterType<object>();
47-
updater.Update(childScope.ComponentRegistry);
47+
updater.UpdateRegistry(childScope.ComponentRegistry);
4848
Assert.True(childScope.IsRegistered<object>());
4949
Assert.False(container.IsRegistered<object>());
5050
}

test/Autofac.Test/RegistrationExtensionsTests.cs

+4
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ public void AutoActivate_ContainerUpdateAutoActivatesNewComponents()
228228
var container = new ContainerBuilder().Build();
229229
var builder = new ContainerBuilder();
230230
builder.RegisterType<MyComponent2>().AutoActivate().OnActivated(e => instanceCount++);
231+
#pragma warning disable CS0618
231232
builder.Update(container);
233+
#pragma warning restore CS0618
232234
Assert.Equal(1, instanceCount);
233235
}
234236

@@ -245,7 +247,9 @@ public void AutoActivate_ContainerUpdateDoesNotAutoActivateExistingComponents()
245247
int secondCount = 0;
246248
var builder2 = new ContainerBuilder();
247249
builder2.RegisterType<MyComponent>().AutoActivate().OnActivated(e => secondCount++);
250+
#pragma warning disable CS0618
248251
builder2.Update(container);
252+
#pragma warning restore CS0618
249253
Assert.Equal(1, firstCount);
250254
Assert.Equal(1, secondCount);
251255
}

0 commit comments

Comments
 (0)