<commit_before>﻿using System;
using System.Linq;

using Ninject;
using Ninject.Extensions.Factory;
using Ninject.Modules;

using WebFormsMvp;
using WebFormsMvp.Binder;

using WhenItsDone.WebFormsClient.App_Start.Factories;

namespace WhenItsDone.WebFormsClient.App_Start.NinjectBindingsModules
{
    public class MVPBindingsModule : NinjectModule
    {
        public override void Load()
        {
            this.Bind<IPresenterFactory>().To<CustomWebFormsMvpPresenterFactory>().InSingletonScope();
            this.Bind<ICustomPresenterFactory>().ToFactory().InSingletonScope();

            this.Bind<IPresenter>().ToMethod(ctx =>
            {
                var parameters = ctx.Parameters.ToList();

                var requestedType = (Type)parameters[0];
                if (requestedType == null)
                {
                    throw new ArgumentNullException("Invalid requested presenter type.");
                }

                var viewType = (Type)parameters[1];
                if (viewType == null)
                {
                    throw new ArgumentNullException("Invalid requested view type.");
                }

                var viewInstance = (IView)parameters[2];
                if (viewInstance == null)
                {
                    viewInstance = (IView)ctx.Kernel.Get(viewType);
                }

                // Unknown possible parameters for each separate IPresenter
                // Binding the view so Ninject can resolve each of them separately.
                var bindingExists = this.Kernel.GetBindings(viewType).Any();
                if (bindingExists)
                {
                    this.Rebind(viewType).ToMethod(context => viewInstance);
                }
                else
                {
                    this.Bind(viewType).ToMethod(context => viewInstance);
                }

                return (IPresenter)ctx.Kernel.Get(requestedType);
            })
            .NamedLikeFactoryMethod((ICustomPresenterFactory factory) => factory.GetPresenter(null, null, null));
        }
    }
}<commit_msg>refactoring ninject module<commit_after>﻿using System;
using System.Linq;

using Ninject;
using Ninject.Extensions.Factory;
using Ninject.Modules;

using WebFormsMvp;
using WebFormsMvp.Binder;

using WhenItsDone.WebFormsClient.App_Start.Factories;
using Ninject.Activation;

namespace WhenItsDone.WebFormsClient.App_Start.NinjectBindingsModules
{
    public class MVPBindingsModule : NinjectModule
    {
        public override void Load()
        {
            this.Bind<IPresenterFactory>().To<CustomWebFormsMvpPresenterFactory>().InSingletonScope();
            this.Bind<ICustomPresenterFactory>().ToFactory().InSingletonScope();

            this.Bind<IPresenter>().ToMethod(this.PresenterFactoryMethod)
                .NamedLikeFactoryMethod((ICustomPresenterFactory factory) => factory.GetPresenter(null, null, null));
        }

        private IPresenter PresenterFactoryMethod(IContext ctx)
        {
            var parameters = ctx.Parameters.ToList();

            var requestedType = (Type)parameters[0];
            if (requestedType == null)
            {
                throw new ArgumentNullException("Invalid requested presenter type.");
            }

            var viewType = (Type)parameters[1];
            if (viewType == null)
            {
                throw new ArgumentNullException("Invalid requested view type.");
            }

            var viewInstance = (IView)parameters[2];
            if (viewInstance == null)
            {
                viewInstance = (IView)ctx.Kernel.Get(viewType);
            }

            // Unknown possible parameters for each separate IPresenter
            // Binding the view so Ninject can resolve each of them separately.
            var bindingExists = this.Kernel.GetBindings(viewType).Any();
            if (bindingExists)
            {
                this.Rebind(viewType).ToMethod(context => viewInstance);
            }
            else
            {
                this.Bind(viewType).ToMethod(context => viewInstance);
            }

            return (IPresenter)ctx.Kernel.Get(requestedType);
        }
    }
}
{'fraction_non_alphanumeric': 0.09125565880390756, 'fraction_numerical': 0.0014295925661186562, 'mean_word_length': 1.9542575650950036, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 18, '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': '2775540', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1030, 'n_words': 206}