Delete multiple row with ids without foreach and postman test - entity-framework

I have a little problem. But I dont know why it doesnt work. And I dont know how to post all ids by postman.
I am using unit of work with generic repository. I want to send int[] ids to my controller. I dont want to send entity. I searched a lot it today. And I changed my code. But what is problem now?
This is my repostiroy:
public async Task DeleteRangeAsync(Expression<Func<T, bool>> predicate)
{
IQueryable<T> query = _dbSet.Where(predicate);
await Task.Run(() => { _dbSet.RemoveRange(query.AsNoTracking()); });
}
This is my KulturManager:
public async Task<IResult> HardDeleteRangeAsync(int[] ids)
{
await UnitOfWork.Kulturs.DeleteRangeAsync(c => ids.Contains(c.Id));
await UnitOfWork.SaveAsync();
return new Result(ResultStatus.Success, Messages.Info("Kultur", "HardDelete"));
}
And this is my KulturController:
[HttpDelete("{ids}")]
public async Task<IActionResult> HardDeleteRangeAsync(int[] ids)
{
var result = await _kulturManager.HardDeleteRangeAsync(ids);
return Ok(result.Message);
}
Thank you for help

You shouldn't fetch all the entities you want to delete. Instead create stub entities for RemoveRange. If you don't have a common base class, this requires reflection, but with a common entity base class you can do it like this:
public void DeleteRange<T>(int[] ids) where T: BaseEntity, new()
{
_dbSet.RemoveRange(ids.Select(i => new T() { Id = i }).ToList());
}
or if the method is in a generic class, the method would look like
public void DeleteRange(int[] ids)
{
_dbSet.RemoveRange(ids.Select(i => new T() { Id = i }).ToList());
}
And there's no reason to mark this as Async now since it doesn't do any database access.

Related

How to mock a MongoDB collection of type IMongoCollection<T> to return some predefined data?

Below is my code:
Controller/Action:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(CustomerViewModel model, string returnUrl = null)
{
try
{
ViewData["ReturnUrl"] = returnUrl;
// when debugging the test, _dbContext.Customers throws exception
CustomerDoc existingCustomer = await _dbContext.Customers.Find(o => o.email == model.email).FirstOrDefaultAsync();
if (existingCustomer != null)
{
ModelState.AddModelError("Email", "email already used.");
}
// other checkings
if (!ModelState.IsValid)
{
return View(model);
}
// if model state is valid, do something here
}
catch (Exception ex)
{
return View(model);
}
return View(model);
}
And my unit test code is:
[Fact]
public async Task should_return_view_with_errors_when_email_already_exists()
{
IEnumerable<CustomerDoc> customers = new List<CustomerDoc>
{
new CustomerDoc
{
email = "test#test.com"
}
};
_dbContextMock.SetupAllProperties();
// below line is causing the error
_dbContextMock.Setup(c => c.Customers).Returns(() =>(IMongoCollection<CustomerDoc>)customers);
CustomerViewModel model = new CustomerViewModel
{
email = "test#test.com"
};
CreateController();
var result = await _controller.Register(model);
Assert.IsType<ViewResult>(result);
Assert.False(_controller.ModelState.IsValid);
Assert.True(_controller.ModelState.ContainsKey("Email"));
}
As you can see in my unit test code comment, I am trying to mock a IMongoCollection to return some data. But I am not able to do so because _dbContext.Customers is throwing exception.
How can I mock IMongoCollection to return some predefined data?
I am using
asp.net core 2.1.0
mongodb driver 2.7.0
You declare customers as a List:
IEnumerable<CustomerDoc> customers = new List<CustomerDoc>
but then try to cast it to IMongoCollection
() =>(IMongoCollection<CustomerDoc>)customers
There's two immediate directions (but both have further issues to deal with):
1) Just return the list without the cast
() => customers
but I can't see the type of c.Customers so I suspect this will just move the issue. I'll take a guess that it's IMongoCollection<CustomerDoc> which is why you're trying to do the cast in the first place? This is problematic as the .Returns would need to be be associated with a function performing the equivalent of c.Customer.Find(). Even so, it's probably better than the alternative.
2) Changing the customers variable to a type that implements IMongoCollection.
Option 1 feels like the way to go as option 2 forces you to start dealing with lots of logic that really shouldn't be relevant to this piece of code.

NET Core MongoDb Update/Replace exclude fields

I'm trying to complete a general repository for all of the entities in my application. I Have a BaseEntity with property Id, CreatorId and LastModifiedUserId. Now I'd like to Update a record in a collection, without having to modify the field CreatorId, so I have (from the client) an Entity valorized with some fields updated that I want to update.
Hi have 2 ways:
UpdateOneAsync
ReplaceOneAsync
The repo is created like this:
public class BaseRepository<T> : IRepository<T> where T : BaseEntity
{
public async Task<T> Replace/Update(T entity){...}
}
So it's very hard to use Update(1), since I should retrieve with reflection all the fields of T and exclude the ones that I don't want to update.
With Replace(2) I cannot find a way to specify which fields i should exclude when replacing an object with another. Projectionproperty in FindOneAndReplaceOptions<T>() just excludes the fields on the document that is returned after the update.
Am I missing a way in the replace method to exclude the fields or should I try to retrieve the fields with reflection and use a Update?
I don't know if this solution is ok for you .. what i do is :
Declare in Base Repo a method like
public virtual bool Update(TEntity entity, string key)
{
var result = _collection.ReplaceOne(x => x.Id.Equals(key), entity, new UpdateOptions
{
IsUpsert = false
});
return result.IsAcknowledged;
}
then in your controller when you want to update your entities is there where you set the prop you want to change .. like:
[HttpPut]
[ProducesResponseType(typeof(OrderDTO), 200)]
[ProducesResponseType(400)]
public async Task<ActionResult<bool>> Put([FromBody] OrderDTO value)
{
try
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var orderOnDb = await _orderService.FindAsync(xx => xx.Id == value.Id);
if (orderOnDb == null) return BadRequest(Constants.Error.NOT_FOUND_ON_MONGO);
// SET PROPERTY TO UPDATE (MANUALLY)
orderOnDb.LastUpdateDate = DateTime.Now;
orderOnDb.PaymentMethod = value.PaymentMethod;
orderOnDb.StateHistory = value.StateHistory;
//Save on db
var res = await _orderRepo.UpdateAsync(orderOnDb, orderOnDb.Id);
return res;
}
catch (Exception ex)
{
_logger.LogCritical(ex, ex.Message);
throw ex;
}
}
Hope it helps you!!!

Re-using reference to an IReliableCollection

Is it acceptable to re-use a reference to a IReliableCollection or should I request from IReliableStateManager every time I want to use it?
For example, if I have a dictionary that is widely used in my application, is it acceptable to retrieve it once in the RunAsync method and then pass this reference to any method that requires it, e.g:
protected override async Task RunAsync(CancellationToken cancellationToken)
{
_someCollection = StateManager.GetOrAddAsync<IReliableDictionary<int, string>>(
"SomeName");
}
public async Task DoSomething(int id, string message)
{
_someClass.DoSomething(_someCollection, id, message);
}
And then use in a class like so:
public class SomeClass
{
public void DoSomething(IReliableDictionary<int, string> dict, int id, string msg)
{
using (ITransaction tx = StateManager.CreateTransaction())
{
await dict.AddAsync(tx, id, msg);
await tx.CommitAsync();
}
}
}
Or should I request from IReliableStateManager on each call, e.g,
public class SomeClass
{
public void DoSomething(int id, string msg)
{
var dict = StateManager.GetOrAddAsync<IReliableDictionary<int, string>>("SomeName");
using (ITransaction tx = StateManager.CreateTransaction())
{
await dict.AddAsync(tx, id, msg);
await tx.CommitAsync();
}
}
}
Passing a reference seems to work fine from what I can tell but I'm not sure whether this would be considered bad practice and I can't find a definitive answer in the guidelines or documentation
Yes. You can.
You can check this to see how you can even receive notifications when new instance of IReliableState is added to ReliableStateManager.
You can also subscribe for events (if this is a dictionary).

Eager Loading of Navigation Properties

In my repository I'm trying to use eager loading to load related entities. Not sure why but it seems like when I return all instances of a particular entity, related entities are returned, but when I limit the results returned the related entities are not included in the results.
This code in the service layer is returning all orders, including related Customer, OrderItem, and Product entities:
public async Task<IEnumerable<Order>> GetOrdersAsync()
{
return await _repository.GetAsync(null, q => q.OrderByDescending(p => p.CreatedDate), "Customer", "OrderItems", "OrderItems.Product");
}
In the repository:
public async Task<IEnumerable<Order>> GetAsync(Expression<Func<Order, bool>> where = null, Func<IQueryable<Order>, IOrderedQueryable<Order>> orderBy = null, params string[] navigationProperties)
{
IQueryable<Order> query = _context.Set<Order>();
if (where != null)
{
query = query.Where(where);
}
//Apply eager loading
foreach (string navigationProperty in navigationProperties)
query = query.Include(navigationProperty);
if (orderBy != null)
{
return await orderBy(query).ToListAsync();
}
else
{
return await query.ToListAsync();
}
}
This code in the service layer is getting an order by id, but for whatever reason is not returning related Customer, OrderItem, and Product entities:
public async Task<Order> GetOrderByIdAsync(long id)
{
return await _repository.GetByIdAsync(id, "Customer", "OrderItems", "OrderItems.Product");
}
In the repository:
public async Task<Order> GetByIdAsync(long id, params string[] navigationProperties)
{
DbSet<Order> dbSet = _context.Set<Order>();
foreach (string navigationProperty in navigationProperties)
dbSet.Include(navigationProperty);
return await dbSet.FindAsync(id);
}
The one difference I see between the two repository methods is one is casting the _context.Set() to IQueryable before including navigation properties, while the other is calling Include directly on the DbSet itself. Should this matter?
So the repository method that was not working was using DBSet.Include incorrectly. Include is actually a method on the DBQuery class, which DBSet inherits from. I needed to use DBQuery.Include to replace the query with a new instance of the query for each navigation property I included. So I changed the implementation to:
public async Task<Order> GetByIdAsync(long id, params string[] navigationProperties)
{
DbQuery<Order> dbQuery = _context.Set<Order>();
foreach (string navigationProperty in navigationProperties)
dbQuery = dbQuery.Include(navigationProperty);
return await dbQuery.Where(o => o.Id == id).FirstOrDefaultAsync();
}

Stack overflow exception while returning an object asynchronously

While using MongoDB C# driver with WebApi I came to the following problem. When I want to read all documents (or even just one) from the database the repo's function will get the correct data but in WebApi the object returned from the repo causes a stack overflow. I suspect that I am doing something wrong with the way the objects are returned.
WebApi where the Repo's method is called:
// GET api/<controller>
public async Task<List<Event>> Get()
{
return await _repo.FindAll();
}
// GET api/<controller>/5
public async Task<Event> Get(string id)
{
Event e = await _repo.FindById(id);
return e;
}
And corresponding methods in the Repo:
public async Task<Event> FindById(string id)
{
Event e = await _collection.Find<Event>(x => x.ID == ObjectId.Parse(id)).FirstAsync();
return e;
}
public async Task<List<Event>> FindAll()
{
var filter = new BsonDocument();
List<Event> list = await _collection.Find(filter).ToListAsync();
return await Task<List<Event>>.FromResult(list);
}
Thanks for all the help in advance!
Edit: I found that when I return string from the function instead of Event the whole thing works.
What I think is making problems is the ID property in the Event.
The problem was that the Event had an ObjecId property. The JSON.Net doesn't know about that type. See the solution here: JSON.NET cast error when serializing Mongo ObjectId