Announcements icon indicating copy to clipboard operation
Announcements copied to clipboard

MVC: Changes to [Activate] in beta-5

Open rynowak opened this issue 11 years ago • 54 comments

Hi all, I wanted to share with you some of the changes that are coming for beta-5 of MVC and some of the rationale about why we're changing [Activate]

What's changing

We're removing [Activate] totally and simplifying some of the things it was possible to accomplish using it. Where [Activate] was used to access context-objects from the current request, it will be replaced by bespoke attributes to handle the supported cases. We've also trimming down the number of context-objects that are available so that it's clear what is supported. Where [Activate] was used to access items from the HttpContext.RequestServices (DI), use constructor injection in its place.

[FromServices] is still supported in controllers on action parameters, public controller properties and model properties. There are no changes to [FromServices] in beta-5.

There are no changes to @inject for beta-5.

Overall there should be very little impact on MVC code unless you're making very heavy use of POCO-style controllers and view components.

POCO Controller Sample

Before:

public class MyController
{
    [Activate]
    public ActionContext ActionContext { get; set; }

    [Activate]
    public HttpContext HttpContext { get; set; }
}

After:

public class MyController
{
    [ActionContext]
    public ActionContext ActionContext { get; set; }

    public HttpContext HttpContext => ActionContext.HttpContext
}

POCO ViewComponent Sample

Before:

public class MyViewComponent
{
    [Activate]
    public IRespository Repository { get; set; }

    [Activate]
    public ViewContext ViewContext { get; set; }
}

After:

public class MyViewComponent
{
    public MyViewComponent(IRepository repository)
    {
        Repository = repository;
    }

    public IRespository Repository { get; }

    [ViewComponentContext]
    public ViewComponentContext ViewComponentContext { get; set; }
}

TagHelper Sample

Before:

public class MyTagHelper : TagHelper
{
    [HtmlAttributeNotBound]
    [Activate]
    public IHtmlEncoder Encoder { get; set; }

    [HtmlAttributeNotBound]
    [Activate]
    public ViewContext ViewContext { get; set; }
}

After:

public class MyTagHelper : TagHelper
{
    public MyTagHelper(IHtmlEncoder encoder)
    {
        Encoder = encoder;
    }

    public IHtmlEncoder Encoder { get; }

    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; }
}

About [Activate]

We originally created [Activate] to solve an implementation problem - how do you get ActionContext and other things into a controller without requiring the user to do this?

public class MyController : Controller
{
    public MyController(ActionContext context)
        : base(context)
    { }
}

We want writing controllers to have the minimum of boilerplate possible, so we created an attribute ([Activate]) that would tell the controller factory to inject certain known types. When we introduced new concepts (TagHelpers, ViewComponents), [Activate] came along for the ride to get the required context into those types.

Once we added the ability to inject arbitrary types from DI with [Activate], we realized that we'd built a secondary DI system that was specific to MVC and that only applied to a few concepts in the framework.

Our goal had always been to play nicely with 3rd party DI so that we could enable the use of the advanced concepts that dedicated DI systems can provide. Each piece of functionality we layer on top is another puzzle to solve for integration with a system like Ninject or Autofac (or your container of choice). We'd rather reset this and build towards a good integrated experience.

We've heard plenty of feedback on aspnet/Mvc#2151 asking us to embrace a merger between DI concepts and [Activate]. Our decision for now is that if there's a significant demand for more powerful DI features, then we should prioritize hooking up existing 3rd party DI solutions to fill the gap. We've done some enabling work for using DI to construct controllers, we might need to go further.

About [FromServices]

This is something we invented when revisiting model binding, and really grew independently of [Activate]. We've heard feedback in the context of the [Activate]/@inject discussion that [FromServices] is a bit awkward and oddball. We're still listening, but have chosen to do nothing for this release.

About @inject

We really feel like @inject is doing what we want it to from a usability standpoint. We heard the feedback loud and clear that everyone is happy with it, so it's staying put.

We don't feel like the many of the concerns we have about [Activate] can be applied to Razor code. Razor has a unique programming style and wouldn't support configuring many of the advanced DI features anyway (since you're not declaring constructors and properties).

The Future

We're still listening to feedback about this even though we're moving in a different direction. We want DI to be part of the Asp.Net platform as a whole, and don't want MVC to have a 'quirks mode' for certain framework pieces. Nothing here is set in stone, this is an iteration and it's still a beta release.

We'd love to see code samples of things you think are too hard or too verbose to do with these changes.

rynowak avatar May 22 '15 06:05 rynowak

Minor typo above:

    public MyTagHelper(IHtmlEncoder encoder)
    {
        Repository = repository;
    }

Should be:

    public MyTagHelper(IHtmlEncoder encoder)
    {
        Encoder = encoder;
    }

justinvp avatar May 22 '15 07:05 justinvp

Fixed, thanks :grinning:

rynowak avatar May 22 '15 07:05 rynowak

"The are no changes to @inject for beta-5" or "There are no changes to @inject for beta-5"?

RickyLin avatar May 22 '15 07:05 RickyLin

I clearly need a better proofreader.

There are no changes to @inject for beta-5

Correct

rynowak avatar May 22 '15 07:05 rynowak

Like I said here, it reads as very repetitive – ViewContext written three times.

If the contexts are supplied from outside of the DI system, why not continue to use the single attribute, but limit its use to supplying framework contexts only? I don't think that the term Activate has any ties to DI, but if it does, you could use something else, like [Provision].

If it is technically possible to insert and retrieve these objects from the DI container, then this attribute is not required at all, and a single [Inject] attribute could be used everywhere (I assume it would be possible to replace this attribute with the attribute supplied with one's DI framework of choice?).

glen-84 avatar May 22 '15 10:05 glen-84

Is there any reason why every dependency couldn't just be injected into a constructor?

michaldudak avatar May 22 '15 10:05 michaldudak

@michaldudak I was about to ask exactly the same question! To me it shows clearly the dependencies.

laurentkempe avatar May 22 '15 11:05 laurentkempe

+1 for using just using constructor injection and dropping the attributes.

kevinkuszyk avatar May 22 '15 11:05 kevinkuszyk

+1 @kevinkuszyk

m0sa avatar May 22 '15 12:05 m0sa

+1 @kevinkuszyk

ghost avatar May 22 '15 12:05 ghost

+1 @kevinkuszyk

ghost avatar May 22 '15 13:05 ghost

+1 @kevinkuszyk

blobor avatar May 22 '15 13:05 blobor

+1 for using just using constructor injection and dropping the attributes.[2]

or i just didin´t what is the advantage

2015-05-22 8:56 GMT-03:00 Kevin Kuszyk [email protected]:

+1 for using just using constructor injection and dropping the attributes.

— Reply to this email directly or view it on GitHub https://github.com/aspnet/Announcements/issues/28#issuecomment-104642798 .

marcodiniz avatar May 22 '15 13:05 marcodiniz

Was wondering the same thing as @michaldudak. Especially this 'hybrid-mode' might look odd:

public class MyTagHelper : TagHelper
{
    public MyTagHelper(IHtmlEncoder encoder)
    {
        Encoder = encoder;
    }

    public IHtmlEncoder Encoder { get; }

    [HtmlAttributeNotBound]
    [ViewContext]
    public ViewContext ViewContext { get; set; }
}

henkmollema avatar May 22 '15 14:05 henkmollema

Isn't it because the context(s) are not "primary" dependencies? In the controller example, you'd rather use constructor injection for your services and/or DbContexts, and get the framework stuff out of the way.

It's this:

public class MyController
{
    public ActionContext ActionContext { get; set; }

    public MyController(MyService service, ActionContext context) { /* ... */ }
}

Versus this:

public class MyController
{
    [ActionContext] or [Inject]
    public ActionContext ActionContext { get; set; }

    public MyController(MyService service) { /* ... */ }
}

Also, if you use the former, you'd expect both arguments to come from the DI container, but only one of them actually is (as far as I understand it).

This would be a lot simpler if the contexts could come from the DI container. That way, the developer can choose how he/she wants them injected.

glen-84 avatar May 22 '15 14:05 glen-84

If it's a dependency necessary for the component to work it should come through the constructor in my opinion. This categorization of dependencies of ctor vs. property injection just adds to confusion down the road.

This is somewhat related to this issue regarding per request services. https://github.com/aspnet/DependencyInjection/issues/237

CoreyKaylor avatar May 22 '15 14:05 CoreyKaylor

The items that are provider by [ViewContext] are not services, and don't come from the DI container, so that's why they are a separate attribute. Your Ninject, Autofact, etc. has no idea what ViewData is. Part of the issue before was conflating "all injected things" as one big bucket, where in reality just because something "comes from somewhere else" doesn't mean it's DI.

Eilon avatar May 22 '15 17:05 Eilon

@Eilon, that's what I was thinking, just not able to articulate it...they are different concerns managed by different "containers"

terryt2000 avatar May 22 '15 18:05 terryt2000

IMO it would be great if the programmer won't have to think if a particular thing should be injected into a constructor or activated some other way. But I do understand this may be impossible/too hard to implement. If, however, we have to use attributes, I'll vote against specific ones like ActionContextAttribute as having the same identifier repeated three times (with three different meanings) just doesn't look right to me.

michaldudak avatar May 22 '15 20:05 michaldudak

I think this is why many of the existing DI frameworks (Ninject, Unity, Autofac) support adding additional bindings to child scopes, rather than just registering them all up front. A developer shouldn't be concerned with where a value is coming from when setting up an object; isn't that one of the major pillars of DI?

Also, +1 for using constructor injection for everything, or adding attribute injection for if you really don't want child objects to have to worry about it.

mdekrey avatar May 22 '15 22:05 mdekrey