Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4c050a8

Browse files
removed the TrackingBehavior
1 parent bb82666 commit 4c050a8

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

‎src/AspnetRun.Application/Interfaces/IProductService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ public interface IProductService
1010
Task<ProductModel> GetProductById(int productId);
1111
Task<IEnumerable<ProductModel>> GetProductByName(string productName);
1212
Task<IEnumerable<ProductModel>> GetProductByCategory(int categoryId);
13-
Task<ProductModel> Create(ProductModel entityDto);
14-
Task Update(ProductModel entityDto);
15-
Task Delete(ProductModel entityDto);
13+
Task<ProductModel> Create(ProductModel productModel);
14+
Task Update(ProductModel productModel);
15+
Task Delete(ProductModel productModel);
1616
}
1717
}

‎src/AspnetRun.Application/Services/ProductService.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public async Task<IEnumerable<ProductModel>> GetProductByCategory(int categoryId
5050
return mapped;
5151
}
5252

53-
public async Task<ProductModel> Create(ProductModel entityDto)
53+
public async Task<ProductModel> Create(ProductModel productModel)
5454
{
55-
await ValidateProductIfExist(entityDto);
55+
await ValidateProductIfExist(productModel);
5656

57-
var mappedEntity = ObjectMapper.Mapper.Map<Product>(entityDto);
57+
var mappedEntity = ObjectMapper.Mapper.Map<Product>(productModel);
5858
if (mappedEntity == null)
5959
throw new ApplicationException($"Entity could not be mapped.");
6060

@@ -65,42 +65,43 @@ public async Task<ProductModel> Create(ProductModel entityDto)
6565
return newMappedEntity;
6666
}
6767

68-
public async Task Update(ProductModel entityDto)
68+
public async Task Update(ProductModel productModel)
6969
{
70-
ValidateProductIfNotExist(entityDto);
70+
ValidateProductIfNotExist(productModel);
71+
72+
var editProduct = await _productRepository.GetByIdAsync(productModel.Id);
73+
if (editProduct == null)
74+
throw new ApplicationException($"Entity could not be loaded.");
7175

72-
var mappedEntity = ObjectMapper.Mapper.Map<Product>(entityDto);
73-
if (mappedEntity == null)
74-
throw new ApplicationException($"Entity could not be mapped.");
76+
ObjectMapper.Mapper.Map<ProductModel, Product>(productModel, editProduct);
7577

76-
await _productRepository.UpdateAsync(mappedEntity);
78+
await _productRepository.UpdateAsync(editProduct);
7779
_logger.LogInformation($"Entity successfully updated - AspnetRunAppService");
7880
}
7981

80-
public async Task Delete(ProductModel entityDto)
82+
public async Task Delete(ProductModel productModel)
8183
{
82-
ValidateProductIfNotExist(entityDto);
83-
84-
var mappedEntity = ObjectMapper.Mapper.Map<Product>(entityDto);
85-
if (mappedEntity == null)
86-
throw new ApplicationException($"Entity could not be mapped.");
84+
ValidateProductIfNotExist(productModel);
85+
var deletedProduct = await _productRepository.GetByIdAsync(productModel.Id);
86+
if (deletedProduct == null)
87+
throw new ApplicationException($"Entity could not be loaded.");
8788

88-
await _productRepository.DeleteAsync(mappedEntity);
89+
await _productRepository.DeleteAsync(deletedProduct);
8990
_logger.LogInformation($"Entity successfully deleted - AspnetRunAppService");
9091
}
9192

92-
private async Task ValidateProductIfExist(ProductModel entityDto)
93+
private async Task ValidateProductIfExist(ProductModel productModel)
9394
{
94-
var existingEntity = await _productRepository.GetByIdAsync(entityDto.Id);
95+
var existingEntity = await _productRepository.GetByIdAsync(productModel.Id);
9596
if (existingEntity != null)
96-
throw new ApplicationException($"{entityDto.ToString()} with this id already exists");
97+
throw new ApplicationException($"{productModel.ToString()} with this id already exists");
9798
}
9899

99-
private void ValidateProductIfNotExist(ProductModel entityDto)
100+
private void ValidateProductIfNotExist(ProductModel productModel)
100101
{
101-
var existingEntity = _productRepository.GetByIdAsync(entityDto.Id);
102+
var existingEntity = _productRepository.GetByIdAsync(productModel.Id);
102103
if (existingEntity == null)
103-
throw new ApplicationException($"{entityDto.ToString()} with this id is not exists");
104+
throw new ApplicationException($"{productModel.ToString()} with this id is not exists");
104105
}
105106
}
106107
}

‎src/AspnetRun.Web/Startup.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,11 @@ public void ConfigureDatabases(IServiceCollection services)
102102
{
103103
// use in-memory database
104104
services.AddDbContext<AspnetRunContext>(c =>
105-
c.UseInMemoryDatabase("AspnetRunConnection")
106-
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));
105+
c.UseInMemoryDatabase("AspnetRunConnection"));
107106

108-
//// use real database
109-
//services.AddDbContext<AspnetRunContext>(c =>
110-
// c.UseSqlServer(Configuration.GetConnectionString("AspnetRunConnection"))
111-
// .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking));
107+
// use real database
108+
services.AddDbContext<AspnetRunContext>(c =>
109+
c.UseSqlServer(Configuration.GetConnectionString("AspnetRunConnection")));
112110
}
113111
}
114112
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /