Using UpdateDefinition for MongoDB Generic Repository

Using UpdateDefinition for MongoDB Generic Repository

I’m trying to create a generic repository for mongodb, and followinf this example : https://github.com/alexandre-spieser/mongodb-generic-repository
This is what I have:
Generic Interface:
public interface IGenericRepository where TEntity : class, new()
{
///

/// Generic Get One Async method
///

/// TEntity
/// ///
Task> GetOneAsync(Guid id);
}

Generic Implemendation
public class GenericRepository : IGenericRepository where TEntity : class, new()
{
private readonly StorageContext _context = null;

public GenericRepository(StorageContext context)
{
_context = context;
}

public async Task UpdateOneAsync(Guid id, UpdateDefinition update)
{
var filter = new FilterDefinitionBuilder().Eq(“Id”, id);
return await UpdateOneAsync(filter, update);
}

public async Task UpdateOneAsync(FilterDefinition filter, UpdateDefinition update)
{
var result = new Result();
try
{
var collection = GetCollection();
var updateRes = await collection.UpdateOneAsync(filter, update);
if (updateRes.ModifiedCount < 1) { var ex = new Exception(); result.Message = ex.ToString(); return result; } result.Success = true; result.Message = "OK"; return result; } catch (Exception ex) { result.Message = ex.ToString(); return result; } } } Inherit repository public class FileRepository : GenericRepository, IFileRepository
{
private readonly StorageContext _context = null;
public FileRepository(StorageContext context) : base(context)
{
_context = context;
}
}

As you can see in the implementation of the generic repository there is a parameter UpdateDefinition update. Shouldn’t this parameter be automatically set by my FileRepository so that in my service i do not have to set it?
If that is not possible, how do I call this in my service ?
await _fileRepository.UpdateOneAsync(fileId, ???);

Solutions/Answers:

Solution 1:

Should work like this:

var update = Builders<BsonDocument>.Update
    .Set("cuisine", "American (New)")
    .CurrentDate("lastModified");
var result = await _fileRepository.UpdateOneAsync(fileId, update);

References

Related:  How do I $concat fields in an array of objects?