This repository is deprecated. Please use: https://github.com/Citolab/persistence
This is a repository library that can be configured to use MongoDB or an in-memory database (using MemoryCache). There is also experimental support for SQL Server.
Packages can be installed using NuGet:
- Install-Package Citolab.Repository (in-memory)
- Install-Package Citolab.Repository.Mongo (MongoDB)
var services = new ServiceCollection();
services.AddInMemoryRepository();
var services = new ServiceCollection();
services.AddMongoRepository("MyDatabase", Configuration.GetConnectionString("MongoDB"));
An entity that must be stored in the database should inherit from Model.
In the example below a Web API controller that uses all methods:
public class UserController : Controller
{
private readonly IRepositoryFactory _repositoryFactory;
public UserController(IRepositoryFactory repositoryFactory) =>
_repositoryFactory = repositoryFactory;
[HttpGet("{id}")]
public Task<User> Get(Guid id) =>
_repositoryFactory.GetRepository<User>().GetAsync(id);
[HttpGet]
public Task<IEnumerable<User>> Get() =>
Task.Run(() => _repositoryFactory.GetRepository<User>().AsQueryable().AsEnumerable());
[HttpPost]
public Task<User> Post([FromBody] User user) =>
_repositoryFactory.GetRepository<User>().AddAsync(user);
[HttpPut]
public Task<bool> Update([FromBody] User user) =>
_repositoryFactory.GetRepository<User>().UpdateAsync(user);
[HttpDelete("{id}")]
public Task<bool> Delete(Guid id) =>
_repositoryFactory.GetRepository<User>().DeleteAsync(id);
}
Created and LastModified are filled by default (existing values will be overwritten). CreatedByUserId and LastModifiedByUserId are provided by a registered instance of ILoggedInUserProvider. By default this won't fill a userId. You can write your own instance of ILoggedInUserProvider and register it before calling .AddRepository.
If you want to set the Created, LastModified, CreatedByUserId and LastModifiedByUserId then you can create an instance of the OverrideDefaultValues class.
using (new OverrideDefaultValues()) {
// your code here
}
Caching can be added using Mongo or SQL Server as database too. Adding the caching attribute above your model classes; these entities will be kept in the MemoryCache. The cache will be updated when entities are changed.
[Cache(300)]
public class User : Model
{
//properties
}