I have a GenericService Add method like so:-
public bool Add(T entity, Expression<Func<T, bool>> filter = null)
{
try
{
_genericRepository.Add(entity, filter);
}
catch (Exception e)
{
return false;
}
return true;
}
and a GenericRepository Add method like so:-
public void Add(T entity, Expression<Func<T, bool>> filter = null)
{
var existing = Get<T>(filter);
if (existing.Result != null) return;
Context.Add(entity);
Save();
}
This is the call I am doing in the ProductsController:-
[HttpPost]
public IActionResult Create([FromBody] Product product)
{
if (product == null)
return BadRequest();
var result = _productsService.Add(product, m => m.Name == product.Name);
if (result)
{
return CreatedAtRoute("GetProducts", new { id = product.Id }, product);
}
return BadRequest("Item not added");
}
I am creating this by means of an integration test as follows :-
testBrand = new Brand { Name = "testBrand" };
testImage = new Image { Name = "testImage", Url = "/Brands/adidas_logo_test.png" };
testBrand.Image = testImage;
testCategory = new Category {Name = "testCategory"};
testProduct = new Product
{
Category = testCategory,
Name = "testProduct",
Brand = testBrand,
BrandId = testBrand.Id,
CategoryId = testCategory.Id,
Deal = false,
Description = "testDescription",
Discount = "50% Discount",
Image = testImage,
ImageId = testImage.Id,
Price = new decimal(50.00),
Stock = 5
};
[Test]
public async Task Create_CreateAProduct_NewBrandNewCategoryNewImageProductsController()
{
//Arrange
//Act
//create new image
var requestImage = "api/Images/";
var postResponseImage = await _client.PostAsJsonAsync(requestImage, testImage);
var created = await postResponseImage.Content.ReadAsStringAsync();
var createdImage = JsonConvert.DeserializeObject<Image>(created);
//Act
testBrand.Image = createdImage;
testBrand.ImageId = createdImage.Id;
testImage.Id = createdImage.Id;
var postResponseProduct = await _client.PostAsJsonAsync(requestProduct, testProduct);
var createdProduct = await postResponseProduct.Content.ReadAsStringAsync();
var createdProductObj = JsonConvert.DeserializeObject<Product>(createdProduct);
var getResponse = await _client.GetAsync(requestProduct + "Get/" + createdProductObj.Id);
var fetched = await getResponse.Content.ReadAsStringAsync();
var fetchedProduct = JsonConvert.DeserializeObject<Product>(fetched);
// Assert
Assert.IsTrue(postResponseProduct.IsSuccessStatusCode);
Assert.IsTrue(getResponse.IsSuccessStatusCode);
Assert.AreEqual(testProduct.Name, createdProductObj.Name);
Assert.AreEqual(testProduct.Name, fetchedProduct.Name);
Assert.AreNotEqual(Guid.Empty, createdProductObj.Id);
Assert.AreEqual(createdProductObj.Id, fetchedProduct.Id);
}
Everything works fine, until I try to insert an entity that has multiple related entities. Let me give an example.
Lets say I have a Product, which has an FK ImageId, a FK for BrandId, and a FK for CategoryId. The Brands entity has already a FK ImageId for the Image entity.
Now when I try to insert a new product, its inserting 2 images, one which comes with the Brand, and the image for the Product itself. So in the Images Table, I get 2 entries, when I only want 1 new entry for the Product Image. Also, this is causing a problem when I want to use an existing image for a new product.
So I was thinking of creating a new Service/Repository for the Product to Inherit from the Generic Service/Repository, and add some more logic to it. However is there a better way to do this?
Thanks for your help and time


ImagetoBrandand toProduct, and the call to yourAddmethod?