T
T
tuxcod2014-10-02 23:07:37
ASP.NET
tuxcod, 2014-10-02 23:07:37

ASP.NET + Problems with Ninject. Updated data is not updated in DBContext. Where is the mistake?

Good day.
Faced a specific problem in my opinion.
Description of the problem:
The site runs on ASP.NET MVC, using Ninject, requests to the database are made through asynchronous requests.
There is Repository with IRepository interface, UnitOfWork with IUnitOfWork, IDatabaseFactory with DataBaseFactory and services that work with repositories.
All this is connected through Ninject:

Bind<IUnitOfWork>().To<UnitOfWork>().InSingletonScope();
            Bind<IDatabaseFactory>().To<DatabaseFactory>().InSingletonScope();

            this.Bind(x => x
                .From(typeof (UserRepository).Assembly)
                .SelectAllClasses().InNamespaceOf(typeof (UserRepository))
                .EndingWith("Repository")
                .BindAllInterfaces()
                .Configure(b => b.InSingletonScope()));

            this.Bind(x => x
                .From(typeof (UserService).Assembly)
                .SelectAllClasses().InNamespaceOf(typeof (UserService))
                .EndingWith("Service")
                .BindAllInterfaces()
                .Configure(b => b.InSingletonScope()));

All work is built in the following way:
The request comes to the Controller from the controller to the service in which IRepository and IUnitOfWork are initialized.
The service receives data from the repository asynchronously. When adding/updating data, the service sends data to the repository and after that a commit is called in the service via
unitOfWork the value it had before the change. If the project is rebuilt, then when the page is updated, the data will be loaded fresh (correct) from the database, but the problem persists when the update is repeated.

Something tells me that the problem is in setting up Ninject
Please help me solve my problem.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tuxcod, 2014-10-03
@tuxcod

Found a solution to this problem uninstalled Ninject and installed AutoFac - everything works fine.
I immediately post a piece on setting up AutoFac, suddenly someone will encounter the same problem:
1. Install the nuget package AutoFac and AutoFac API
2. Add the setting function to Global.asax

private void RegisterIOC()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterApiControllers(typeof(MvcApplication).Assembly);
            Core.AutofacConfiguration.Init(builder);
            builder.RegisterFilterProvider();
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new Autofac.Integration.WebApi.AutofacWebApiDependencyResolver(container);
        }

3. Create an AutoFac configuration file, let's call it AutofacConfiguration, and write the implementation of the interface to the class:
public static class AutofacConfiguration
    {
        public static void Init(ContainerBuilder builder)
        {
           //Пример настройки
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();

        }

    }

4. Add function initialization to Application_Start
RegisterIOC();

P
Pavel_Develop, 2015-01-29
@Pavel_Develop

"but when fetching changed data, the changed field from the repository returns the old value that was before the change." - different database contexts are probably created, one when the data is loaded, the other when the data changes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question