<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using Akka.Actor;
using Akka.DI.Core;
using SimpleInjector;
using SimpleInjector.Extensions.ExecutionContextScoping;
using SI = SimpleInjector;

namespace Akka.DI.SimpleInjector
{
    public class SimpleInjectorDependencyResolver : IDependencyResolver
    {
        private readonly Container _container;
        private readonly ActorSystem _actorSystem;
        private readonly ConcurrentDictionary<string, Type> _typeCache;
        private readonly ConditionalWeakTable<ActorBase, SI.Scope> _references;
 
        public SimpleInjectorDependencyResolver(Container container, ActorSystem actorSystem)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            if (actorSystem == null)
            {
                throw new ArgumentNullException("actorSystem");
            }

            _container = container;

            _actorSystem = actorSystem;

            _actorSystem.AddDependencyResolver(this);

            _typeCache = new ConcurrentDictionary<string, Type>(StringComparer.InvariantCultureIgnoreCase);

            _references = new ConditionalWeakTable<ActorBase, SI.Scope>();
        }
 
        public Type GetType(string actorName)
        {
            return _typeCache.GetOrAdd(actorName, actorName.GetTypeValue());
        }
 
        public Func<ActorBase> CreateActorFactory(Type actorType)
        {
            return () =>
            {
                var container = _container;

                var references = _references;

                var scope = container.BeginExecutionContextScope();

                var actor = (ActorBase)container.GetInstance(actorType);

                references.Add(actor, scope);
 
                return actor;
            };
        }
 
        public Props Create<TActor>() where TActor : ActorBase
        {
            return Create(typeof(TActor));
        }
 
        public Props Create(Type actorType)
        {
            return _actorSystem.GetExtension<DIExt>().Props(actorType);
        }
 
        public void Release(ActorBase actor)
        {
            SI.Scope scope;

            if (_references.TryGetValue(actor, out scope))
            {
                scope.Dispose();

                _references.Remove(actor);
            }
        }
    }
}<commit_msg>Small refactor<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using Akka.Actor;
using Akka.DI.Core;
using SimpleInjector;
using SimpleInjector.Extensions.ExecutionContextScoping;
using SI = SimpleInjector;

namespace Akka.DI.SimpleInjector
{
    public class SimpleInjectorDependencyResolver : IDependencyResolver
    {
        private readonly Container _container;
        private readonly ActorSystem _actorSystem;
        private readonly ConcurrentDictionary<string, Type> _typeCache;
        private readonly ConditionalWeakTable<ActorBase, SI.Scope> _references;
 
        public SimpleInjectorDependencyResolver(Container container, ActorSystem actorSystem)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            if (actorSystem == null)
            {
                throw new ArgumentNullException("actorSystem");
            }

            _container = container;

            _actorSystem = actorSystem;

            _actorSystem.AddDependencyResolver(this);

            _typeCache = new ConcurrentDictionary<string, Type>(StringComparer.InvariantCultureIgnoreCase);

            _references = new ConditionalWeakTable<ActorBase, SI.Scope>();
        }
 
        public Type GetType(string actorName)
        {
            return _typeCache.GetOrAdd(actorName, actorName.GetTypeValue());
        }
 
        public Func<ActorBase> CreateActorFactory(Type actorType)
        {
            return () =>
            {
                var scope = _container.BeginExecutionContextScope();

                var actor = (ActorBase)_container.GetInstance(actorType);

                _references.Add(actor, scope);
 
                return actor;
            };
        }
 
        public Props Create<TActor>() where TActor : ActorBase
        {
            return Create(typeof(TActor));
        }
 
        public Props Create(Type actorType)
        {
            return _actorSystem.GetExtension<DIExt>().Props(actorType);
        }
 
        public void Release(ActorBase actor)
        {
            SI.Scope scope;

            if (_references.TryGetValue(actor, out scope))
            {
                scope.Dispose();

                _references.Remove(actor);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07612312811980033, 'fraction_numerical': 0.0, 'mean_word_length': 2.0207286432160805, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1789005', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1140, 'n_words': 245}