0

This is Autofac code from project

https://github.com/MarlabsInc/SocialGoal

How can I change this code on Ninject?

I want use repository from social goal project but I prefer use ninject instead autofac.

public static class Bootstrapper
{
    public static void Run()
    {
        SetAutofacContainer();
        //Configure AutoMapper
        AutoMapperConfiguration.Configure();
    }
    private static void SetAutofacContainer()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
        builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();
        builder.RegisterAssemblyTypes(typeof(FocusRepository).Assembly)
        .Where(t => t.Name.EndsWith("Repository"))
        .AsImplementedInterfaces().InstancePerHttpRequest();
        builder.RegisterAssemblyTypes(typeof(GoalService).Assembly)
       .Where(t => t.Name.EndsWith("Service"))
       .AsImplementedInterfaces().InstancePerHttpRequest();

        builder.RegisterAssemblyTypes(typeof(DefaultFormsAuthentication).Assembly)
     .Where(t => t.Name.EndsWith("Authentication"))
     .AsImplementedInterfaces().InstancePerHttpRequest();

        builder.Register(c => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>( new SocialGoalEntities())))
            .As<UserManager<ApplicationUser>>().InstancePerHttpRequest();

        builder.RegisterFilterProvider();
        IContainer container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));            
    }
}

1 Answers1

1

InstancePerHttpRequest is done in ninject by appending InRequestScope() to a binding, like so:

kernel.Bind<IFoo>().To<UnitOfWork>().InRequestScope();

For InRequestScope to be available you'll need the appropriate Ninject.Web.Mvc* nuget package, see here. There's several posts on stackoverflow covering this (how to set up ninject for asp.net / MVC) already.


Things like RegisterAssemblyTypes is done using the Ninject.Extensions.Conventions. Example:

builder.RegisterAssemblyTypes(typeof(FocusRepository).Assembly)
       .Where(t => t.Name.EndsWith("Repository"))
       .AsImplementedInterfaces().InstancePerHttpRequest();

is done like:

kernel.Bind(x => x.From(typeof(FocusRepository).Assembly)
      .IncludingNonePublicTypes() // only required if the classes aren't `public`
      .SelectAllClasses()
      .EndingWith("Repository")
      .BindAllInterfaces()
      .Configure(b => b.InRequestScope()));
BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
  • It's working but only when I use GetAll method from services. When I use Delete or Create it not working. This repository is autofac repository, maybe this is problem. This repository looks the same like below link http://stackoverflow.com/questions/12390665/mvc-4-autofac-and-generic-repository-pattern – user3620627 Apr 24 '15 at 11:14
  • what exactly isn't working or just working when using `kernel.GetAll<>`? It's not a problem with the design of the repository class, they are not autofac specific, you can use them with Ninject, Autofac, simpleinjector... what ever. But binding (especially generics) works a bit differently. – BatteryBackupUnit Apr 24 '15 at 11:51
  • I think it would be a good idea to post a new question where you [show what you've implemented and what exactly isn't working](http://stackoverflow.com/help/mcve). – BatteryBackupUnit Apr 24 '15 at 11:52