- a module for IT services business
- Mvc-like action binding in the WF web api ... ? * Yavsc.sln: * Web.csproj: * MvcActionValueBinder.cs: * yavscModel.csproj: * WFOrder.cs: * IWFOrder.cs: * BasketImpact.cs: * ProjectInfo.cs: * IWFModule.cs: * IWFCommand.cs: * WorkFlowController.cs: * NewProjectModel.cs: * IContentProvider.cs: * ITCPNpgsqlProvider.cs: * WorkFlowProvider.csproj: * ITContentProvider.csproj: * AssemblyInfo.cs: * OrderStatusChangedEventArgs.cs: * NpgsqlContentProvider.cs:
This commit is contained in:
parent
04804b89a9
commit
fa93ce7fee
19 changed files with 412 additions and 122 deletions
21
web/Controllers/BasketImpact.cs
Normal file
21
web/Controllers/BasketImpact.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
|
||||
using System.Web.Http;
|
||||
using WorkFlowProvider;
|
||||
using yavscModel.WorkFlow;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Http.ModelBinding;
|
||||
|
||||
namespace Yavsc.ApiControllers
|
||||
{
|
||||
|
||||
public class BasketImpact
|
||||
{
|
||||
public string ProductRef { get; set; }
|
||||
public int count { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -6,9 +6,11 @@ using System.Web;
|
|||
using System.Web.Http;
|
||||
using WorkFlowProvider;
|
||||
using yavscModel.WorkFlow;
|
||||
using System.Web.Http.Controllers;
|
||||
|
||||
namespace Yavsc.ApiControllers
|
||||
{
|
||||
[HttpControllerConfiguration(ActionValueBinder=typeof(Basic.MvcActionValueBinder))]
|
||||
public class WorkFlowController : ApiController
|
||||
{
|
||||
[HttpGet]
|
||||
|
|
@ -17,7 +19,13 @@ namespace Yavsc.ApiControllers
|
|||
return new { test="Hello World" };
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public object Order (BasketImpact bi)
|
||||
{
|
||||
return new { c="lmk,", message="Panier impacté", impactRef=bi.ProductRef, count=bi.count};
|
||||
}
|
||||
|
||||
/*
|
||||
public object Details(int id)
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
|
|
@ -27,6 +35,7 @@ namespace Yavsc.ApiControllers
|
|||
{
|
||||
throw new NotImplementedException ();
|
||||
}
|
||||
|
||||
public object Edit(int id)
|
||||
{
|
||||
throw new NotImplementedException ();
|
||||
|
|
@ -46,6 +55,6 @@ namespace Yavsc.ApiControllers
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
|
|
|||
89
web/MvcActionValueBinder.cs
Normal file
89
web/MvcActionValueBinder.cs
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Formatting;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Http.ModelBinding;
|
||||
using System.Web.Http.ValueProviders;
|
||||
using System.Web.Http.ValueProviders.Providers;
|
||||
|
||||
namespace Basic
|
||||
{
|
||||
// Binder with MVC semantics. Treat the body as KeyValue pairs and model bind it.
|
||||
public class MvcActionValueBinder : DefaultActionValueBinder
|
||||
{
|
||||
// Per-request storage, uses the Request.Properties bag. We need a unique key into the bag.
|
||||
private const string Key = "5DC187FB-BFA0-462A-AB93-9E8036871EC8";
|
||||
|
||||
public override HttpActionBinding GetBinding (HttpActionDescriptor actionDescriptor)
|
||||
{
|
||||
|
||||
HttpActionBinding actionBinding = new HttpActionBinding ();
|
||||
|
||||
HttpParameterDescriptor[] parameters = actionDescriptor.GetParameters ().ToArray ();
|
||||
HttpParameterBinding[] binders = Array.ConvertAll (parameters, p => DetermineBinding (actionBinding, p));
|
||||
|
||||
actionBinding.ParameterBindings = binders;
|
||||
return actionBinding;
|
||||
}
|
||||
|
||||
private HttpParameterBinding DetermineBinding (HttpActionBinding actionBinding, HttpParameterDescriptor parameter)
|
||||
{
|
||||
HttpConfiguration config = parameter.Configuration;
|
||||
|
||||
var attr = new ModelBinderAttribute(); // use default settings
|
||||
|
||||
ModelBinderProvider provider = attr.GetModelBinderProvider(config);
|
||||
|
||||
// Alternatively, we could put this ValueProviderFactory in the global config.
|
||||
List<ValueProviderFactory> vpfs = new List<ValueProviderFactory>(attr.GetValueProviderFactories(config));
|
||||
vpfs.Add(new BodyValueProviderFactory());
|
||||
//vpfs.Add (new RouteDataValueProviderFactory ());
|
||||
return new ModelBinderParameterBinding(parameter, provider, vpfs);
|
||||
}
|
||||
|
||||
// Derive from ActionBinding so that we have a chance to read the body once and then share that with all the parameters.
|
||||
private class MvcActionBinding : HttpActionBinding
|
||||
{
|
||||
// Read the body upfront , add as a ValueProvider
|
||||
public override Task ExecuteBindingAsync (HttpActionContext actionContext, CancellationToken cancellationToken)
|
||||
{
|
||||
HttpRequestMessage request = actionContext.ControllerContext.Request;
|
||||
HttpContent content = request.Content;
|
||||
if (content != null) {
|
||||
// content.ReadAsStreamAsync ().Result;
|
||||
FormDataCollection fd = content.ReadAsAsync<FormDataCollection> ().Result;
|
||||
if (fd != null) {
|
||||
|
||||
NameValueCollection nvc = fd.ReadAsNameValueCollection ();
|
||||
|
||||
System.Web.Http.ValueProviders.IValueProvider vp = new System.Web.Http.ValueProviders.Providers.NameValueCollectionValueProvider (nvc, CultureInfo.InvariantCulture);
|
||||
|
||||
request.Properties.Add (Key, vp);
|
||||
}
|
||||
}
|
||||
|
||||
return base.ExecuteBindingAsync (actionContext, cancellationToken);
|
||||
}
|
||||
}
|
||||
// Get a value provider over the body. This can be shared by all parameters.
|
||||
// This gets the values computed in MvcActionBinding.
|
||||
private class BodyValueProviderFactory : System.Web.Http.ValueProviders.ValueProviderFactory
|
||||
{
|
||||
public override System.Web.Http.ValueProviders.IValueProvider GetValueProvider (HttpActionContext actionContext)
|
||||
{
|
||||
object vp;
|
||||
actionContext.Request.Properties.TryGetValue (Key, out vp);
|
||||
return (System.Web.Http.ValueProviders.IValueProvider)vp; // can be null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>10.0.0</ProductVersion>
|
||||
<ProductVersion>8.0.30703</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{77044C92-D2F1-45BD-80DD-AA25B311B027}</ProjectGuid>
|
||||
<ProjectTypeGuids>{349C5851-65DF-11DA-9384-00065B846F21};{603C0E0B-DB56-11DC-BE95-000D561079B0};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
|
|
@ -62,25 +62,18 @@
|
|||
<Reference Include="Npgsql" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Security" />
|
||||
<Reference Include="nunit.framework, Version=2.6.0.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="nunit.framework, Version=2.6.0.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77" />
|
||||
<Reference Include="CodeKicker.BBCode">
|
||||
<HintPath>lib\CodeKicker.BBCode.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<Reference Include="System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<Reference Include="Mono.Posix" />
|
||||
<Reference Include="System.ServiceModel.Web" />
|
||||
<Reference Include="System.ServiceModel.Routing" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Net.Http.WebRequest" />
|
||||
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=c7439020c8fedf87">
|
||||
<Private>False</Private>
|
||||
<Package>monodevelop</Package>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Http.WebHost">
|
||||
|
|
@ -89,10 +82,10 @@
|
|||
</Reference>
|
||||
<Reference Include="System.Configuration.Install" />
|
||||
<Reference Include="System.Web.Services" />
|
||||
<Reference Include="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.WebPages.Deployment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
|
||||
<Reference Include="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<Reference Include="System.Web.WebPages.Deployment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
|
||||
<Reference Include="System.Net.Http.Formatting">
|
||||
<HintPath>..\..\..\..\..\usr\lib\mono\4.5\System.Net.Http.Formatting.dll</HintPath>
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
|
@ -147,6 +140,8 @@
|
|||
<Compile Include="Controllers\BasketController.cs" />
|
||||
<Compile Include="Basket\Basket.cs" />
|
||||
<Compile Include="FileInfoCollection.cs" />
|
||||
<Compile Include="Controllers\BasketImpact.cs" />
|
||||
<Compile Include="MvcActionValueBinder.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Views\Web.config" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue