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

objectUnitOfWork

Implements

IDisposable

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

NameDescription

The Babel database context.

Exceptions

NameDescription

UnitOfWork(string)

Initializes a new instance of the UnitOfWork class.

public UnitOfWork(string connectionString)

Parameters

NameDescription

connectionString string

The database connection string.

Exceptions

NameDescription

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

NameDescription

The options for this unit of work context.

Properties

ApiKeyRepository

Gets the API key repository.

public IApiKeyRepository ApiKeyRepository { get; }

Property Value

IApiKeyRepository

AssemblyRepository

Gets the Assembly repository.

public IAssemblyRepository AssemblyRepository { get; }

Property Value

IAssemblyRepository

ContactRepository

Gets the contact repository.

public IGenericRepository<Contact> ContactRepository { get; }

Property Value

IGenericRepository<Contact>

Context

Gets the Babel database context.

public BabelDbContext Context { get; }

Property Value

BabelDbContext

CustomerRepository

Gets the customer repository.

public ICustomerRepository CustomerRepository { get; }

Property Value

ICustomerRepository

IsDisposed

Gets a value indicating whether this object is disposed.

public bool IsDisposed { get; }

Property Value

bool

LicenseRepository

Gets the license repository.

public ILicenseRepository LicenseRepository { get; }

Property Value

ILicenseRepository

LicenseTemplateRepository

Gets the license template repository.

public ILicenseTemplateRepository LicenseTemplateRepository { get; }

Property Value

ILicenseTemplateRepository

LicenseTokenRepository

Gets the license token repository.

public ILicenseTokenRepository LicenseTokenRepository { get; }

Property Value

ILicenseTokenRepository

LogEntryRepository

Gets the log repository.

public ILogEntryRepository LogEntryRepository { get; }

Property Value

ILogEntryRepository

OrderRepository

Gets the order repository.

public IOrderRepository OrderRepository { get; }

Property Value

IOrderRepository

ProductRepository

Gets the product repository.

public IProductRepository ProductRepository { get; }

Property Value

IProductRepository

ReleaseRepository

Gets the release repository.

public IReleaseRepository ReleaseRepository { get; }

Property Value

IReleaseRepository

ReportPropertyRepository

Gets the report property repository.

public IReportPropertyRepository ReportPropertyRepository { get; }

Property Value

IReportPropertyRepository

ReportRepository

Gets the report repository.

public IReportRepository ReportRepository { get; }

Property Value

IReportRepository

ResourceRepository

Gets the resource repository.

public IResourceRepository ResourceRepository { get; }

Property Value

IResourceRepository

ScriptRepository

Gets the script repository.

public IScriptRepository ScriptRepository { get; }

Property Value

IScriptRepository

ScriptTagRepository

Gets the script tag repository.

public IScriptTagRepository ScriptTagRepository { get; }

Property Value

IScriptTagRepository

UserRepository

Gets the user repository.

public IUserRepository UserRepository { get; }

Property Value

IUserRepository

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

NameDescription

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

NameDescription

A new instance of a generic repository for the specified entity type.

Type Parameters

NameDescription

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

NameDescription

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

NameDescription

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

NameDescription

A task that represents the asynchronous save operation.

Last updated