UnitOfWork
Namespace: Babel.Data Assembly: Babel.Data.dll
A UnitOfWork instance represents a combination Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit.
public class UnitOfWork : IDisposable
Inheritance
Implements
Inherited Members
object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()
Constructors
UnitOfWork()
Initializes a new instance of the UnitOfWork class. The Configure(Action<DbContextOptionsBuilder> config) method will be called to allow further configuration of the options.
public UnitOfWork()
UnitOfWork(BabelDbContext)
Initializes a new instance of the UnitOfWork class.
public UnitOfWork(BabelDbContext context)
Parameters
context
BabelDbContext
The Babel database context.
Exceptions
UnitOfWork(string)
Initializes a new instance of the UnitOfWork class.
public UnitOfWork(string connectionString)
Parameters
connectionString
string
The database connection string.
Exceptions
UnitOfWork(DbContextOptions<BabelDbContext>)
Initializes a new instance of the UnitOfWork class using the specified options. The Configure(Action<DbContextOptionsBuilder> config) method will still be called to allow further configuration of the options.
public UnitOfWork(DbContextOptions<BabelDbContext> options)
Parameters
options
DbContextOptions<BabelDbContext>
The options for this unit of work context.
Properties
ApiKeyRepository
Gets the API key repository.
public IApiKeyRepository ApiKeyRepository { get; }
Property Value
AssemblyRepository
Gets the Assembly repository.
public IAssemblyRepository AssemblyRepository { get; }
Property Value
ContactRepository
Gets the contact repository.
public IContactRepository ContactRepository { get; }
Property Value
Context
Gets the Babel database context.
public BabelDbContext Context { get; }
Property Value
CustomerRepository
Gets the customer repository.
public ICustomerRepository CustomerRepository { get; }
Property Value
IsDisposed
Gets a value indicating whether this object is disposed.
public bool IsDisposed { get; }
Property Value
LicenseRepository
Gets the license repository.
public ILicenseRepository LicenseRepository { get; }
Property Value
LicenseTemplateRepository
Gets the license template repository.
public ILicenseTemplateRepository LicenseTemplateRepository { get; }
Property Value
LicenseTokenRepository
Gets the license token repository.
public ILicenseTokenRepository LicenseTokenRepository { get; }
Property Value
LicenseTraceRepository
Gets the license trace repository.
public ILicenseTraceRepository LicenseTraceRepository { get; }
Property Value
LogEntryRepository
Gets the log repository.
public ILogEntryRepository LogEntryRepository { get; }
Property Value
OrderRepository
Gets the order repository.
public IOrderRepository OrderRepository { get; }
Property Value
ProductRepository
Gets the product repository.
public IProductRepository ProductRepository { get; }
Property Value
ReleaseRepository
Gets the release repository.
public IReleaseRepository ReleaseRepository { get; }
Property Value
ReportPropertyRepository
Gets the report property repository.
public IReportPropertyRepository ReportPropertyRepository { get; }
Property Value
ReportRepository
Gets the report repository.
public IReportRepository ReportRepository { get; }
Property Value
ResourceRepository
Gets the resource repository.
public IResourceRepository ResourceRepository { get; }
Property Value
ScriptRepository
Gets the script repository.
public IScriptRepository ScriptRepository { get; }
Property Value
ScriptTagRepository
Gets the script tag repository.
public IScriptTagRepository ScriptTagRepository { get; }
Property Value
UserRepository
Gets the user repository.
public IUserRepository UserRepository { get; }
Property Value
Methods
Configure(Action<DbContextOptionsBuilder>)
Configure the database (and other options) to be used for this unit of work. This method is called for each instance of the unit of work that is created.
public static void Configure(Action<DbContextOptionsBuilder> config)
Parameters
config
Action<DbContextOptionsBuilder>
A callback that provide a builder object instance used to create or modify options for this context.
Examples
using Babel.Data;
// SQL Server
UnitOfWork.Configure(options => {
string connection = "Server=127.0.0.1;User=root;Database=licenses;Password=<database password>";
options.UseSqlServer(connection, x => x.MigrationsAssembly("Babel.Data.SQLServer"));
});
// MySQL
UnitOfWork.Configure(uwc => {
string connection = "server=localhost;database=licenses;user=root;password=<database password>;"
options.UseMySql(connection, ServerVersion.AutoDetect(connection),
x => x.MigrationsAssembly("Babel.Data.MySQL"));
});
// SQLite
UnitOfWork.Configure(uwc => {
string connection = "data source=<full path to licenses.db database>";
options.UseSqlite(connection, x => x.MigrationsAssembly("Babel.Data.SQLite"));
});
CreateRepository<T>()
Creates a new instance of a generic repository for the specified entity type.
protected virtual IGenericRepository<T> CreateRepository<T>() where T : EntityTracker
Returns
A new instance of a generic repository for the specified entity type.
Type Parameters
T
The type of entity for which to create a repository.
Dispose(bool)
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
protected virtual void Dispose(bool disposing)
Parameters
disposing
bool
True to release both managed and unmanaged resources; false to release only unmanaged resources.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
~UnitOfWork()
Finaliser.
protected ~UnitOfWork()
Migrate()
Applies any pending migrations for the context to the database. Will create the database if it does not already exist.
public void Migrate()
Save()
Saves all changes made in this context to the underlying database.
public virtual bool Save()
Returns
True if it succeeds, false if it fails.
SaveAsync()
Asynchronously saves all changes made in this context to the underlying database.
public virtual Task<bool> SaveAsync()
Returns
Last updated