-
Notifications
You must be signed in to change notification settings - Fork 1
BlayerContext class
- Creates a new context using repositories from the informed configuration. If
repositoriesis informed, they are concatenated into the config ones. - Parameters:
-
RepositoryConfiguration config- Repository configuration -
IRepository[] repositories- Extra repositories to be loaded
-
Usage:
Repository configuration class:
public class AppConfiguration : RepositoryConfiguration
{
public StateRepository StateRepository;
public CountryRepository CountryRepository;
public CityRepository CityRepository;
}Business logic layer:
using (var ctx = new AppContext(new AppConfiguration()))
{
return ctx.GetRepository<Country>().GetAll();
}Using extra repositories:
using (var ctx = new AppContext(new AppConfiguration(), new DistrictRepository()))
{
return ctx.GetRepository<District>().GetAll();
}- Retrieves a repository representing the database entity of the informed type.
- Type parameter:
-
T : EntityBase- Repository base class type
-
Usage:
using (var ctx = new AppContext(new AppConfiguration()))
{
var countryRepository = ctx.GetRepository<Country>();
return countryRepository.GetAll();
}- Retrieves a repository casting to a specific type. Using it this way we're able to use repository specific methods.
- Type parameters:
-
T : EntityBase- Repository base class type (your POCO class) -
TReturn : Repository<T>- Repository type
-
Usage:
Repository class:
public class CountryRepository : Repository<Country>
{
public Country GetByCountryCode(string countryCode)
{
return GetAll().FirstOrDefault(c => c.CountryCode == countryCode);
}
}Usage:
using (var ctx = new AppContext(new AppConfiguration()))
{
return ctx.GetRepository<Country, CountryRepository>()
.GetByCountryCode("USA"); // repository specific method
}T Add<T>(T entity)T Add<T>()T Update<T>(T entity, string[] changedProperties)void Delete<T>(T entity)void DeleteWhere<T>(Expression<Func<T, bool>> where)
- Adds a new entity to the context with the provided data
- Type parameter:
-
T : EntityBase- Type of the entity
-
- Parameters:
-
T entity- Entity's data
-
Usage:
using (var ctx = new AppContext(new AppConfiguration()))
{
var account = ctx.Add(new Account
{
CreationDate = DateTime.Now,
Name = "Super account"
});
ctx.Save();
}- Adds a new, empty entity to the context
- Type parameter:
-
T : EntityBase- Type of the entity
-
Usage:
using (var ctx = new AppContext(new AppConfiguration()))
{
var account = ctx.Add<Account>();
account.CreationDate = DateTime.Now;
account.Name = "Super account";
ctx.Save();
}- Updates an entity
- Type parameter:
-
T : EntityBase- Type of the entity
-
- Parameters:
-
T entity- Entity's data -
string[] changedProperties- Properties changed
-
Usage:
using (var ctx = new AppContext(new AppConfiguration()))
{
var account = ctx.Update(new Account
{
AccountId = 15,
LastAction = DateTime.Now, // should be done using `IAdditionalStep`. Here only for learning purposes
Name = "Super mega account"
}, new[] { "LastAction", "Name" });
ctx.Save();
}- Marks an entity for removal
- Type parameter:
-
T : EntityBase- Type of the entity
-
- Parameters:
-
T entity- Entity's data
-
Usage:
using (var ctx = new AppContext(new AppConfiguration()))
{
var account = ctx.GetRepository<Account>().GetById(id);
ctx.Delete(account);
ctx.Save();
}- Marks a group of entities for removal
- Type parameter:
-
T : EntityBase- Type of the entity
-
- Parameters:
-
Expression<Func<T, bool>> where- Expression to filter data to be removed
-
Usage:
using (var ctx = new AppContext(new AppConfiguration()))
{
var ids = new [] { 15, 22, 351, 11 };
ctx.DeleteWhere<Account>(a => ids.Contains(a.AccountId));
ctx.DeleteWhere<Account>(a => a.CreationDate < DateTime.Now);
ctx.Save();
}- Saves the attached entities
- Parameters:
-
bool disposeAfter- If true, maintains the entities in memory for further access, else removes context from memory
-
If you need to access data from the saved entity/record, make sure you set disposeAfter to false. Otherwise the object will have been disposed after the Save call. (see examples below)
Usage:
See Performing CRUD operations for several examples
- Removes the context from memory
Usage:
Usually you won't need to use this method, since most people use the using context wrapper. But if you are creating your custom context handler, than you'll need it.
- Logged user. There's not secret here, a simple property in which we inform the logged user, if any.
Usage:
using (var ctx = new AppContext(new AppConfiguration()))
{
ctx.LoggedUser = ctx.GetRepository<User, UserRepository>()
.GetByUsername("mr_user"); // repository specific method
ctx.DeleteWhere<Account>(a => ids.Contains(a.AccountId));
ctx.Save();
}Even though this works fine, if you need to set the logged user most of the times, I'd recommend creating a wrapper class that handles the context's creation and disposal and there you set the logged user.