I am following this excellent article in order to create my own attribute in the mvc.net framework v2 .
The Author , Simone Chiaretta, uses Ninject to inject dependency within an ActionFilter using the attribute [Inject] over the dependencies he has in his class.
I would like to know if the same kind of technics might be used with structuremap.
I hope to have given enough details, if not please ask.. 😉
yours,
UPDATE 1:
Here is some code to enlighten a bit my problem
public class GreetingAttribute :
ActionFilterAttribute,
ISuperActionFilter,IGreetingAttribute
{
**[Inject]**
public IGestioneUtenti _service { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_service.Elenca();
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
ViewResult result = filterContext.Result as ViewResult;
_service.Elenca();
if (result != null)
{
result.ViewData["Title"] += " - " ;
}
}
}
I would have loved to be able to have this [Inject] attribute. So that my InvokeActionMethodWithFilters would have looked like this :
protected override ActionExecutedContext InvokeActionMethodWithFilters(
ControllerContext controllerContext, IList<IActionFilter> filters,
ActionDescriptor actionDescriptor, IDictionary<string, object> parameters)
{
foreach (IActionFilter actionFilter in filters)
{
ISuperActionFilter superActionFilter =
actionFilter as ISuperActionFilter;
if (superActionFilter != null)
{
ObjectFactory.DoInjectionForMarkedAttribute(actionFilter);
}
}
return base.InvokeActionMethodWithFilters(
controllerContext, filters, actionDescriptor, parameters);
}
Below my final implementation in an answer.
The SetterProperty attribute is equivalent to Ninject’s Inject.
Also, take a look at this blog post: http://codebetter.com/jeremymiller/2008/10/09/setter-injection-in-structuremap-2-5/ for more details on setter injection in Structuremap. Unfortunately Some of the examples use an older syntax , but all the ideas apply.