Reputation: 39
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));
}
}
Upvotes: 0
Views: 600
Reputation: 13233
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()));
Upvotes: 1