From 4f948d42eeac27025b23162ea33a02b11b6d1f45 Mon Sep 17 00:00:00 2001 From: yarik Date: Tue, 14 Feb 2017 19:44:12 +0200 Subject: [PATCH] Mapper --- src/Maps/Startup.cs | 2 ++ src/Maps/project.json | 3 ++- src/MapsDb/DataService/BusStopDs.cs | 40 ++++++++-------------------------------- src/MapsDb/project.json | 3 ++- 4 files changed, 14 insertions(+), 34 deletions(-) diff --git a/src/Maps/Startup.cs b/src/Maps/Startup.cs index 1d15f79..5d9f40b 100644 --- a/src/Maps/Startup.cs +++ b/src/Maps/Startup.cs @@ -12,6 +12,7 @@ using MapsDb; using MapsDb.Interfaces; using MapsDb.DataService; using MapsModels; + namespace Maps { public class Startup @@ -59,6 +60,7 @@ namespace Maps services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); diff --git a/src/Maps/project.json b/src/Maps/project.json index 66d2df1..ae6ba10 100644 --- a/src/Maps/project.json +++ b/src/Maps/project.json @@ -36,7 +36,8 @@ "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.2", "Npgsql.EntityFrameworkCore.PostgreSQL.Design": "1.0.2", "MapsDb": "1.0.0-*", - "MapsModels": "1.0.0-*" + "MapsModels": "1.0.0-*", + "AutoMapper": "5.2.0" }, "tools": { diff --git a/src/MapsDb/DataService/BusStopDs.cs b/src/MapsDb/DataService/BusStopDs.cs index dd1ca30..ef93655 100644 --- a/src/MapsDb/DataService/BusStopDs.cs +++ b/src/MapsDb/DataService/BusStopDs.cs @@ -2,10 +2,12 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; +using AutoMapper; using MapsDb.Interfaces; using MapsDb.Models; using MapsModels.DsModels; using Microsoft.EntityFrameworkCore; + namespace MapsDb.DataService { public class BusStopDs : IBusStopDs @@ -13,29 +15,17 @@ namespace MapsDb.DataService private PostgresDbContext _context; public BusStopDs(){ _context = new PostgresDbContext(); + Mapper.Initialize(cnf => { + cnf.CreateMap(); + cnf.CreateMap(); + }); } public Task> GetIndexListAsync(PaginationDsM pagination){ return Task.Factory.StartNew(()=> { return GetAllBusStop(pagination); }); } private IList GetAllBusStop(PaginationDsM pagination) { - var data = _context.BusStop.Select(BusStop => new BusStopEditDsM - { - Id = BusStop.Id, - RoadId = BusStop.RoadId, - RegionId = BusStop.RegionId, - SettlementId = BusStop.SettlementId, - LocationLeft = BusStop.LocationLeft, - LocationRight = BusStop.LocationRight, - SurfaceTypeId = BusStop.SurfaceTypeId, - AreaStopAvailability = BusStop.AreaStopAvailability, - AreaLandAvailability = BusStop.AreaLandAvailability, - PocketAvailability = BusStop.PocketAvailability, - ToiletAvailability = BusStop.ToiletAvailability, - YearBuild = BusStop.YearBuild, - YearRepair = BusStop.YearRepair, - StateCommonId = BusStop.StateCommonId - }).Skip(pagination.from).Take(pagination.perPage); + var data = _context.BusStop.Select(BusStop => Mapper.Map(BusStop)).Skip(pagination.from).Take(pagination.perPage); switch (pagination.orderType()) { case "ASC": @@ -73,21 +63,7 @@ namespace MapsDb.DataService return Model; } public BusStop InsertModel(BusStopEditDsM data){ - BusStop Model = new BusStop{ - RoadId = data.RoadId, - RegionId = data.RegionId, - SettlementId = data.SettlementId, - LocationLeft = data.LocationLeft, - LocationRight = data.LocationRight, - SurfaceTypeId = data.SurfaceTypeId, - AreaStopAvailability = data.AreaStopAvailability, - AreaLandAvailability = data.AreaLandAvailability, - PocketAvailability = data.PocketAvailability, - ToiletAvailability = data.ToiletAvailability, - YearBuild = data.YearBuild, - YearRepair = data.YearRepair, - StateCommonId = data.StateCommonId - }; + BusStop Model = Mapper.Map(data); return Model; } public async Task DeleteAsync(int Id) diff --git a/src/MapsDb/project.json b/src/MapsDb/project.json index cd19575..4ef9cd8 100755 --- a/src/MapsDb/project.json +++ b/src/MapsDb/project.json @@ -13,7 +13,8 @@ "version": "1.0.0" }, "Npgsql.EntityFrameworkCore.PostgreSQL": "1.0", - "MapsModels": "1.0.0-*" + "MapsModels": "1.0.0-*", + "AutoMapper": "5.2.0", }, "frameworks": { "netcoreapp1.0": { -- libgit2 0.21.4