<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject.Extensions.Conventions;
using Ninject.Infrastructure.Language;
using Ninject.Modules;

namespace Hadouken.Common.DI.Ninject.Modules
{
    public class ComponentModule : NinjectModule
    {
        public override void Load()
        {
            Kernel.Bind(
                x =>
                x.From(AppDomain.CurrentDomain.GetAssemblies())
                 .Select(t => t.HasAttribute<ComponentAttribute>())
                 .BindAllInterfaces());
        }
    }
}<commit_msg>Rewrote the Component module loading in Ninject to support singletons.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Ninject.Extensions.Conventions;
using Ninject.Infrastructure.Language;
using Ninject.Modules;

namespace Hadouken.Common.DI.Ninject.Modules
{
    public class ComponentModule : NinjectModule
    {
        public override void Load()
        {
            var componentTypes = (from asm in AppDomain.CurrentDomain.GetAssemblies()
                                  from type in asm.GetTypes()
                                  where type.HasAttribute<ComponentAttribute>()
                                  where type.IsClass && !type.IsAbstract
                                  let attr = type.GetAttribute<ComponentAttribute>()
                                  select new
                                      {
                                          Type = type,
                                          Attribute = attr
                                      });

            foreach (var componentType in componentTypes)
            {
                switch (componentType.Attribute.ComponentType)
                {
                    case ComponentType.Singleton:
                        Kernel.Bind(componentType.Type.GetInterfaces()).To(componentType.Type).InSingletonScope();
                        break;

                    case ComponentType.Transient:
                        Kernel.Bind(componentType.Type.GetInterfaces()).To(componentType.Type).InTransientScope();
                        break;
                }
            }
        }
    }

    public static class ReflectionExtensions
    {
        public static T GetAttribute<T>(this MemberInfo mi)
        {
            return (T)mi.GetCustomAttributes(typeof(T), true).FirstOrDefault();
        }
    }
}
{'fraction_non_alphanumeric': 0.07769726247987117, 'fraction_numerical': 0.0, 'mean_word_length': 1.5565843621399178, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6016001', 'n_tokens_mistral': 578, 'n_tokens_neox': 556, 'n_words': 117}