ASP.NET MVC 3 RTM way of ServiceLocator - asp.net-mvc

I'm a little confused with IServiceLocator, IMvcServiceLocator, IDependencyResolver, etc...
What is the ASP.NET MVC 3 RTM way of locating services?
All I need is to access something like
T Resolve<T>();
T Resolve<T>(string key);
from anywhere (including another assembly).
EDIT: example
My web app has a kind of modular architecture. Themes are one aspect of modules. Each theme is an individual class library. It's not possible to know what services a theme needs. E.g. some view might display a tag cloud widget and that widget needs an instance of TagRepository.
Currently I'm using Windsor for IoC and I could expose that container for modules. But I don't really want to make every module depend on Windsor. I would like to know if there's a solution in standard Mvc library since modules need a reference to that anyway.

Asp.net Mvc doesn't have it's own DI container. You would have to implement IDependencyResolver. Take a look at this question: Castle Windsor Dependency Resolver for MVC 3

Related

Add EF6 to a razor class library project to be shared by many BLAZOR projects?

Is it possible to add EF6 to a razor class library ? The goal is to have many components shared by other BLAZOR projects, but a lot of these controls require access to the database, which is the same for all the projects inside this solution. Also, I assume that the db access will be via a controller since it is not possible to inject as a service. Is this correct ?
Is it possible to add EF6 to a razor class library ?
Yes but you would immediately restrict that library ro Blazor Server side.
a lot of these controls require access to the database
That is not a great architecture
I assume that the db access will be via a controller
Not necessary with server-side, you can inject a DbContext into your pages and components
When you have components that depend strongly on your Entity classes and business logic then a Razor Class Library is maybe not the best choice.
When you do want this, create a specific layer with storage-agnostic models and interfaces for services. It's the 2 layers at the core in the Onion architecture.
Your component can depend on the interfaces and the main app can implement them.

Dependency Injection in .net core, Autofac vs StructureMap vs Factory Method to resolve interface if registered with multiple implementation

In my project I using chain of responsibility designing pattern for which I need to create multiple handlers which will implement same interface. In .net application(not .net core) I would have used DI using UnityContainer where I could have resolved handlers using named parameter. But in .net core I can not do that. Now I have few options to use other DI libraries like Autofac, Structuremap or create factory method which can give me objects based on name passed. Please help me in picking right approach between these or suggest something better if available. I have not used Autofac or Structuremap so I very little idea of same. Thanks.
For the most part, all DI tools out there achieve the same thing with different APIs. These days the differences are highlighted by your needs.
StructureMap or Autofac are both good candidates. It all boils down to "flavor".
Check this article. I think is a good one. Configuration comparison dependency injection containers
Again, don't stress over them unless you need a very very specific feature form one of them.
I would do Autofac just because I haven't had the need for something else however, I would not hesitate to use StructureMap, Ninject or whatever new kid on the block is brought up.

Dependecy Injection with MVC other than controllers

I am trying to understand dependency injection in MVC. I got some idea and implemented successfully with Controllers using extending DefaultControllerFactory and using ninject. But i read something like DI in views , Actions etc.
I didn't get DI in Views or my understanding is wrong. If yes, please tell me use and how to do that. I didn't understand other DI areas in MVC. Please explain the areas where DI is used in MVC other than Controllers
In MVC, you should try and make your Views as independent as possible. Ideally, the only dependency it rely on is the ViewModel (other view services and helpers are available through the WebPageBase class that all views inherit by default).

StructureMap and MVC 3.0

Has anyone used structuremap with the new DI features of asp.net mvc 3.0? If so, could you post some example code or some links to examples that have been helpful? In attempting to learn ASP.NET MVC 3, I am trying to make sure I use all the "new" features and am struggling with integrating a IoC container.
As others mentioned you'd want to look at Common Service Locator (CSL) that is a simple service resolution facade around any container (any of your choice).
MVC3 internally heavily uses the new IDependencyResolver interface which is somewhat like CSL but in ASP.NET MVC context. The good thing is that if you are already using CSL then you can set it as Dependency Resolver for MVC.
This topic is equally essential for any container and not specific to StructureMap.
So what is going on there:
You provide all necessary container registrations (preferrably through Registry DSL in case of StructureMap);
You get the StructureMap adapter for Common Service Locator;
You (optionally - if you need CSL along with DependencyResolver) register your StructureMap adapter as current Service Locator:
ServiceLocator.SetLocatorProvider(() => yourStructureMapAdapter)
You register your CSL (backed by StructureMap) as MVC DependencyResolver:
DependencyResolver.SetResolver(yourStructureMapAdapter)
MVC3 automatically wires everything up through IDependencyResolver interface internally (using all StrucutreMap DI auto-wiring capabilities).
Along with MVC3 baked-in IoC capabilities, use the power of IoC tool at your disposal (e.g. use Assemblies scanning available in StructureMap) to max extent.
Ran into this the other day, may be helpful:
http://weblogs.asp.net/rashid/archive/2009/02/15/asp-net-mvc-unity-and-common-service-locator.aspx
I looked around a bit and this is the first Google result I got. It gives a good of what is new in MVC 3 service location: http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html
While it is dependent on a beta version of MVC 3, I am sure with some experimentation one can figure it out.
In a nutshell, it looks like they added some interfaces and extension methods that you can use to call StructureMap, or whatever your preferred IoC library is.
Update:
I just happened upon this link in the blogs I subscribe to. It has some good-looking sample code.
http://stevesmithblog.com/blog/how-do-i-use-structuremap-with-asp-net-mvc-3/

How do I use dependency injection with an ASP.NET MVC model?

I would like to inject a dependency into an ASP.NET MVC model, but I can't figure out where in the pipeline to do the injection.
It's very straightforward with a ControllerFactory, but not nearly as much when dealing with models.
You can find a reasonable How-To on Shiju Vargheses Blog: ASP.NET MVC Tip: Dependency Injection with Unity Application Block
usually i inject dependencies in the controller like this
PersonController(IPersonRepository r)
{
\\ constrtuctor code
}
in the models probably when need some instance of something that inherits an interface you do something like this :
var r = container.Resolve<IPersonRepository>();
I ended up creating a service locator: http://martinfowler.com/articles/injection.html#UsingAServiceLocator
I find it easier than dealing with an IoC container and trying to insert my DI code all over the MVC pipeline.
I'd recommend reviewing S#arp Architecture
http://www.sharparchitecture.net/
Open source framework addon for asp.net mvc.
Are you completely sure you need to inject a dependency into your domain model itself? An entity or business object will typically encapsulate the state and expose methods to modify that state according to business rules. Code that does not fall into this category typically will be found in a service. Have you read into the concept of a domain service at all? Perhaps using one would better suit your needs and you won't need to inject any dependencies into your domain itself.
Checkout this sample I've created based on Ayende's explanations on his blog. Basically, I use Castle as my IoC container and I use Mvc Contrib to add all controllers to the container and make Mvc get them from it. Then I can inject anything into the containers, such as NHibernate ISession.
If you want to inject stuff inside your model classes (entities), NH now supports Dependency Injection of Hibernate-managed objects. See this, this, and this for specific examples for Spring and Windsor.
What your talking about is more along the lines of the Active Record pattern.
Whether AR is possible or not will depend on which ORM/DAO your using.
The AR pattern is generally better suited for small projects.

Resources