Added ValueObject abstract class. Set NoTracking on CatalogItems

This commit is contained in:
unai 2017-02-06 12:51:56 +01:00
parent 963de048ef
commit 0a18af14dc
3 changed files with 89 additions and 26 deletions

View File

@ -1,17 +1,15 @@
 using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
{ {
using Extensions.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ViewModel;
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
public class CatalogController : ControllerBase public class CatalogController : ControllerBase
{ {
@ -22,6 +20,8 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
{ {
_context = context; _context = context;
_settings = settings; _settings = settings;
((DbContext)context).ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
} }
// GET api/v1/[controller]/items/[?pageSize=3&pageIndex=10] // GET api/v1/[controller]/items/[?pageSize=3&pageIndex=10]
@ -30,7 +30,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
public async Task<IActionResult> Items(int pageSize = 10, int pageIndex = 0) public async Task<IActionResult> Items(int pageSize = 10, int pageIndex = 0)
{ {
var totalItems = await _context.CatalogItems var totalItems = await _context.CatalogItems
.LongCountAsync(); .LongCountAsync();
var itemsOnPage = await _context.CatalogItems var itemsOnPage = await _context.CatalogItems
.OrderBy(c=>c.Name) .OrderBy(c=>c.Name)

View File

@ -1,4 +1,9 @@
{ {
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"debugType": "portable"
},
"dependencies": { "dependencies": {
"Microsoft.NETCore.App": { "Microsoft.NETCore.App": {
"version": "1.1.0", "version": "1.1.0",
@ -23,9 +28,6 @@
"Microsoft.EntityFrameworkCore.Design": "1.1.0", "Microsoft.EntityFrameworkCore.Design": "1.1.0",
"Swashbuckle": "6.0.0-beta902" "Swashbuckle": "6.0.0-beta902"
}, },
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
},
"frameworks": { "frameworks": {
"netcoreapp1.1": { "netcoreapp1.1": {
"imports": [ "imports": [
@ -34,16 +36,6 @@
] ]
} }
}, },
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"debugType": "portable"
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"publishOptions": { "publishOptions": {
"include": [ "include": [
"wwwroot", "wwwroot",
@ -56,6 +48,14 @@
"Dockerfile" "Dockerfile"
] ]
}, },
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
},
"scripts": {}, "scripts": {},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
},
"userSecretsId": "aspnet-Catalog.API-20161122013618" "userSecretsId": "aspnet-Catalog.API-20161122013618"
} }

View File

@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
{
public abstract class ValueObject
{
protected static bool EqualOperator(ValueObject left, ValueObject right)
{
if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null))
{
return false;
}
return ReferenceEquals(left, null) || left.Equals(right);
}
protected static bool NotEqualOperator(ValueObject left, ValueObject right)
{
return !(EqualOperator(left, right));
}
protected abstract IEnumerable<object> GetAtomicValues();
public override bool Equals(object obj)
{
if (obj == null || obj.GetType() != GetType())
{
return false;
}
ValueObject other = (ValueObject)obj;
IEnumerator<object> thisValues = GetAtomicValues().GetEnumerator();
IEnumerator<object> otherValues = other.GetAtomicValues().GetEnumerator();
while (thisValues.MoveNext() && otherValues.MoveNext())
{
if (ReferenceEquals(thisValues.Current, null) ^ ReferenceEquals(otherValues.Current, null))
{
return false;
}
if (thisValues.Current != null && !thisValues.Current.Equals(otherValues.Current))
{
return false;
}
}
return !thisValues.MoveNext() && !otherValues.MoveNext();
}
public override int GetHashCode()
{
return GetAtomicValues()
.Select(x => x != null ? x.GetHashCode() : 0)
.Aggregate((x, y) => x ^ y);
}
public ValueObject GetCopy()
{
return this.MemberwiseClone() as ValueObject;
}
}
}