Show / Hide Table of Contents

Class SQLServerRepository<T1, T2>

Repository base class for SQL Server databases. Implements the GlitchedPolygons.RepositoryPattern.IRepository<T1, T2> interface.

Inheritance
object
SQLServerRepository<T1, T2>
Implements
IRepository<T1, T2>
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: GlitchedPolygons.RepositoryPattern.SQLServer
Assembly: GlitchedPolygons.RepositoryPattern.SQLServer.dll
Syntax
public abstract class SQLServerRepository<T1, T2> : IRepository<T1, T2> where T1 : IEntity<T2>
Type Parameters
Name Description
T1

The type of entity stored in the repository. The name of this type should also exactly match the name of the db table!

T2

The type of unique id with which the entities will be identified inside the repo (typically this will be either a string containing a GUID or an auto-incremented int).

Constructors

SQLServerRepository(string, string, string, string)

Creates a new SQL Server repository using a specific connection string.

Declaration
protected SQLServerRepository(string connectionString, string schemaName = null, string tableName = null, string idColumnName = "id")
Parameters
Type Name Description
string connectionString

The SQL Server db connection string. Ensure that this is valid!

string schemaName

Optional SQL Server schema. If left out, the default schema [dbo] is used.

string tableName

Optional custom name for the underlying SQL Server database table. If left out, the entity's type name is used.

string idColumnName

Name of the primary key's column (usually, this will be the default value of id, but you can customize it if needed).

See Also
IRepository<T1, T2>

Properties

this[T2]

Gets an entity by its unique identifier.

Declaration
public T1 this[T2 id] { get; }
Parameters
Type Name Description
T2 id

The entity's unique identifier.

Property Value
Type Description
T1

The first found GlitchedPolygons.RepositoryPattern.IEntity<T>; null if nothing was found.

See Also
IRepository<T1, T2>

SchemaName

The name of the underlying SQL Server schema (default is "public").

Declaration
public string SchemaName { get; }
Property Value
Type Description
string
See Also
IRepository<T1, T2>

TableName

The name of the underlying SQL Server database table where the repository entities are stored.

Declaration
public string TableName { get; }
Property Value
Type Description
string
See Also
IRepository<T1, T2>

Methods

Add(T1)

Adds the specified entity to the data repository.

You need to ENSURE the uniqueness of the addendum GlitchedPolygons.RepositoryPattern.IEntity<T>.Id!
Declaration
public abstract Task<bool> Add(T1 entity)
Parameters
Type Name Description
T1 entity

The entity to add.

Returns
Type Description
Task<bool>

Whether the entity could be added successfully or not.

See Also
IRepository<T1, T2>

AddRange(IEnumerable<T1>)

Adds multiple entities at once.

You need to ENSURE the uniqueness of each added GlitchedPolygons.RepositoryPattern.IEntity<T>.Id!
Declaration
public abstract Task<bool> AddRange(IEnumerable<T1> entities)
Parameters
Type Name Description
IEnumerable<T1> entities

The entities to add.

Returns
Type Description
Task<bool>

Whether the entities were added successfully or not.

See Also
IRepository<T1, T2>

Find(Expression<Func<T1, bool>>)

Finds all entities according to the specified predicate Expression.

WARNING: Can be very slow! For specialized queries, just create a new repository! Derive from this class and create custom SQL queries that return the mapped types that you need.
Declaration
public Task<IEnumerable<T1>> Find(Expression<Func<T1, bool>> predicate)
Parameters
Type Name Description
Expression<Func<T1, bool>> predicate

The search predicate (all entities that match the provided conditions will be added to the query's result).

Returns
Type Description
Task<IEnumerable<T1>>

The found entities (IEnumerable<T>).

See Also
IRepository<T1, T2>

Get(T2)

Gets an entity by its unique identifier.

Declaration
public Task<T1> Get(T2 id)
Parameters
Type Name Description
T2 id

The entity's unique identifier.

Returns
Type Description
Task<T1>

The first found GlitchedPolygons.RepositoryPattern.IEntity<T>; null if nothing was found.

See Also
IRepository<T1, T2>

GetAll()

Gets all entities from the repository.

Declaration
public Task<IEnumerable<T1>> GetAll()
Returns
Type Description
Task<IEnumerable<T1>>

All entities inside the repo.

See Also
IRepository<T1, T2>

OpenConnection()

Opens a IDbConnection to the SQL Server database.

Does not dispose automatically: make sure to wrap your usage into a using block.
Declaration
protected IDbConnection OpenConnection()
Returns
Type Description
IDbConnection

The opened IDbConnection (remember to dispose of it asap after you're done!).

See Also
IRepository<T1, T2>

Remove(T1)

Removes the specified entity.

Declaration
public Task<bool> Remove(T1 entity)
Parameters
Type Name Description
T1 entity

The entity to remove.

Returns
Type Description
Task<bool>

Whether the entity could be removed successfully or not.

See Also
IRepository<T1, T2>

Remove(T2)

Removes the specified entity.

Declaration
public Task<bool> Remove(T2 id)
Parameters
Type Name Description
T2 id

The unique id of the entity to remove.

Returns
Type Description
Task<bool>

Whether the entity could be removed successfully or not.

See Also
IRepository<T1, T2>

RemoveAll()

Removes all entities at once from the repository.

Declaration
public Task<bool> RemoveAll()
Returns
Type Description
Task<bool>

Whether the entities were removed successfully or not. If the repository was already empty, false is returned (because nothing was actually <<removed>> ).

See Also
IRepository<T1, T2>

RemoveRange(IEnumerable<T1>)

Removes the range of entities from the repository.

Declaration
public Task<bool> RemoveRange(IEnumerable<T1> entities)
Parameters
Type Name Description
IEnumerable<T1> entities

The entities to remove.

Returns
Type Description
Task<bool>

Whether all entities were removed successfully or not.

See Also
IRepository<T1, T2>

RemoveRange(IEnumerable<T2>)

Removes the range of entities from the repository.

Declaration
public Task<bool> RemoveRange(IEnumerable<T2> ids)
Parameters
Type Name Description
IEnumerable<T2> ids

The unique ids of the entities to remove.

Returns
Type Description
Task<bool>

Whether all entities were removed successfully or not.

See Also
IRepository<T1, T2>

RemoveRange(Expression<Func<T1, bool>>)

Removes all entities that match the specified conditions (via the predicate Expression parameter).

WARNING: Can be slow. If you know the entities' id, please use the other RemoveRange overloads!
Declaration
public Task<bool> RemoveRange(Expression<Func<T1, bool>> predicate)
Parameters
Type Name Description
Expression<Func<T1, bool>> predicate

The predicate Expression that defines which entities should be removed.

Returns
Type Description
Task<bool>

Whether the entities were removed successfully or not.

See Also
IRepository<T1, T2>

SingleOrDefault(Expression<Func<T1, bool>>)

Gets a single entity from the repo according to the specified predicate condition.

If 0 or >1 entities are found, null is returned.

WARNING: Can be very slow! For specialized queries, just create a new repository! Derive from this class and create custom SQL queries that return the mapped types that you need.
Declaration
public Task<T1> SingleOrDefault(Expression<Func<T1, bool>> predicate)
Parameters
Type Name Description
Expression<Func<T1, bool>> predicate

The search predicate.

Returns
Type Description
Task<T1>

Single found entity; default(T1) (usually this is null) if 0 or >1 entities were found.

See Also
IRepository<T1, T2>

Update(T1)

Updates the specified entity.

Declaration
public abstract Task<bool> Update(T1 entity)
Parameters
Type Name Description
T1 entity

The entity to update.

Returns
Type Description
Task<bool>

Whether the entity could be updated successfully or not.

See Also
IRepository<T1, T2>

Implements

GlitchedPolygons.RepositoryPattern.IRepository<T1, T2>

See Also

IRepository<T1, T2>
Back to top Generated by DocFX