autofac / Autofac.Mvc

ASP.NET MVC integration for Autofac
MIT License
49 stars 23 forks source link

Enable properties on MVC models to be injected during model binding #8

Closed tillig closed 8 years ago

tillig commented 9 years ago

From @alexmg on January 22, 2014 14:5

From steve.he...@gmail.com on February 08, 2011 14:41:08

For the past year and half, I've been using an extension method and a generic model binder combination that allow me to inject view models on am Mvc ActionResult.

For example - say I have a view model called "SimpleOrderModel" that injects a repository (imho a controller shouldn't understand how to load and save a model - it's the model's job).

Therefore, I should be able to create an action result that looks like this:

[HttpPost]
public ActionResult Configure(SimpleOrderModel simpleOrderModel)
{
  if(ModelState.IsValid)
  {

To go along with this code, I have a registration extension where the call looks like this:

builder.RegisterInjectedModel<SimpleOrderViewModel>().InstancePerDependency();

I find this incredibly useful.

I have this in my local repo and I've included a patch file of the implementation. I could also move this to a named branch or check it my fork depending on the process of including it.

Attachment: autofacmvc_iocmodelbinder.patch

Original issue: http://code.google.com/p/autofac/issues/detail?id=292

Copied from original issue: autofac/Autofac#292

tillig commented 9 years ago

From @alexmg on January 22, 2014 14:5

From travis.illig on September 21, 2012 09:30:36

Labels: Module-Integration-Mvc

tillig commented 9 years ago

From @alexmg on January 22, 2014 14:5

From travis.illig on December 05, 2012 10:53:22

This appears to overlap the ExtensibleActionInvoker, which can also do some model injection. Have you tried that? Is there something missing from ExtensibleActionInvoker that we could enable to solve this?

tillig commented 9 years ago

From @alexmg on January 22, 2014 14:5

From travis.illig on January 02, 2013 12:10:13

This looks like it is somewhat related to the Autofac.Integration.Mvc.ExtensibleActionInvoker, which injects parameters into an action. I'm not sure if the two functions could (or should) be tied together, but if/when this gets implemented it's something to think about. It would mean you don't have to register specific models for injection - it'd "just work."

Summary: Enable properties on MVC models to be injected during model binding (was: An Mvc ModelBinder extension)
Owner: ---
Labels: -Type-Defect Type-Enhancement

tillig commented 8 years ago

Given Google Code is now out of commission, I've brought forward the patch file originally posted: autofacmvc_iocmodelbinder.patch.txt

tillig commented 8 years ago

I added the ability to pass an IPropertySelector into the ExtensibleActionInvoker when you register it so you can now inject properties on models as needed.