ninject

DbContext Disposed after first request when using Ninject's InRequestScope()

偶尔善良 提交于 2019-12-03 09:06:15
I am new to both EF and Ninject so forgive me if this does not make sense :) I have an MVC3 application with the Ninject and Ninject.Web.Common references. I am trying to inject a DbContext into my repositories. What I am seeing is that on the first request, everything works wonderfully but the subsequent requests return: System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed. at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression) at

Selective validation of child properties - Fluent Validation in MVC

℡╲_俬逩灬. 提交于 2019-12-03 08:47:47
I'm using Fluent Validation with the Ninject.Web.Mvc.FluentValidation library to automatically wire up all of my validators (and use dependency injection to create the validators). I created the following Models: public class Parent { public string Name { get; set; } public Child Child1 { get; set; } public Child Child2 { get; set; } } public class Child { public string ChildProperty { get; set; } } With the following validators: public class ParentValidator : AbstractValidator<Parent> { public ParentValidator() { RuleFor(model => model.Name).NotEmpty(); RuleFor(model => model.Child1)

Entity Framework Context in Singleton

孤人 提交于 2019-12-03 08:46:53
I'm building a App that use Context of EF in Singleton Pattern like NHibernate work with Session: public class DbContextFactory { private static volatile DbContextFactory _dbContextFactory; private static readonly object SyncRoot = new Object(); public DbContext Context; public static DbContextFactory Instance { get { if (_dbContextFactory == null) { lock (SyncRoot) { if (_dbContextFactory == null) _dbContextFactory = new DbContextFactory(); } } return _dbContextFactory; } } public DbContext GetOrCreateContext() { if (this.Context == null) this.Context = new DbContext(ConfigurationManager

ASP.NET MVC 3 and Global Filter Injection

大憨熊 提交于 2019-12-03 08:41:23
Hello am an trying to implement a global filter with injection. The filter looks like this. public class WikiFilter : IActionFilter { private IWikiService service; public WikiFilter(IWikiService service) { this.service = service; } public void OnActionExecuting(ActionExecutingContext filterContext) { !!!Code here!! } public void OnActionExecuted(ActionExecutedContext filterContext) { throw new NotImplementedException(); } } And i have attached the filter with injection the following way in my global.asax. public class MvcApplication : System.Web.HttpApplication, IAuthenticationApplication<User

How to tell Ninject to bind to an implementation it doesn't have a reference to

北慕城南 提交于 2019-12-03 08:05:24
I'm using NinjectMVC3 in my ASP.NET MVC3 project. I have 3 layers Foo.Web Foo.Services Foo.Data Foo.Web references Foo.Services but not Foo.Data. One of my services looks like this public class FooService : IFooService { private readonly IFooRepository _fooRepository; public FooService(IFooRepository fooRepository) { _fooRepository = fooRepository; } // ... } NinjectMVC3 executes this bootstrapping method in the Foo.Web startup private static void RegisterServices(IKernel kernel) { kernel.Bind<IFooService>().To<FooService>(); kernel.Bind<IFooRepository>().To<FooRepository>(); // Foo.Web doesn

Dependency Injection with constructor parameters that aren't interfaces

空扰寡人 提交于 2019-12-03 07:40:44
问题 I'm still a newbie at DI, and I am trying to understand if I am thinking of things the wrong way. I am working on a toy problem when I want to represent a Die object that has a dependency on an IRandomProvider. That interface is simple: public interface IRandomProvider { int GetRandom(int lower, int upper); } I want to have a Die constructor that looks like this: Die(int numSides, IRandomProvider provider) I'm trying to use a static DIFactory that has a method like this: public static T

Inject different classes that implement the same interface using Ninject

北城余情 提交于 2019-12-03 07:30:21
问题 I am implementing the builder design pattern to construct different kinds of graph objects to be displayed on a WPF UI. I am using Ninject as my IOC container. However, I am trying to find an elegant extendable solution. I have a ChartDirector object that takes a IChartBuilder as a dependency. I also have TemperatureChartBuilder and ThresholdChartBuilder that implement IChartBuilder . I want to inject either TemperatureChartBuilder OR ThresholdChartBuilder to ChartDirector depending on an

Avoiding Service Locator Antipattern with legacy app not designed for IOC

懵懂的女人 提交于 2019-12-03 07:26:25
问题 I have read often that Service Locators in IOC are an anti-pattern. Last year we introduced IOC (Ninject specifically) to our application at work. The app is legacy, it's very big and it's fragmented. There are lots of ways a class, or a chain of classes can get created. Some are created by the web framework (which is custom), some are created by nHibernate. Lots are just scattered around in weird places. How would we handle the different scenarios and not come up with something thats not at

Ninject with ASP.Net webforms and MVC

核能气质少年 提交于 2019-12-03 07:17:09
I want to use Ninject in a project which combines ASP.Net webforms and ASP.Net MVC. I'm using Ninject 2, but when I use NinjectHttpApplication from Ninject.Web.Mvc it complains when I use somethings like a PageBase that the Kernel is not created. I have the following in the Global.asax and am unsure what to add. public class MvcApplication : Ninject.Web.Mvc.NinjectHttpApplication { protected override void OnApplicationStarted() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); RegisterAllControllersIn(Assembly.GetExecutingAssembly()); } protected override Ninject

Ninject and ASP.NET Identity 2.0

只愿长相守 提交于 2019-12-03 07:11:50
I just upgraded the ASP.NET Identity Entity Framework package from 1.0 to 2.0 and one of the Ninject bindings is now broken: kernel.Bind<IUserStore<User>>().To<UserStore<User>>(); kernel.Bind<UserManager<User>>().ToSelf(); kernel.Bind<IRoleStore<IdentityRole>>().To<RoleStore<IdentityRole>>(); kernel.Bind<RoleManager<IdentityRole>>().ToSelf(); The second last one is giving this error on compile: The type 'Microsoft.AspNet.Identity.EntityFramework.RoleStore' cannot be used as type parameter 'TImplementation' in the generic type or method 'Ninject.Syntax.IBindingToSyntax.To()'. There is no