init odata
implemented simple access provided db context via DI
This commit is contained in:
@@ -56,7 +56,25 @@
|
||||
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.AspNet.OData, Version=7.1.0.21120, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.OData.7.1.0\lib\net45\Microsoft.AspNet.OData.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="Microsoft.Extensions.DependencyInjection, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Extensions.DependencyInjection.2.2.0\lib\net461\Microsoft.Extensions.DependencyInjection.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.2.2.0\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.OData.Core, Version=7.5.4.30215, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.OData.Core.7.5.4\lib\portable-net45+win8+wpa81\Microsoft.OData.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.OData.Edm, Version=7.5.4.30215, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.OData.Edm.7.5.4\lib\portable-net45+win8+wpa81\Microsoft.OData.Edm.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Spatial, Version=7.5.4.30215, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Spatial.7.5.4\lib\portable-net45+win8+wpa81\Microsoft.Spatial.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -66,6 +84,16 @@
|
||||
<Reference Include="System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.7\lib\net45\System.Net.Http.Formatting.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Reflection, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Reflection.4.3.0\lib\net462\System.Reflection.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Runtime.Extensions, Version=4.1.1.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Runtime.Extensions.4.3.1\lib\net462\System.Runtime.Extensions.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Entity" />
|
||||
<Reference Include="System.Web.ApplicationServices" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
@@ -98,9 +126,9 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="App_Start\Bootstrapper.cs" />
|
||||
<Compile Include="App_Start\DependencyInjectionConfig.cs" />
|
||||
<Compile Include="App_Start\WebApiConfig.cs" />
|
||||
<Compile Include="Controllers\ValuesController.cs" />
|
||||
<Compile Include="Controllers\SongsController.cs" />
|
||||
<Compile Include="Database\DataContext.cs" />
|
||||
<Compile Include="Database\Model\Access.cs" />
|
||||
<Compile Include="Database\Model\Change.cs" />
|
||||
@@ -122,7 +150,9 @@
|
||||
<ItemGroup>
|
||||
<Content Include="favicon.ico" />
|
||||
<Content Include="Global.asax" />
|
||||
<Content Include="Web.config" />
|
||||
<Content Include="Web.config">
|
||||
<SubType>Designer</SubType>
|
||||
</Content>
|
||||
<Content Include="Web.Debug.config">
|
||||
<DependentUpon>Web.config</DependentUpon>
|
||||
</Content>
|
||||
@@ -130,9 +160,7 @@
|
||||
<DependentUpon>Web.config</DependentUpon>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="App_Data\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
using Autofac;
|
||||
using Autofac.Integration.WebApi;
|
||||
using System.Reflection;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace API.App_Start
|
||||
{
|
||||
/// <remarks>
|
||||
/// https://autofaccn.readthedocs.io/en/latest/integration/webapi.html#quick-start
|
||||
/// </remarks>
|
||||
public class Bootstrapper
|
||||
{
|
||||
public void InitContainer() {
|
||||
var config = GlobalConfiguration.Configuration;
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
|
||||
builder.RegisterWebApiFilterProvider(config);
|
||||
builder.RegisterWebApiModelBinderProvider();
|
||||
|
||||
RegisterServices(builder);
|
||||
|
||||
var container = builder.Build();
|
||||
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
|
||||
}
|
||||
|
||||
private void RegisterServices(ContainerBuilder builder) {
|
||||
// ... builder.RegisterType<object>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
39
API/API/App_Start/DependencyInjectionConfig.cs
Normal file
39
API/API/App_Start/DependencyInjectionConfig.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using API.Database;
|
||||
using Autofac;
|
||||
using Autofac.Integration.WebApi;
|
||||
using ProductService.Controllers;
|
||||
using System.Reflection;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace API.App_Start {
|
||||
/// <remarks>
|
||||
/// https://autofaccn.readthedocs.io/en/latest/integration/webapi.html#quick-start
|
||||
/// </remarks>
|
||||
public static class DependencyInjectionConfig {
|
||||
public static void RegisterDependecyInjection(this HttpConfiguration config) {
|
||||
var builder = new ContainerBuilder();
|
||||
builder.RegisterControllers();
|
||||
builder.RegisterWebApiFilterProvider(config);
|
||||
builder.RegisterWebApiModelBinderProvider();
|
||||
|
||||
builder.RegisterServices();
|
||||
builder.RegisterDataProvider();
|
||||
|
||||
var container = builder.Build();
|
||||
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
|
||||
}
|
||||
|
||||
private static void RegisterControllers(this ContainerBuilder builder) {
|
||||
builder.RegisterType<SongsController>().InstancePerRequest();
|
||||
}
|
||||
|
||||
private static void RegisterServices(this ContainerBuilder builder) {
|
||||
// ... builder.RegisterType<object>();
|
||||
}
|
||||
|
||||
private static void RegisterDataProvider(this ContainerBuilder builder) {
|
||||
builder.RegisterType<DataContext>().InstancePerRequest();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using API.App_Start;
|
||||
using API.Database.Model;
|
||||
using Microsoft.AspNet.OData.Builder;
|
||||
using Microsoft.AspNet.OData.Extensions;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace API
|
||||
{
|
||||
public static class WebApiConfig
|
||||
{
|
||||
public static void Register(HttpConfiguration config)
|
||||
{
|
||||
namespace API {
|
||||
public static class WebApiConfig {
|
||||
public static void Register(HttpConfiguration config) {
|
||||
// Web API configuration and services
|
||||
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
|
||||
|
||||
// Web API routes
|
||||
config.MapHttpAttributeRoutes();
|
||||
|
||||
config.Routes.MapHttpRoute(
|
||||
name: "DefaultApi",
|
||||
routeTemplate: "api/{controller}/{id}",
|
||||
defaults: new { id = RouteParameter.Optional }
|
||||
);
|
||||
|
||||
// Web API oData configuration
|
||||
var builder = new ODataConventionModelBuilder();
|
||||
builder.EntitySet<Song>("songs");
|
||||
config.MapODataServiceRoute(
|
||||
routeName: "songs",
|
||||
routePrefix: "odata",
|
||||
model: builder.GetEdmModel());
|
||||
|
||||
config.RegisterDependecyInjection();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
78
API/API/Controllers/SongsController.cs
Normal file
78
API/API/Controllers/SongsController.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using API.Database;
|
||||
using API.Database.Model;
|
||||
using Microsoft.AspNet.OData;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Entity.Infrastructure;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Http;
|
||||
namespace ProductService.Controllers {
|
||||
public class SongsController : ODataController {
|
||||
|
||||
public SongsController(DataContext db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
private readonly DataContext db;
|
||||
|
||||
private bool ProductExists(int key) {
|
||||
return db.Songs.Any(p => p.ID == key);
|
||||
}
|
||||
|
||||
[EnableQuery]
|
||||
public IQueryable<Song> Get() {
|
||||
return db.Songs;
|
||||
}
|
||||
|
||||
[EnableQuery]
|
||||
public SingleResult<Song> Get([FromODataUri] int key) {
|
||||
var result = db.Songs.Where(p => p.ID == key);
|
||||
return SingleResult.Create(result);
|
||||
}
|
||||
|
||||
public async Task<IHttpActionResult> Post(Song song) {
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
db.Songs.Add(song);
|
||||
await db.SaveChangesAsync();
|
||||
return Created(song);
|
||||
}
|
||||
|
||||
public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<Song> product) {
|
||||
if (!ModelState.IsValid) {
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
var entity = await db.Songs.FindAsync(key);
|
||||
if (entity == null) return NotFound();
|
||||
|
||||
product.Patch(entity);
|
||||
try {
|
||||
await db.SaveChangesAsync();
|
||||
} catch (DbUpdateConcurrencyException) {
|
||||
if (!ProductExists(key)) return NotFound();
|
||||
|
||||
throw;
|
||||
|
||||
}
|
||||
return Updated(entity);
|
||||
}
|
||||
|
||||
public async Task<IHttpActionResult> Put([FromODataUri] int key, Song update) {
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
if (key != update.ID) return BadRequest();
|
||||
|
||||
db.Entry(update).State = EntityState.Modified;
|
||||
try {
|
||||
await db.SaveChangesAsync();
|
||||
} catch (DbUpdateConcurrencyException) {
|
||||
if (!ProductExists(key)) return NotFound();
|
||||
|
||||
throw;
|
||||
|
||||
}
|
||||
return Updated(update);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace API.Controllers
|
||||
{
|
||||
public class ValuesController : ApiController
|
||||
{
|
||||
// GET api/values
|
||||
public IEnumerable<string> Get()
|
||||
{
|
||||
return new string[] { "value1", "value2" };
|
||||
}
|
||||
|
||||
// GET api/values/5
|
||||
public string Get(int id)
|
||||
{
|
||||
return "value";
|
||||
}
|
||||
|
||||
// POST api/values
|
||||
public void Post([FromBody]string value)
|
||||
{
|
||||
}
|
||||
|
||||
// PUT api/values/5
|
||||
public void Put(int id, [FromBody]string value)
|
||||
{
|
||||
}
|
||||
|
||||
// DELETE api/values/5
|
||||
public void Delete(int id)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,9 @@
|
||||
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
|
||||
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
|
||||
</configSections>
|
||||
<connectionStrings>
|
||||
<add name="DataContext" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=wgenerator;Integrated Security=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />
|
||||
</connectionStrings>
|
||||
<appSettings>
|
||||
<add key="webpages:Version" value="3.0.0.0" />
|
||||
<add key="webpages:Enabled" value="false" />
|
||||
@@ -61,6 +64,34 @@
|
||||
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-5.2.7.0" newVersion="5.2.7.0" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Runtime.Extensions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Microsoft.OData.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Microsoft.OData.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Microsoft.Extensions.DependencyInjection" publicKeyToken="adb9793829ddae60" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-2.2.0.0" newVersion="2.2.0.0" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Microsoft.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
<entityFramework>
|
||||
|
||||
@@ -4,10 +4,28 @@
|
||||
<package id="Autofac" version="4.9.1" targetFramework="net462" />
|
||||
<package id="Autofac.WebApi2" version="4.2.0" targetFramework="net462" />
|
||||
<package id="EntityFramework" version="6.2.0" targetFramework="net462" />
|
||||
<package id="Microsoft.AspNet.OData" version="7.1.0" targetFramework="net462" />
|
||||
<package id="Microsoft.AspNet.WebApi" version="5.2.7" targetFramework="net462" />
|
||||
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net462" />
|
||||
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net462" />
|
||||
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.7" targetFramework="net462" />
|
||||
<package id="Microsoft.Extensions.DependencyInjection" version="2.2.0" targetFramework="net462" />
|
||||
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="2.2.0" targetFramework="net462" />
|
||||
<package id="Microsoft.OData.Core" version="7.5.4" targetFramework="net462" />
|
||||
<package id="Microsoft.OData.Edm" version="7.5.4" targetFramework="net462" />
|
||||
<package id="Microsoft.Spatial" version="7.5.4" targetFramework="net462" />
|
||||
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net462" />
|
||||
<package id="System.Collections" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.ComponentModel" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.Globalization" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.Linq" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.Reflection" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.Runtime.Extensions" version="4.3.1" targetFramework="net462" />
|
||||
<package id="System.Threading" version="4.3.0" targetFramework="net462" />
|
||||
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net462" />
|
||||
<package id="WebGrease" version="1.6.0" targetFramework="net462" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user