Repository pattern for MongoDB with extended features
You don't need to create a model, but if you are doing so you need to extend Entity
// If you are able to define your model
public class User : Entity
{
public string Username { get; set; }
public string Password { get; set; }
}
There are multiple base constructors, read summaries of others
public class UserRepository : Repository<User>
{
public UserRepository(string connectionString) : base(connectionString) { }
// Custom method
public User FindByUsername(string username)
{
return First(i => i.Username == username);
}
// Custom method2
public void UpdatePassword(User item, string newPassword)
{
repo.Update(item, i => i.Password, newPassword);
}
// Custom async method
public async Task<User> FindByUsernameAsync(string username)
{
return awaitFirstAsync(i => i.Username == username);
}
}
If you want to create a repository for already defined non-entity model
public class UserRepository : Repository<Entity<User>>
{
public UserRepository(string connectionString) : base(connectionString) { }
// Custom method
public User FindByUsername(string username)
{
return First(i => i.Content.Username == username);
}
}
Each method has multiple overloads, read method summary for additional parameters
UserRepository repo = new UserRepository("mongodb://localhost/sample")
// Get
User user = repo.Get("58a18d16bc1e253bb80a67c9");
// Insert
User item = new User()
{
Username = "username",
Password = "password"
};
repo.Insert(item);
// Update
// Single property
repo.Update(item, i => i.Username, "newUsername");
// Multiple property
// Updater has many methods like Inc, Push, CurrentDate, etc.
var update1 = Updater.Set(i => i.Username, "oldUsername");
var update2 = Updater.Set(i => i.Password, "newPassword");
repo.Update(item, update1, update2);
// All entity
item.Username = "someUsername";
repo.Replace(item);
// Delete
repo.Delete(item);
// Queries - all queries has filter, order and paging features
var first = repo.First();
var last = repo.Last();
var search = repo.Find(i => i.Username == "username");
var allItems = repo.FindAll();
// Utils
var any = repo.Any(i => i.Username.Contains("user"));
// Count
// Get number of filtered documents
var count = repo.Count(p => p.Age > 20);
// EstimatedCount
// Get number of all documents
var count = repo.EstimatedCount();