-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dependency Injection in Views #946
Conversation
|
||
A Simple Example | ||
---------------- | ||
With the above warning in mind, you can inject a service into a view using the ``@inject`` directive, as shown below: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see some more descriptive text on the syntax of @inject
. It's effectively a property definition, where the first parameter is the type of the property and the second parameter is the property name. @using
statements apply when specifying the property type. The property will then get populated from DI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure it's worth calling out the warning here. If you emphasize it enough in the intro it'll stand on its own.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove "With the above warning in mind,"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code below (from the http://docs.asp.net/projects/mvc/en/latest/views/view-components.html sample), shows injecting a service into a view using the @Inject directive:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to include syntax and remove "with the above warning".
@NTaylorMullen Please review. |
|
||
`View sample files <https://github.com/aspnet/Docs/tree/1.0.0-rc1/mvc/views/dependency-injection/sample>`_ | ||
|
||
.. warning:: When applying this functionality, ensure you maintain `separation of concerns <http://deviq.com/separation-of-concerns>`_ between your controllers and views. Most of the data your views display should be passed in from the controller. This technique is recommended only for view-specific data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ehh, not sure this warrants a warning. Weave this into the above introduction on how dependency injection can be good and bad if not used correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd write it as
We recommend you maintain SoC between .. Generally the data displayed in views should be passed ...
what is This technique? Write that as
Injecting a service into a view is recommended for view-specific data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reworded; removed warning block.
Hmm, after looking over the format of this article I'm not sure the format's right. It dives into the linked samples pretty extensively but I was under the assumption this would be more of a reference doc (@danroth27 please verify)? If so I'd envision the layout being:
Each showcasing how to do inject the service and the outcome but not diving too much into detail on what's going on in the sample. |
|
||
public List<State> ListStates() | ||
{ | ||
// the abbreviated states of america |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some abbreviated states in the USA.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
@NTaylorMullen This topic wasn't intended to be an @ardalis Please do add a section about being able to override a previously injected service. For example you can override |
Are you sure overriding works? I have this: |
Hey @ardalis what bits are you using? I tried this locally with rc1-update1 bits and all seems well: public class FooHelper
{
public int Value { get; set; }
} |
@ardalis Taylor dot Mullen at microsoft.com |
@NTaylorMullen Thanks. Not sure how to resolve that immediately. I'll write the code I think should work and check it in and you can see if it looks good and wrap up this PR, ok? Thanks! |
@ardalis Does it work at runtime for you? |
@NTaylorMullen it does now that I remembered to add the helper to DI... Should be all good now. Shipit? |
|
||
By `Steve Smith`_ | ||
|
||
ASP.NET MVC 6 supports `dependency injection <https://docs.asp.net/en/latest/fundamentals/dependency-injection.html>`_ into views. This can be useful for view-specific services, such as localization or data required only for populating view elements. You should try to maintain `separation of concerns <http://deviq.com/separation-of-concerns>`_ between your controllers and views. Most of the data your views display should be passed in from the controller; injecting services into views is recommended only for view-specific data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danroth27 should we be changing names to core (instead of MVC 6)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope - not until we ship bits with the new names. See https://blogs.msdn.microsoft.com/webdev/2016/02/01/an-update-on-asp-net-core-and-net-core/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nuke ; injecting services into views is recommended only for view-specific data.
. You already mention this in the previous bit.
@NTaylorMullen Shouldn't we be using the latest RC2 bits at this late stage. Why have planned obsolescence? |
@ardalis Just spin up a clean VM with the latest bits, it's the lowest friction approach to a clean environment. I do my development on a VM and have a scheduled task robocopy the code to my publishing VM every minute. |
@Rick-Anderson RC2 bits have the core renames. If @danroth27 wants to hold off on those then RC1 bits should be fine. |
8de372d
to
67e6c6a
Compare
Merged. |
Fixes #128