11 septembre 2007
2
11
/09
/septembre
/2007
21:40
Enterprise Library - Repository (partie 3)
IV Repository
Toujours avec Contact pour exemple
On crée une interface listant les méthodes qui devront être implémenté
public interface IContactRepository { List<Contact> GetAllContacts(); List<Contact> GetContactsOfCategory(int categoryID); Contact GetContact(int contactID); int AddContact(Contact contact); int UpdateContact(Contact contact); int RemoveContact(int contactID); } |
La classe hérite de Repository<TDomainObject> et implémente son interface
public class ContactRepository : Repository<Contact>,IContactRepository { public ContactRepository(string databaseName) : base(databaseName) { } public List<Contact> GetAllContacts() { ISelectionFactory<NullableIdentity> selectionFactory = new GetAllContactsSelectionFactory(); try { NullableIdentity nullableIdentity = new NullableIdentity(); return Find(selectionFactory, new ContactFactory(), nullableIdentity); } catch (SqlException ex) { HandleSqlException(ex, selectionFactory); } return new List<Contact>(); } public List<Contact> GetContactsOfCategory(int contactCategoryID) { ISelectionFactory<int> selectionFactory = new GetContactsOfCategorySelectionFactory(); try { return Find<int>(selectionFactory, new ContactFactory(), contactCategoryID); } catch (SqlException ex) { HandleSqlException(ex, selectionFactory); } return new List<Contact>(); } public Contact GetContact(int contactID) { ISelectionFactory<int> selectionFactory = new GetContactSelectionFactory(); try { return FindOne(selectionFactory, new ContactFactory(), contactID); } catch (SqlException ex) { HandleSqlException(ex, selectionFactory); } return new Contact(); } public int AddContact(Contact contact) { IInsertFactory<Contact> insertFactory = new ContactInsertFactory(); try { return Add(insertFactory, contact); } catch (SqlException ex) { HandleSqlException(ex, insertFactory); } return 0; } public int UpdateContact(Contact contact) { IUpdateFactory<Contact> updateFactory = new ContactUpdateFactory(); try { return Update(updateFactory, contact); } catch (SqlException ex) { HandleSqlException(ex, updateFactory); } return 0; } public int RemoveContact(int contactID) { IDeleteFactory<int> deleteFactory = new ContactDeleteFactory(); try { return Remove(deleteFactory, contactID); } catch (SqlException ex) { HandleSqlException(ex, deleteFactory); } return 0; } private void HandleSqlException(SqlException ex, IDbToBusinessEntityNameMapper mapper) { if (ex.Number == ErrorCodes.SqlUserRaisedError) { switch (ex.State) { case ErrorCodes.ValidationError: string[] messageParts = ex.Errors[0].Message.Split(':'); throw new RepositoryValidationException( mapper.MapDbParameterToBusinessEntityProperty(messageParts[0]), messageParts[1], ex); case ErrorCodes.ConcurrencyViolationError: throw new ConcurrencyViolationException(ex.Message, ex); } } throw new RepositoryFailureException(ex); } } |
J’utilise une et une seule fabrique pour la création d’instances à partir d’un datareader
Exemple ici ContactFactory
internal class ContactFactory : IDomainObjectFactory<Contact> { public Contact Construct(System.Data.IDataReader reader) { Contact contact = new Contact(); contact.ContactID = Convert.ToInt32(reader["ContactID"]); contact.ContactName = Convert.ToString(reader["ContactName"]); contact.ContactFirstName = Convert.ToString(reader["ContactFirstName"]); if (reader["ContactAge"] != DBNull.Value) contact.ContactAge = Convert.ToInt32(reader["ContactAge"]); contact.ContactCategoryID = Convert.ToInt32(reader["ContactCategoryID"]); return contact; } } |
V Création d’une «classe façade » Repositories
Ici dans la couche BusinessLogic, c’est avec cette classe que la couche présentation dialoguera
public static class Repositories { private const string databaseName = "ContactDBConnectionString"; private static IContactRepository contactRepository = new ContactRepository(databaseName); private static ICategoryRepository categoryRepository = new CategoryRepository(databaseName); public static IContactRepository Contacts { get { return contactRepository; } } public static ICategoryRepository Categories { get { return categoryRepository; } } } |
VI Couche présentation
Le fichier de configuration peut être au plus simple :
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="ContactDBConnectionString" connectionString="Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|ContactDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/> </connectionStrings> </configuration> |
Exemple de code
private void button1_Click(object sender, EventArgs e) { List<Contact> contacts = Repositories.Contacts.GetAllContacts(); dataGridView1.DataSource = contacts; } |
Published by Romagny13
-
dans
O-RM (Enterprise Library-CodeFluent-NHibernate)
commenter cet article …
commenter cet article …