Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| 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); } } } |