Foodolini.Database.Repository Class Reference

A repository can store simple classes. More...

Collaboration diagram for Foodolini.Database.Repository:
Collaboration graph

List of all members.

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.
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.
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.

Detailed Description

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.


Constructor & Destructor Documentation

Foodolini.Database.Repository.Repository ( DbConnection  connection,
SqlStrategy  builder 
)

Creates an instance of Repository.

Parameters:
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.


Member Function Documentation

void Foodolini.Database.Repository.Add< 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.

Parameters:
item The item to add to the database
Type Constraints
T :class 
T :new() 
IEnumerable<T> Foodolini.Database.Repository.All< T > (  ) 

List all rows in the table for T.

Returns:
The result of the query as instances of T
Type Constraints
T :class 
T :new() 
void Foodolini.Database.Repository.Close (  ) 

Close the database connection.

The same as dispose

Definition at line 586 of file Repository.cs.

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.

Parameters:
database Path to the database, or :memory: for in-memory database
disableCommit Disable commit, used for testing
Returns:
Repository for the database

Definition at line 130 of file Repository.cs.

static Repository Foodolini.Database.Repository.ConnectSqlite ( string  database  )  [static]

Create a Repository connected to a database.

Parameters:
database Path to the database, or :memory: for in-memory database
Returns:
Repository for the 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.

Type Constraints
T :class 
T :new() 
bool Foodolini.Database.Repository.Delete< 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.

Parameters:
item Item representing the row to be deleted
Returns:
True, if exactly one row was deleted.
Type Constraints
T :class 
T :new() 
int Foodolini.Database.Repository.DeleteWhere< T > ( string  condition,
params object[]  parameters 
)

Deletes all rows where the condition is satisfied.

Parameters:
condition Condition, will be replaced with the first parameter
parameters Parameters to the condition, inserted into the condition with , and so on.
Returns:
Number of rows deleted
Type Constraints
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.

Parameters:
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.

Type Constraints
T :class 
T :new() 
IEnumerable<T> Foodolini.Database.Repository.Paged< T > ( int  page,
int  pageSize 
)

List all rows in the table for T, paged.

Parameters:
page Page number, starting from 0
pageSize Page size, should be >= 1
Returns:
The result of the query as instances of T
Type Constraints
T :class 
T :new() 
IEnumerable<T> Foodolini.Database.Repository.Query< T > ( string  sql,
params object[]  parameters 
)

Execute a custom sql query.

Parameters:
sql A System.String
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.
Returns:
The result of the query as instances of T
Type Constraints
T :class 
T :new() 
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.

Parameters:
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.
Returns:
The result of the query as instance of T or null
Type Constraints
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

Parameters:
condition Condition to execute
parameters Parameters to the Sql condition, {0} is substituted for the first parameter, and {1} for next and so on.
Returns:
The result of the query as instance of T or null
Type Constraints
T :class 
T :new() 
string Foodolini.Database.Repository.TableName< T > (  )  [private]

Get table name of a type.

Returns:
A System.String table name
Type Constraints
T :class 
T :new() 
bool Foodolini.Database.Repository.Update< 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.

Parameters:
item Instance of T to update in the database
Returns:
True, if one row was affected.
Type Constraints
T :class 
T :new() 
IEnumerable<T> Foodolini.Database.Repository.Where< T > ( string  condition,
params object[]  parameters 
)

Executes a where Sql statement with a custom condition.

Parameters:
condition Condition to execute
parameters Parameters to the Sql condition, {0} is substituted for the first parameter, and {1} for next and so on.
Returns:
The result of the query as instances of T
Type Constraints
T :class 
T :new() 

Member Data Documentation

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.

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.


The documentation for this class was generated from the following file:

Foodolini 1.0.0 Documentation, generated with DoxyGen.