| Juillet 2009 | ||||||||||
| L | M | M | J | V | S | D | ||||
| 1 | 2 | 3 | 4 | 5 | ||||||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 | ||||
| 13 | 14 | 15 | 16 | 17 | 18 | 19 | ||||
| 20 | 21 | 22 | 23 | 24 | 25 | 26 | ||||
| 27 | 28 | 29 | 30 | 31 | ||||||
|
||||||||||
|
public class
Repository<TDomainObject>
{
private string
databaseName;
private Database db;
public Repository(string
databaseName)
{
this.databaseName =
databaseName;
this.db = DatabaseFactory.CreateDatabase(databaseName);
}
public List<TDomainObject> Find<TIdentity>(
ISelectionFactory<TIdentity>
selectionFactory,
IDomainObjectFactory<TDomainObject> domainObjectFactory,
TIdentity identity)
{
List<TDomainObject> results =
new List<TDomainObject>();
using (DbCommand command = selectionFactory.ConstructSelectCommand(db, identity))
{
using
(IDataReader rdr = db.ExecuteReader(command))
{
while (rdr.Read())
{
results.Add(domainObjectFactory.Construct(rdr));
}
}
}
return results;
}
public TDomainObject FindOne<TIdentity>(
ISelectionFactory<TIdentity>
selectionFactory,
IDomainObjectFactory<TDomainObject> domainObjectFactory,
TIdentity identity)
{
TDomainObject result = default(TDomainObject);
using (DbCommand command = selectionFactory.ConstructSelectCommand(db, identity))
{
using
(IDataReader rdr = db.ExecuteReader(command))
{
if (rdr.Read())
{
result = domainObjectFactory.Construct(rdr);
}
}
}
return result;
}
public int Add(IInsertFactory<TDomainObject> insertFactory,
TDomainObject domainObj)
{
using (DbCommand command = insertFactory.ConstructInsertCommand(db, domainObj))
{
int result
= db.ExecuteNonQuery(command);
insertFactory.SetNewID(db, command,
domainObj);
return
result;
}
}
public int
Update(IUpdateFactory<TDomainObject> updateFactory, TDomainObject domainObj)
{
using (DbCommand command = updateFactory.ConstructUpdateCommand(db, domainObj))
{
return
db.ExecuteNonQuery(command);
}
}
public int
Remove<TIdentityObject>(IDeleteFactory<TIdentityObject> deleteFactory,
TIdentityObject identityObj)
{
using (DbCommand command = deleteFactory.ConstructDeleteCommand(db, identityObj))
{
return
db.ExecuteNonQuery(command);
}
}
}
|