Link Search Menu Expand Document

ConnectionPersistency


This is the enumeration used to define the persistency of the DbConnection instance within the BaseRepository and DbRepository objects.

This enumueration is very useful when you are on a situation that you do need to control the way how repository instantiate a connection object.

Enum Values

Below is the list of enum values.

NameDescription
PerCallIn every method call, a new DbConnection object is being used. This is the default setting.
InstanceA single instance of DbConnection object is used all throughout the lifespan of the repository.

Usability

Simply pass the connection persistency value in the constructor when instantiating a repository object.

Let us say, you create a repository object named Person that inherits the BaseRepository and you would like to share a single DbConnection object in all calls.

// Repository
public class PersonRepository : BaseRepository<Person, SqlConnection>
{
    public PersonRepository(string connectionString)
        : base(connectionString, ConnectionPersistency.Instance)
    { }

    ...
}

// Instantiation
using (var repository = new PersonRepository(connectionString))
{
    ...
}

Or for DbRepository object.

// Repository
public class PersonRepository : DbRepository<SqlConnection>
{
    public PersonRepository(string connectionString)
        : base(connectionString, ConnectionPersistency.Instance)
    { }

    ...
}

// Instantiation
using (var repository = new PersonRepository(connectionString))
{
    ...
}