Moving ViewContext to rendering, breaking coupling to RequestContext

This commit is contained in:
Ryan Nowak 2014-03-10 12:47:29 -07:00
parent 07974b44e2
commit 86f18f5da7
17 changed files with 251 additions and 95 deletions

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30110.0
VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE4C74-D06F-4874-A166-33305D2643CE}"
EndProject
@ -59,6 +59,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Mvc.Razor.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestCommon.net45", "test\TestCommon\TestCommon.net45.csproj", "{75A07B53-C5EE-4995-A55B-27562C23BCCD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Mvc.Rendering.Test.net45", "test\Microsoft.AspNet.Mvc.Rendering.Test\Microsoft.AspNet.Mvc.Rendering.Test.net45.csproj", "{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -149,6 +151,10 @@ Global
{75A07B53-C5EE-4995-A55B-27562C23BCCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75A07B53-C5EE-4995-A55B-27562C23BCCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75A07B53-C5EE-4995-A55B-27562C23BCCD}.Release|Any CPU.Build.0 = Release|Any CPU
{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -179,5 +185,6 @@ Global
{537CC0EE-4B62-4789-9AE9-94BE28E0D25A} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
{3EB2CFF9-6E67-4C03-9AC4-2DD169024938} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
{75A07B53-C5EE-4995-A55B-27562C23BCCD} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
EndGlobalSection
EndGlobal

View File

@ -8,7 +8,8 @@
"Microsoft.AspNet.Mvc.ModelBinding" : "",
"Microsoft.AspNet.Mvc.Core" : "",
"Microsoft.AspNet.Mvc" : "",
"Microsoft.AspNet.Mvc.Razor": ""
"Microsoft.AspNet.Mvc.Razor": "",
"Microsoft.AspNet.Mvc.Rendering" : ""
},
"configurations": {
"net45": {

View File

@ -35,10 +35,7 @@ namespace Microsoft.AspNet.Mvc
context.HttpContext.Response.ContentType = "text/html";
using (var writer = new StreamWriter(context.HttpContext.Response.Body, Encoding.UTF8, 1024, leaveOpen: true))
{
var viewContext = new ViewContext(context.HttpContext, context.RouteValues, ViewData)
{
ServiceProvider = _serviceProvider
};
var viewContext = new ViewContext(context.HttpContext, ViewData, _serviceProvider);
await view.RenderAsync(viewContext, writer);
}
}

View File

@ -153,10 +153,4 @@
<data name="ValueProviderResult_NoConverterExists" xml:space="preserve">
<value>The parameter conversion from type '{0}' to type '{1}' failed because no type converter can convert between these types.</value>
</data>
<data name="ViewDataDictionary_ModelCannotBeNull" xml:space="preserve">
<value>The model item passed is null, but this ViewData instance requires a non-null model item of type '{0}'.</value>
</data>
<data name="ViewData_WrongTModelType" xml:space="preserve">
<value>The model item passed into the ViewData is of type '{0}', but this ViewData instance requires a model item of type '{1}'.</value>
</data>
</root>

View File

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class ViewContext : RequestContext
{
public ViewContext(HttpContext context, IDictionary<string, object> routeValues, ViewData viewData) :
base(context, routeValues)
{
ViewData = viewData;
}
public IServiceProvider ServiceProvider { get; set; }
public ViewData ViewData { get; private set; }
}
}

View File

@ -27,9 +27,9 @@ namespace Microsoft.AspNet.Mvc.Razor
return base.RenderAsync(context, writer);
}
private void InitHelpers(RequestContext context)
private void InitHelpers(ViewContext context)
{
Html = new HtmlHelper<TModel>(context, ViewData);
Html = new HtmlHelper<TModel>(context.HttpContext, ViewData);
}
}
}

View File

@ -1,14 +1,13 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
using System.Reflection;
using System.Text;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Mvc
{
@ -21,23 +20,16 @@ namespace Microsoft.AspNet.Mvc
public static readonly string ValidationSummaryCssClassName = "validation-summary-errors";
public static readonly string ValidationSummaryValidCssClassName = "validation-summary-valid";
private static readonly object _html5InputsModeKey = new object();
public HtmlHelper(RequestContext requestContext, ViewData viewData)
public HtmlHelper([NotNull] HttpContext httpContext, ViewData viewData)
{
if (requestContext == null)
{
throw new ArgumentNullException("requestContext");
}
RequestContext = requestContext;
HttpContext = httpContext;
ViewData = viewData;
// ClientValidationRuleFactory = (name, metadata) => ModelValidatorProviders.Providers.GetValidators(metadata ?? ModelMetadata.FromStringExpression(name, ViewData), ViewContext).SelectMany(v => v.GetClientValidationRules());
}
//internal Func<string, ModelMetadata, IEnumerable<ModelClientValidationRule>> ClientValidationRuleFactory { get; set; }
public RequestContext RequestContext { get; private set; }
public HttpContext HttpContext { get; private set; }
public ViewData ViewData
{
@ -164,38 +156,6 @@ namespace Microsoft.AspNet.Mvc
return TagBuilder.CreateSanitizedId(name);
}
//public static string GenerateLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
//{
// return GenerateLink(requestContext, routeCollection, linkText, routeName, actionName, controllerName, null /* protocol */, null /* hostName */, null /* fragment */, routeValues, htmlAttributes);
//}
//public static string GenerateLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
//{
// return GenerateLinkInternal(requestContext, routeCollection, linkText, routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, htmlAttributes, true /* includeImplicitMvcValues */);
//}
//private static string GenerateLinkInternal(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool includeImplicitMvcValues)
//{
// string url = UrlHelper.GenerateUrl(routeName, actionName, controllerName, protocol, hostName, fragment, routeValues, routeCollection, requestContext, includeImplicitMvcValues);
// TagBuilder tagBuilder = new TagBuilder("a")
// {
// InnerHtml = (!String.IsNullOrEmpty(linkText)) ? HttpUtility.HtmlEncode(linkText) : String.Empty
// };
// tagBuilder.MergeAttributes(htmlAttributes);
// tagBuilder.MergeAttribute("href", url);
// return tagBuilder.ToString(TagRenderMode.Normal);
//}
//public static string GenerateRouteLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
//{
// return GenerateRouteLink(requestContext, routeCollection, linkText, routeName, null /* protocol */, null /* hostName */, null /* fragment */, routeValues, htmlAttributes);
//}
//public static string GenerateRouteLink(RequestContext requestContext, RouteCollection routeCollection, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
//{
// return GenerateLinkInternal(requestContext, routeCollection, linkText, routeName, null /* actionName */, null /* controllerName */, protocol, hostName, fragment, routeValues, htmlAttributes, false /* includeImplicitMvcValues */);
//}
public static string GetFormMethodString(FormMethod method)
{
switch (method)

View File

@ -1,18 +1,12 @@
using System;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Mvc
{
public class HtmlHelper<TModel> : HtmlHelper
{
public HtmlHelper(RequestContext requestContext, ViewData<TModel> viewData)
: base(requestContext, viewData)
public HtmlHelper([NotNull]HttpContext httpContext, ViewData<TModel> viewData)
: base(httpContext, viewData)
{
if (requestContext == null)
{
throw new ArgumentNullException("requestContext");
}
ViewData = viewData;
}

View File

@ -0,0 +1,62 @@
// <auto-generated />
namespace Microsoft.AspNet.Mvc.Rendering
{
using System.Globalization;
using System.Reflection;
using System.Resources;
internal static class Resources
{
private static readonly ResourceManager _resourceManager
= new ResourceManager("Microsoft.AspNet.Mvc.Rendering.Resources", typeof(Resources).GetTypeInfo().Assembly);
/// <summary>
/// The model item passed is null, but this ViewData instance requires a non-null model item of type '{0}'.
/// </summary>
internal static string ViewData_ModelCannotBeNull
{
get { return GetString("ViewData_ModelCannotBeNull"); }
}
/// <summary>
/// The model item passed is null, but this ViewData instance requires a non-null model item of type '{0}'.
/// </summary>
internal static string FormatViewData_ModelCannotBeNull(object p0)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ViewData_ModelCannotBeNull"), p0);
}
/// <summary>
/// The model item passed into the ViewData is of type '{0}', but this ViewData instance requires a model item of type '{1}'.
/// </summary>
internal static string ViewData_WrongTModelType
{
get { return GetString("ViewData_WrongTModelType"); }
}
/// <summary>
/// The model item passed into the ViewData is of type '{0}', but this ViewData instance requires a model item of type '{1}'.
/// </summary>
internal static string FormatViewData_WrongTModelType(object p0, object p1)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ViewData_WrongTModelType"), p0, p1);
}
private static string GetString(string name, params string[] formatterNames)
{
var value = _resourceManager.GetString(name);
System.Diagnostics.Debug.Assert(value != null);
if (formatterNames != null)
{
for (var i = 0; i < formatterNames.Length; i++)
{
value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}");
}
}
return value;
}
}
}

View File

@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ViewData_ModelCannotBeNull" xml:space="preserve">
<value>The model item passed is null, but this ViewData instance requires a non-null model item of type '{0}'.</value>
</data>
<data name="ViewData_WrongTModelType" xml:space="preserve">
<value>The model item passed into the ViewData is of type '{0}', but this ViewData instance requires a model item of type '{1}'.</value>
</data>
</root>

View File

@ -0,0 +1,21 @@
using System;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Mvc
{
public class ViewContext
{
public ViewContext(HttpContext context, ViewData viewData, IServiceProvider serviceProvider)
{
HttpContext = context;
ViewData = viewData;
ServiceProvider = serviceProvider;
}
public HttpContext HttpContext { get; private set; }
public IServiceProvider ServiceProvider { get; private set; }
public ViewData ViewData { get; private set; }
}
}

View File

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Dynamic;
namespace Microsoft.AspNet.Mvc.ModelBinding
namespace Microsoft.AspNet.Mvc
{
public class ViewData : DynamicObject
{

View File

@ -1,8 +1,9 @@
using System;
using System.Globalization;
using Microsoft.AspNet.Mvc.ModelBinding.Internal;
using Microsoft.AspNet.Mvc.Rendering;
namespace Microsoft.AspNet.Mvc.ModelBinding
namespace Microsoft.AspNet.Mvc
{
public class ViewData<TModel> : ViewData
{
@ -36,7 +37,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
string message;
if (value == null)
{
message = String.Format(CultureInfo.CurrentCulture, Resources.ViewDataDictionary_ModelCannotBeNull, typeof(TModel));
message = String.Format(CultureInfo.CurrentCulture, Resources.ViewData_ModelCannotBeNull, typeof(TModel));
}
else
{

View File

@ -18,6 +18,7 @@
"System.IO": "4.0.0.0",
"System.Reflection": "4.0.10.0",
"System.Reflection.Extensions": "4.0.0.0",
"System.Resources.ResourceManager": "4.0.0.0",
"System.Runtime": "4.0.20.0",
"System.Runtime.Extensions": "4.0.10.0",
"System.Runtime.InteropServices": "4.0.10.0",

View File

@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding.Test
namespace Microsoft.AspNet.Mvc.Rendering.Test
{
public class ViewDataOfTTest
{

View File

@ -0,0 +1,15 @@
{
"version" : "0.1-alpha-*",
"dependencies": {
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
"Microsoft.AspNet.PipelineCore": "0.1-alpha-*",
"Microsoft.AspNet.Mvc.Rendering" : "",
"TestCommon" : "",
"Moq": "4.0.10827",
"Xunit": "1.9.1",
"Xunit.extensions": "1.9.1"
},
"configurations": {
"net45": { }
}
}