After creating a class that inherits from NinjectModule, and overriding the Load() method with all my binding calls, where do I setup ninject in my asp.net web application? (MVC)
Is it a httpmodule that I have to create? global.asax?
UPDATE NB see comment on question - this only makes sense for MVC if using a very old Ninject (2 or earlier) and a very old MVC (2 or earlier)
There's a NinjectHttpApplication you derive from which auto-injects pages.
See Does anyone know of a good guide to get Ninject 2 working in ASP.NET MVC?
Related
This question already has answers here:
Ioc/DI - Why do I have to reference all layers/assemblies in application's entry point?
(4 answers)
Closed 3 years ago.
My application includes the Presentation Layer Project which is the Web-server's project.
In the Startup class of the PL I add a singleton service of type IServiceFacade. The controllers will be injected with it and use it to speak with the lower Service Layer Project functionality (PL Project holds a reference to the ServiceLayer Project).
The ServiceFacade holds an object of type IBusinessLogicFacade and uses it to speak with the lower Business Layer Project (Service Layer Project holds a reference to the Business Logic Project).
I also use the .net Core built-in logging API to inject ILogger to the controllers, the ServiceFacade and the BusinessLogicFacade, this only requires a reference to Microsoft.Extensions.Logging.
Like I added the ServiceFacade as a service, thus enabling the injection,
I would like to add the BusinessLogicFacade as a service, But this will require a reference from the PL Project to the Business Logic Project, breaking the layer separation.
I could create the BusinessLogicFacade "manually" in the ServiceFacade, but I will then need to supply an ILogger too, because I cannot use the injection of it when creating the BusinessLogicFacade manually.
services.AddSingleton<IServiceFacade,ServiceFacade> (OK, PL holds a reference to ServiceLayer Project)
services.AddSingleton<IBusinessLogicFacade,BusinessLogicFacade> (Not OK, Requires a reference from PL to BL).
Is there a way to receive the some parameters (as the ILogger) through injection when "manually" creating an object?
How should I approach this problem?
Unfortunately, this (dependency reference) is not straightforward to avoid - especially when using the default ASP.Net core container. You could work around this issue using 3rd party DI containers. You can read about architecture ideas here. Specifically for your question, read the "Note" section in the "UI layer types" paragraph (after Figure 5.12 in the specified link).
I want to know when we use the key word virtual with navigation properties (I learnt it's for lazy loading) but I'm reading a tutorial in https://docs.asp.net/en/latest/data/ef-mvc/intro.html that creates an asp.net web application core and they are not using that virtual anymore.
I checked on old versions (MVC4, MVC5) it is always there but not in the core.
Can anyone explain to me why?
You use virtual properties on entities, so Entity Framework can create a proxy class at runtime that inherits from your entity and injects a stub into overridden properties. This stub does a database call when you access the property's getter from code.
Entity Framework Core does not support lazy loading (yet, and probably never will), so there's no reason for it to mark properties as virtual.
See also: Loading Related Data - Entity Framework Core 1.0.0 Documentation in the officical documentation, Lazy Loading · Issue #3797 · aspnet/EntityFramework · GitHub on GitHub and Why use 'virtual' for class properties in Entity Framework model definitions? here on Stack Overflow.
Using Unity with MVC4 I'm able to target extension controllers just fine. And I'm also able to inject to the constructor of a local controller while still having all the extensible controllers available. I just can't seem to find a way to do inject into the constructor of an extensible controller. I tried MEF as well with even less success.
I'm looking for links to sample code that does this and any suggestions are welcome. I have mock-up code for testing and can post it if needed.
I am using Unity Container and Automapper and I'm looking for a place to call my initialization and bootstrapping code in my WCF Service. My internet searches have recommended one of four approaches as discussed here
http://blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx
(e.g.
1. Global.asax,
2. App_Code\AppInitialize,
3. custom ServiceHost, and
4. ServiceHostBase.InitializeRuntime)
However, I was also wondering if anyone has used the "Bootstrapper" project
http://bootstrapper.codeplex.com/
with the Unity and Automapper extensions.
So where/how is the best place to call the "Bootstrapper.Run()" code in a WCF Service? Sample code would be greatly appreciated. Thanks!
You can use web activator and call initialize method on your bootstrapper class. see details on https://www.nuget.org/packages/WebActivatorEx/
I want to use Ninject 2 on Castle Monorail. Searching on google, I found nothing about this.
I know there is Windsor which magically can integrate with Monorail, same as Ninject (with MVC extension) with ASP.NET MVC.
What steps I need to do to integrate DI framework (other than Windsor) with Monorail ? (any website link, tutorial, or code sample (preferably using Ninject 2))
fyi, I'm using C#
I don't think there's any documentation about this, but it's quite simple really. There's no magic to it. Since MonoRail and Windsor are completely separate projects, all you have to do is see how they integrate, then do the same for Ninject instead of Windsor.
More concretely, start with the MonoRailFacility which is the root of the integration. Instead of a Windsor facility, you'd use a Ninject module. Note it registers some components: IControllerTree, IWizardPageFactory, etc. The most important is IControllerFactory, which lets you resolve controllers from the container (in your case Ninject). You can leave all others as default for now (e.g. IFilterFactory/DefaultFilterFactory), and implement them as needed (i.e. when you need container control of filters).
Then call ServiceProviderLocator.Instance.AddLocatorStrategy(new NinjectAccessorStrategy()); where NinjectAccessorStrategy is an implementation of IAccessorStrategy which returns the Ninject kernel as a Castle.Core.IServiceProviderEx (which is nothing but a trivial extension of System.IServiceProvider). Since the Ninject kernel already implements IServiceProvider, it's trivial to write an adapter for IServiceProviderEx.