Show / Hide Table of Contents

Class UnitOfWorkBase<TContext>

Base class that implements the interface with the methods responsible for operations outside of the DbContext.

Unit of Work is a design pattern to resolution of concurrency problems and coordinates the writing out of changes.

Inheritance
System.Object
UnitOfWorkBase<TContext>
Implements
IUnitOfWork<TContext>
System.IDisposable
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ToString()
System.Object.ReferenceEquals(System.Object, System.Object)
Namespace: StoneCo.Framework.Data.SqlServer
Assembly: cs.temp.dll.dll
Syntax
public abstract class UnitOfWorkBase<TContext> : IUnitOfWork<TContext>, IDisposable where TContext : IDbContext
Type Parameters
Name Description
TContext

The type of context being operated on by this unit of work.

Constructors

UnitOfWorkBase(TContext)

Default constructor that set the context operated on by this unit of work.

Declaration
protected UnitOfWorkBase(TContext context)
Parameters
Type Name Description
TContext context

Instance of the context operated on by this unit of work.

Examples

note

This is an initial configuration that should be used as a basis for the following examples.

Here we have the interface for the Foo context.

public interface IDbContextFoo : IDbContext
{
}

Class that defines the Foo context by implementing the IDbContextFoo interface that was previously created.

public class DbContextFoo : DbContextBase, IDbContextFoo
{
    public DbContextFoo(DbContextOptions dbContextOptions)
        : base(dbContextOptions)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

Here we have the interface for the UnitOfWork.

public interface IUnitOfWork : IUnitOfWork<IDbContextFoo>
{
}

Class that defines the UnitOfWork by implementing the IUnitOfWork interface that was previously created.

public class UnitOfWork : UnitOfWorkBase<IDbContextFoo>, IUnitOfWork
{
    public UnitOfWork(DbContextFoo context)
        : base(context)
    {
    }
}

For a working example, you need to create the entire dependency injection and configuration part using the Options design pattern.

note

This part of the example is not required to use the Framework.

Configuration file appsettings.json with the connection string.

{
  "ConnectionStrings": {
    "DefaultConnectionConfiguration": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;persistsecurityinfo=True;"
  }
}

Loading the information from the configuration file above.

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", false)
    .Build();

Loading information for dependency injection.

var services = new ServiceCollection();

services
    .AddScoped<IDbContextFoo, DbContextFoo>()
    .AddScoped<IUnitOfWork, UnitOfWork>()
    .AddDbContext<DbContextFoo>(options => 
        options.UseSqlServer(configuration.GetConnectionString("DefaultConnectionConfiguration")));

Methods

BeginTransaction()

Starts a new transaction.

Declaration
public IDbContextTransaction BeginTransaction()
Returns
Type Description
IDbContextTransaction

A transaction against the database.

Examples

Bar class used in this example.

public class Bar
{
    private IUnitOfWork UnitOfWork { get; }

    public Bar(IUnitOfWork unitOfWork) => UnitOfWork = unitOfWork;

    public async Task SampleBeginTransaction()
    {
        using (var transaction = UnitOfWork.BeginTransaction())
        {
            transaction.Commit();
        }

    }
}

Dispose()

Dispose the context.

Declaration
public void Dispose()

Dispose(Boolean)

Dispose the context.

Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type Name Description
System.Boolean disposing

True to dispose the context, otherwise, false.

SaveChanges()

Synchronous method that confirms the modifications made to entities within the context.

Declaration
public virtual int SaveChanges()
Returns
Type Description
System.Int32

The number of state entries written to the database.

SaveChangesAsync(CancellationToken)

Asynchronous method that confirms the modifications made to entities within the context.

Declaration
public virtual Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type Name Description
System.Threading.CancellationToken cancellationToken

Spreads the notification that transactions should be canceled.

Returns
Type Description
System.Threading.Tasks.Task<System.Int32>

A task that represents the asynchronous save operation. The task result contains the number of state entries written to the database.

SetTimeOut(Int32)

Set timeout to execute the commands into context instance.

Declaration
public void SetTimeOut(int timeoutInSeconds = 180)
Parameters
Type Name Description
System.Int32 timeoutInSeconds

Value that represent in seconds the timeout.

Examples

Bar class used in this example.

public class Bar
{
    private IUnitOfWork UnitOfWork { get; }

    public Bar(IUnitOfWork unitOfWork) => UnitOfWork = unitOfWork;

    public void SetTimeOut()
    {
        var timeoutInSeconds = 10;

        UnitOfWork.SetTimeOut(timeoutInSeconds);
    }
}

Implements

IUnitOfWork<TContext>
System.IDisposable
Back to top Generated by DocFX