Good day to all, help me figure it out, study mvc and came across an example like this:

[assembly: WebActivator.PreApplicationStartMethod(typeof(Test.App_Start.NinjectWebCommon), "Start")] [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(Test.App_Start.NinjectWebCommon), "Stop")] namespace Test.App_Start { using System; using System.Web; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Ninject.Web.Common; using System.Configuration; public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); /// <summary> /// Starts the application /// </summary> public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } /// <summary> /// Stops the application. /// </summary> public static void Stop() { bootstrapper.ShutDown(); } /// <summary> /// Creates the kernel that will manage your application. /// </summary> /// <returns>The created kernel.</returns> private static IKernel CreateKernel() { var kernel = new StandardKernel(); try { kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel); return kernel; } catch { kernel.Dispose(); throw; } } /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { kernel.Bind<DataClasses1DataContext>().ToMethod(c => new DataClasses1DataContext(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString)); kernel.Bind<IRepository>().To<SqlRepository>().InRequestScope(); } } } 

So, while I found the needed WebActivator lost a day, can it be done somehow differently? What is this example? Maybe now this is somehow done differently, because I think the example on mvc3 is already outdated. thanks for the help

    1 answer 1

    This opus is intended to show the course of my thoughts and omitted a lot of details and checks to simplify.

    It is not entirely clear what you want to end up with, but with this configuration, you get every time a dependency resolution (DataClasses1DataContext) creates a new instance of the context, which is passed a connection string taken from the application configuration (* .config) as an argument to the constructor. In general, the decision as a decision, although I probably would still make the configuration in a separate type and identify the interface:

     IConfigurationProvider { GetConcreteConnectionString(); } 

    Well, respectively, its implementation

     ConfigurationProvider : IConfigurationProvider { // Желательно приделать поле и хранить там полученную строку public GetConcreteConnectionString() => ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString; } 

    And in the context constructor, IConfigurationProvider would already accept:

     DataClasses1DataContext : DbContext { internal DataClasses1DataContext(IConfigurationProvider provider) : base(provider.GetConcreteConnectionString()) { } } 

    And binding would designate something like this:

     kernel.Bind<IConfigurationProvider>().To<ConfigurationProvider>().InSingletonScope(); /* Здесь нужно быть внимательным и как по мне лучше время жизни контролировать с использование расширения Ninject NamedScope на тот случай если в последующем вы решите использовать этот набор типов в другом формате приложения (WinForms || WPF) */ kernel.Bind<DataClasses1DataContext>().ToSelf(); kernel.Bind<IRepository>().To<SqlRepository>().InRequestScope(); 
    • Thanks for your thoughts and suggestions - Ethernets
    • one
      @Ethernets was glad to help - kimaman2