Skip to content

Commit

Permalink
Implement dispose pattern, rename fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Jul 17, 2023
1 parent 37548fd commit c9a1bc3
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions src/Agent/NewRelic/Agent/Core/DependencyInjection/CoreContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#if NETSTANDARD2_0
using System;
using System.Collections.Generic;
using NewRelic.Agent.Core.Logging;

using System.Linq;
using System.Text;
using Autofac;
using NewRelic.Core.Logging;

Expand All @@ -16,28 +12,29 @@ namespace NewRelic.Agent.Core.DependencyInjection
public class CoreContainer : IContainer
{

private readonly ContainerBuilder builder;
private Autofac.IContainer container;
private readonly ContainerBuilder _builder;
private Autofac.IContainer _container;

// use the scope instead of the container to resolve instances. This allows us to replace registrations in a new scope for unit testing
private ILifetimeScope scope;
private Dictionary<Type, object> _registrationsToReplace = new Dictionary<Type, object>();
private ILifetimeScope _scope;
private bool _disposedValue;
private readonly Dictionary<Type, object> _registrationsToReplace = new Dictionary<Type, object>();

public CoreContainer()
{
this.builder = new ContainerBuilder();
_builder = new ContainerBuilder();
}

public void Build()
{
this.container = builder.Build();
scope = this.container.BeginLifetimeScope();
_container = _builder.Build();
_scope = _container.BeginLifetimeScope();
}

public void ReplaceRegistrations()
{
// create a new nested scope, registering the requested replacement instances
scope = scope.BeginLifetimeScope(ReplaceRegistrations);
// create a new nested scope, registering the requested replacement instances.
_scope = _scope.BeginLifetimeScope(ReplaceRegistrations);

_registrationsToReplace.Clear();
}
Expand All @@ -50,37 +47,32 @@ private void ReplaceRegistrations(ContainerBuilder builder)
}
}

public void Dispose()
{
scope?.Dispose();
container?.Dispose();
}

public void Register<TInterface, TConcrete>()
where TInterface : class
where TConcrete : class, TInterface
{
builder.RegisterType<TConcrete>().As<TInterface>().InstancePerLifetimeScope();
_builder.RegisterType<TConcrete>().As<TInterface>().InstancePerLifetimeScope();
}

public void Register<TInterface1, TInterface2, TConcrete>()
where TInterface1 : class
where TInterface2 : class
where TConcrete : class, TInterface1, TInterface2
{
builder.RegisterType<TConcrete>().As<TInterface1, TInterface2>().InstancePerLifetimeScope();
_builder.RegisterType<TConcrete>().As<TInterface1, TInterface2>().InstancePerLifetimeScope();
}

public void RegisterInstance<TInterface>(TInterface instance)
where TInterface : class
{
builder.RegisterInstance<TInterface>(instance).As<TInterface>().SingleInstance();
_builder.RegisterInstance<TInterface>(instance).As<TInterface>().SingleInstance();
}

public void RegisterFactory<TInterface>(Func<TInterface> func)
where TInterface : class
{
builder.Register(c => func.Invoke()).As<TInterface>();
_builder.Register(c => func.Invoke()).As<TInterface>();
}

public void ReplaceInstanceRegistration<TInterface>(TInterface instance)
Expand All @@ -93,29 +85,53 @@ public void ReplaceInstanceRegistration<TInterface>(TInterface instance)
public T Resolve<T>()
{
Check(typeof(T));
return scope.Resolve<T>();
return _scope.Resolve<T>();
}

public IEnumerable<T> ResolveAll<T>()
{
Check(typeof(T));
try
{
return scope.Resolve<IEnumerable<T>>();
return _scope.Resolve<IEnumerable<T>>();
}
catch (Exception ex)
{
Log.Error($"Error during ResolveAll of {typeof(T)}");
throw ex;
Log.Error($"Error during ResolveAll of {typeof(T)}: {ex}");
throw;
}
}
private void Check(Type type)
{
if (scope == null)
if (_scope == null)
{
throw new Exception("Resolve invoked with uninitialized container for " + type);
}
}

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_scope?.Dispose();
_container?.Dispose();

_scope = null;
_container = null;
}

_disposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
#endif

0 comments on commit c9a1bc3

Please sign in to comment.