A repository can store simple classes. More...
Public Member Functions | |
Repository (DbConnection connection, SqlStrategy builder) | |
Creates an instance of Repository. | |
void | Commit () |
Commit changes to the database. | |
void | Rollback () |
Rollback the database to last commit. | |
void | CreateTable< T > () |
Creates a table for type T. | |
void | Add< T > (T item) |
Add an instance of T to the database. | |
bool | Update< T > (T item) |
Update a row in the database. | |
bool | Delete< T > (T item) |
Delete a row from the database. | |
int | DeleteWhere< T > (string condition, params object[] parameters) |
Deletes all rows where the condition is satisfied. | |
IEnumerable< T > | Where< T > (string condition, params object[] parameters) |
Executes a where Sql statement with a custom condition. | |
T | SingleWhere< T > (string condition, params object[] parameters) |
Executes a where Sql statement with a custom condition, returns only one row or null. | |
IEnumerable< T > | All< T > () |
List all rows in the table for T. | |
IEnumerable< T > | Paged< T > (int page, int pageSize) |
List all rows in the table for T, paged. | |
IEnumerable< T > | Query< T > (string sql, params object[] parameters) |
Execute a custom sql query. | |
T | SingleQuery< T > (string sql, params object[] parameters) |
Execute a custom sql query, and return only the first row or null. | |
void | Dispose () |
Release resources held by this object. | |
void | Close () |
Close the database connection. | |
Static Public Member Functions | |
static Repository | ConnectSqlite (string database) |
Create a Repository connected to a database. | |
static Repository | ConnectSqlite (string database, bool disableCommit) |
Create a Repository connected to a database. | |
Protected Member Functions | |
virtual void | Dispose (bool disposing) |
Dispose this object. | |
Private Member Functions | |
void | EnsureTable< T > () |
Ensures the existance of a table. | |
string | TableName< T > () |
Get table name of a type. | |
~Repository () | |
Finalize this object. | |
Private Attributes | |
DbConnection | connection = null |
Database connection, this is remain open untill disposed/closed. | |
SqlStrategy | builder |
SqlFactory for building sql statments. | |
List< string > | tables = new List<string>() |
List of tables, so that we need not check before every operation. | |
DbTransaction | transaction = null |
Current transaction. | |
bool | disableCommit = false |
True, if commit should be disabled, used for testing. |
A repository can store simple classes.
Types used with this model may not contain properties that are not both get and set. The properties must not be assumed case sensitive.
Definition at line 22 of file Repository.cs.
Foodolini.Database.Repository.Repository | ( | DbConnection | connection, | |
SqlStrategy | builder | |||
) |
Creates an instance of Repository.
connection | A DbConnection to the database, the constructor will Open this connection, just initialize it. | |
builder | A SqlFactory for building sql statements for the database engine. |
Definition at line 56 of file Repository.cs.
Foodolini.Database.Repository.~Repository | ( | ) | [private] |
Finalize this object.
Definition at line 573 of file Repository.cs.
void Foodolini.Database.Repository.Add< T > | ( | T | item | ) |
Add an instance of T to the database.
Note that the items primary key will be updated to match that of the newly inserted row.
item | The item to add to the database |
T | : | class | |
T | : | new() |
IEnumerable<T> Foodolini.Database.Repository.All< T > | ( | ) |
List all rows in the table for T.
T | : | class | |
T | : | new() |
void Foodolini.Database.Repository.Close | ( | ) |
void Foodolini.Database.Repository.Commit | ( | ) |
Commit changes to the database.
This method commits current transaction and starts a new transaction. Do not call this method too often, as it may cause performance issues. Invoke this method whenever the database variant is upheld.
Definition at line 80 of file Repository.cs.
static Repository Foodolini.Database.Repository.ConnectSqlite | ( | string | database, | |
bool | disableCommit | |||
) | [static] |
Create a Repository connected to a database.
database | Path to the database, or :memory: for in-memory database | |
disableCommit | Disable commit, used for testing |
Definition at line 130 of file Repository.cs.
static Repository Foodolini.Database.Repository.ConnectSqlite | ( | string | database | ) | [static] |
Create a Repository connected to a database.
database | Path to the database, or :memory: for in-memory database |
Definition at line 104 of file Repository.cs.
void Foodolini.Database.Repository.CreateTable< T > | ( | ) |
Creates a table for type T.
A property named classnamed + 'Id' will be used as primary key, should be a long.
T | : | class | |
T | : | new() |
bool Foodolini.Database.Repository.Delete< T > | ( | T | item | ) |
Delete a row from the database.
This method uses primary key to identify which row to delete. The primary key on item is set to 0, once deleted.
item | Item representing the row to be deleted |
T | : | class | |
T | : | new() |
int Foodolini.Database.Repository.DeleteWhere< T > | ( | string | condition, | |
params object[] | parameters | |||
) |
Deletes all rows where the condition is satisfied.
condition | Condition, will be replaced with the first parameter | |
parameters | Parameters to the condition, inserted into the condition with , and so on. |
T | : | class | |
T | : | new() |
void Foodolini.Database.Repository.Dispose | ( | ) |
Release resources held by this object.
Definition at line 564 of file Repository.cs.
virtual void Foodolini.Database.Repository.Dispose | ( | bool | disposing | ) | [protected, virtual] |
Dispose this object.
This boolean disposing parameter here ensures that objects with a finalizer is not disposed, this is method is invoked from the finalizer. Do overwrite, and call, this method in base classes if you use any unmanaged resources.
disposing | A System.Boolean False if called from the finalizer, True if called from Dispose. |
Definition at line 546 of file Repository.cs.
void Foodolini.Database.Repository.EnsureTable< T > | ( | ) | [private] |
Ensures the existance of a table.
This method does not check that the columns of the table exists and are correct. Just that a table with the name of the type + "s" exists. This method uses a catched list of tables, so it is not expensive.
T | : | class | |
T | : | new() |
IEnumerable<T> Foodolini.Database.Repository.Paged< T > | ( | int | page, | |
int | pageSize | |||
) |
List all rows in the table for T, paged.
page | Page number, starting from 0 | |
pageSize | Page size, should be >= 1 |
T | : | class | |
T | : | new() |
IEnumerable<T> Foodolini.Database.Repository.Query< T > | ( | string | sql, | |
params object[] | parameters | |||
) |
void Foodolini.Database.Repository.Rollback | ( | ) |
Rollback the database to last commit.
Definition at line 90 of file Repository.cs.
T Foodolini.Database.Repository.SingleQuery< T > | ( | string | sql, | |
params object[] | parameters | |||
) |
Execute a custom sql query, and return only the first row or null.
sql | Sql query to execute | |
parameters | Parameters to the Sql query, {0} is substituted for the first parameter, and {1} for next and so on. |
T | : | class | |
T | : | new() |
T Foodolini.Database.Repository.SingleWhere< T > | ( | string | condition, | |
params object[] | parameters | |||
) |
Executes a where Sql statement with a custom condition, returns only one row or null.
This is the same as Repository.Where except it only returns one row
condition | Condition to execute | |
parameters | Parameters to the Sql condition, {0} is substituted for the first parameter, and {1} for next and so on. |
T | : | class | |
T | : | new() |
string Foodolini.Database.Repository.TableName< T > | ( | ) | [private] |
Get table name of a type.
T | : | class | |
T | : | new() |
bool Foodolini.Database.Repository.Update< T > | ( | T | item | ) |
Update a row in the database.
The row is found using the primary key, if the type has no primary key, this method will throw and exception.
item | Instance of T to update in the database |
T | : | class | |
T | : | new() |
IEnumerable<T> Foodolini.Database.Repository.Where< T > | ( | string | condition, | |
params object[] | parameters | |||
) |
SqlFactory for building sql statments.
Definition at line 32 of file Repository.cs.
DbConnection Foodolini.Database.Repository.connection = null [private] |
Database connection, this is remain open untill disposed/closed.
Definition at line 27 of file Repository.cs.
bool Foodolini.Database.Repository.disableCommit = false [private] |
True, if commit should be disabled, used for testing.
Definition at line 116 of file Repository.cs.
List<string> Foodolini.Database.Repository.tables = new List<string>() [private] |
List of tables, so that we need not check before every operation.
Definition at line 37 of file Repository.cs.
DbTransaction Foodolini.Database.Repository.transaction = null [private] |
Current transaction.
Transactions is a requirement for Sqlite to offer proper performance.
Definition at line 45 of file Repository.cs.