<commit_before>﻿namespace SimpleInjector.CodeSamples
{
    using System;
    using System.Linq.Expressions;

    /// <summary>
    /// Injects a new instance for each dependency. Behaves the same as Transient but prevents any lifestyle
    /// mismatches from being reported. Use with care!!!
    /// </summary>
    public class InstancePerDependencyLifestyle : Lifestyle
    {
        public InstancePerDependencyLifestyle() : base("Instance Per Dependency") { }

        // MaxValue prevents lifestyle mismatches
        protected override int Length => int.MaxValue;

        protected override Registration CreateRegistrationCore<TService, TImplementation>(Container c) => 
            new InstancePerDependencyRegistration<TService, TImplementation>(this, c);

        protected override Registration CreateRegistrationCore<TService>(Func<TService> ic, Container c) => 
            new InstancePerDependencyRegistration<TService>(this, c, ic);

        private sealed class InstancePerDependencyRegistration<TService> : Registration where TService : class
        {
            private readonly Func<TService> ic;

            public InstancePerDependencyRegistration(Lifestyle l, Container c, Func<TService> ic) : base(l, c)
            {
                this.ic = ic;
            }

            public override Type ImplementationType => typeof(TService);
            public override Expression BuildExpression() => this.BuildTransientExpression(this.ic);
        }

        private class InstancePerDependencyRegistration<TService, TImpl> : Registration
            where TImpl : class, TService where TService : class
        {
            public InstancePerDependencyRegistration(Lifestyle l, Container c) : base(l, c) { }
            public override Type ImplementationType => typeof(TImpl);
            public override Expression BuildExpression() => this.BuildTransientExpression<TService, TImpl>();
        }
    }
}<commit_msg>Improvement to InstancePerDependencyLifestyle.<commit_after>﻿namespace SimpleInjector.CodeSamples
{
    using System;
    using System.Linq.Expressions;

    /// <summary>
    /// Injects a new instance for each dependency. Behaves the same as Transient but prevents any lifestyle
    /// mismatches from being reported. Use with care!!!
    /// </summary>
    public class InstancePerDependencyLifestyle : Lifestyle
    {
        public InstancePerDependencyLifestyle() : base("Instance Per Dependency") { }

        // MaxValue prevents lifestyle mismatches when injected into a component, while allowing instances
        // to depend on singletons.
        protected override int Length => 1000;

        protected override Registration CreateRegistrationCore<TService, TImplementation>(Container c) => 
            new InstancePerDependencyRegistration<TService, TImplementation>(this, c);

        protected override Registration CreateRegistrationCore<TService>(Func<TService> ic, Container c) => 
            new InstancePerDependencyRegistration<TService>(this, c, ic);

        private sealed class InstancePerDependencyRegistration<TService> : Registration where TService : class
        {
            private readonly Func<TService> ic;

            public InstancePerDependencyRegistration(Lifestyle l, Container c, Func<TService> ic) : base(l, c)
            {
                this.ic = ic;
            }

            public override Type ImplementationType => typeof(TService);
            public override Expression BuildExpression() => this.BuildTransientExpression(this.ic);
        }

        private class InstancePerDependencyRegistration<TService, TImpl> : Registration
            where TImpl : class, TService where TService : class
        {
            public InstancePerDependencyRegistration(Lifestyle l, Container c) : base(l, c) { }
            public override Type ImplementationType => typeof(TImpl);
            public override Expression BuildExpression() => this.BuildTransientExpression<TService, TImpl>();
        }
    }
}
{'fraction_non_alphanumeric': 0.07746126936531735, 'fraction_numerical': 0.0009995002498750624, 'mean_word_length': 2.999000999000999, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 41, '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': '1693325', 'n_tokens_mistral': 951, 'n_tokens_neox': 909, 'n_words': 280}