Skip to content

Commit 0a18af1

Browse files
committed
Added ValueObject abstract class. Set NoTracking on CatalogItems
1 parent 963de04 commit 0a18af1

3 files changed

Lines changed: 89 additions & 26 deletions

File tree

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-

1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
4+
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
5+
using Microsoft.eShopOnContainers.Services.Catalog.API.ViewModel;
6+
using Microsoft.Extensions.Options;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
211
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
312
{
4-
using Extensions.Options;
5-
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.EntityFrameworkCore;
7-
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
8-
using Model;
9-
using System;
10-
using System.Collections.Generic;
11-
using System.Linq;
12-
using System.Threading.Tasks;
13-
using ViewModel;
14-
1513
[Route("api/v1/[controller]")]
1614
public class CatalogController : ControllerBase
1715
{
@@ -22,6 +20,8 @@ public CatalogController(CatalogContext context, IOptions<Settings> settings)
2220
{
2321
_context = context;
2422
_settings = settings;
23+
24+
((DbContext)context).ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
2525
}
2626

2727
// GET api/v1/[controller]/items/[?pageSize=3&pageIndex=10]
@@ -30,7 +30,7 @@ public CatalogController(CatalogContext context, IOptions<Settings> settings)
3030
public async Task<IActionResult> Items(int pageSize = 10, int pageIndex = 0)
3131
{
3232
var totalItems = await _context.CatalogItems
33-
.LongCountAsync();
33+
.LongCountAsync();
3434

3535
var itemsOnPage = await _context.CatalogItems
3636
.OrderBy(c=>c.Name)

src/Services/Catalog/Catalog.API/project.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{
2+
"buildOptions": {
3+
"emitEntryPoint": true,
4+
"preserveCompilationContext": true,
5+
"debugType": "portable"
6+
},
27
"dependencies": {
38
"Microsoft.NETCore.App": {
49
"version": "1.1.0",
@@ -23,9 +28,6 @@
2328
"Microsoft.EntityFrameworkCore.Design": "1.1.0",
2429
"Swashbuckle": "6.0.0-beta902"
2530
},
26-
"tools": {
27-
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
28-
},
2931
"frameworks": {
3032
"netcoreapp1.1": {
3133
"imports": [
@@ -34,16 +36,6 @@
3436
]
3537
}
3638
},
37-
"buildOptions": {
38-
"emitEntryPoint": true,
39-
"preserveCompilationContext": true,
40-
"debugType": "portable"
41-
},
42-
"runtimeOptions": {
43-
"configProperties": {
44-
"System.GC.Server": true
45-
}
46-
},
4739
"publishOptions": {
4840
"include": [
4941
"wwwroot",
@@ -56,6 +48,14 @@
5648
"Dockerfile"
5749
]
5850
},
51+
"runtimeOptions": {
52+
"configProperties": {
53+
"System.GC.Server": true
54+
}
55+
},
5956
"scripts": {},
57+
"tools": {
58+
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
59+
},
6060
"userSecretsId": "aspnet-Catalog.API-20161122013618"
6161
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
5+
{
6+
public abstract class ValueObject
7+
{
8+
protected static bool EqualOperator(ValueObject left, ValueObject right)
9+
{
10+
if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null))
11+
{
12+
return false;
13+
}
14+
return ReferenceEquals(left, null) || left.Equals(right);
15+
}
16+
17+
18+
protected static bool NotEqualOperator(ValueObject left, ValueObject right)
19+
{
20+
return !(EqualOperator(left, right));
21+
}
22+
23+
24+
protected abstract IEnumerable<object> GetAtomicValues();
25+
26+
27+
public override bool Equals(object obj)
28+
{
29+
if (obj == null || obj.GetType() != GetType())
30+
{
31+
return false;
32+
}
33+
ValueObject other = (ValueObject)obj;
34+
IEnumerator<object> thisValues = GetAtomicValues().GetEnumerator();
35+
IEnumerator<object> otherValues = other.GetAtomicValues().GetEnumerator();
36+
while (thisValues.MoveNext() && otherValues.MoveNext())
37+
{
38+
if (ReferenceEquals(thisValues.Current, null) ^ ReferenceEquals(otherValues.Current, null))
39+
{
40+
return false;
41+
}
42+
if (thisValues.Current != null && !thisValues.Current.Equals(otherValues.Current))
43+
{
44+
return false;
45+
}
46+
}
47+
return !thisValues.MoveNext() && !otherValues.MoveNext();
48+
}
49+
50+
51+
public override int GetHashCode()
52+
{
53+
return GetAtomicValues()
54+
.Select(x => x != null ? x.GetHashCode() : 0)
55+
.Aggregate((x, y) => x ^ y);
56+
}
57+
58+
public ValueObject GetCopy()
59+
{
60+
return this.MemberwiseClone() as ValueObject;
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)