ConnectionPersistency
A enumeration that defines the persistency of the database connection object when working with repository.
This enum defines the persistency of the DbConnection instance within the BaseRepository and DbRepository objects.
This enumeration is useful when fine-grained control over how a repository instantiates its connection object is required.
Enum Values
| Name | Description |
|---|---|
| PerCall | In every method call, a new DbConnection object is being used. This is the default setting. |
| Instance | A single instance of DbConnection object is used all throughout the lifespan of the repository. |
Usability
Pass the connection persistency value in the constructor when instantiating a repository object.
The following example creates a PersonRepository that inherits BaseRepository and shares a single DbConnection instance across all calls.
// Repository
public class PersonRepository : BaseRepository<Person, SqlConnection>
{
public PersonRepository(string connectionString)
: base(connectionString, ConnectionPersistency.Instance)
{ }
...
}
// Instantiation
using (var repository = new PersonRepository(connectionString))
{
...
}
The same applies to a DbRepository object.
// Repository
public class PersonRepository : DbRepository<SqlConnection>
{
public PersonRepository(string connectionString)
: base(connectionString, ConnectionPersistency.Instance)
{ }
...
}
// Instantiation
using (var repository = new PersonRepository(connectionString))
{
...
}