11 septembre 2007
2
11
/09
/septembre
/2007
21:43
Enterprise Library - Repository (partie 2)
II Création des entités métiers
Ici dans la couche BusinessEntities
Exemple :
public class Contact { private int _ContactID; public int ContactID { get { return _ContactID; } set { _ContactID = value; } } private string _ContactName; public string ContactName { get { return _ContactName; } set { _ContactName = value; } } private string _ContactFirstName; public string ContactFirstName { get { return _ContactFirstName; } set { _ContactFirstName = value; } } private Nullable<int> _ContactAge; public Nullable<int> ContactAge { get { return _ContactAge; } set { _ContactAge = value; } } private int _ContactCategoryID; public int ContactCategoryID { get { return _ContactCategoryID; } set { _ContactCategoryID = value; } } public Contact() { } public Contact(int ContactID,string ContactName,string ContactFirstName,Nullable<int> ContactAge,int ContactCategoryID) { this.ContactID = ContactID; this.ContactName = ContactName; this.ContactFirstName = ContactFirstName; this.ContactAge = ContactAge; this.ContactCategoryID = ContactCategoryID; } public override string ToString() { return "Name : " + ContactName + "rFirstname : " + ContactFirstName + (ContactAge.HasValue ? "rAge : " + ContactAge : string.Empty) + "rCategory : " + ContactCategoryID.ToString(); } } |
III Classes de création des commandes
- Celles-ci implémentent toutes une interface liée à la création commandes(ISelectionFactory<TIdentity> ,IInsertFactory <TDomainObject>,IUpdateFactory<TDomainObject>,IDeleteFactory<TDomainObject>)
Exemple avec la classe Contact
internal class GetAllContactsSelectionFactory : ISelectionFactory<NullableIdentity> { public DbCommand ConstructSelectCommand(Database db, NullableIdentity nullableIdentity) { DbCommand command = db.GetStoredProcCommand("dbo.GetAllContacts"); return command; } public string MapDbParameterToBusinessEntityProperty(string dbParameter) { switch (dbParameter) { default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter); } } } internal class GetContactsOfCategorySelectionFactory : ISelectionFactory<int> { public DbCommand ConstructSelectCommand(Database db, int contactCategoryID) { DbCommand command = db.GetStoredProcCommand("dbo.GetContactsOfCategory"); db.AddInParameter(command, "ContactCategoryID", DbType.Int32, contactCategoryID); return command; } public string MapDbParameterToBusinessEntityProperty(string dbParameter) { switch (dbParameter) { case "ContactCategoryID": return "ContactCategoryID"; default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter); } } } internal class GetContactSelectionFactory : ISelectionFactory<int> { public DbCommand ConstructSelectCommand(Database db, int contactID) { DbCommand command = db.GetStoredProcCommand("dbo.GetContact"); db.AddInParameter(command, "ContactID", DbType.Int32, contactID); return command; } public string MapDbParameterToBusinessEntityProperty(string dbParameter) { switch (dbParameter) { case "ContactID": return "ContactID"; default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter); } } } internal class ContactInsertFactory : IDbToBusinessEntityNameMapper, IInsertFactory<Contact> { public DbCommand ConstructInsertCommand(Database db, Contact contact) { DbCommand command = db.GetStoredProcCommand("dbo.AddContact"); db.AddInParameter(command, "ContactName", DbType.String, contact.ContactName); db.AddInParameter(command, "ContactFirstName", DbType.String, contact.ContactFirstName); if (contact.ContactAge.HasValue) db.AddInParameter(command, "ContactAge", DbType.Int32, contact.ContactAge); else db.AddInParameter(command, "ContactAge", DbType.Int32, DBNull.Value); db.AddInParameter(command, "ContactCategoryID", DbType.String, contact.ContactCategoryID); return command; } public void SetNewID(Database db, DbCommand command, Contact contact) { } public string MapDbParameterToBusinessEntityProperty(string dbParameter) { switch (dbParameter) { case "ContactAge": return "ContactAge"; case "ContactCategoryID": return "ContactCategoryID"; case "ContactFirstName": return "ContactFirstName"; case "ContactName": return "ContactName"; default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter); } } } internal class ContactUpdateFactory : IDbToBusinessEntityNameMapper, IUpdateFactory<Contact> { public DbCommand ConstructUpdateCommand(Database db, Contact contact) { DbCommand command = db.GetStoredProcCommand("dbo.UpdateContact"); db.AddInParameter(command, "ContactID", DbType.String, contact.ContactID); db.AddInParameter(command, "ContactName", DbType.String, contact.ContactName); db.AddInParameter(command, "ContactFirstName", DbType.String, contact.ContactFirstName); if (contact.ContactAge.HasValue) db.AddInParameter(command, "ContactAge", DbType.Int32, contact.ContactAge); else db.AddInParameter(command, "ContactAge", DbType.Int32, DBNull.Value); db.AddInParameter(command, "ContactCategoryID", DbType.String, contact.ContactCategoryID); return command; } public string MapDbParameterToBusinessEntityProperty(string dbParameter) { switch (dbParameter) { case "ContactAge": return "ContactAge"; case "ContactCategoryID": return "ContactCategoryID"; case "ContactFirstName": return "ContactFirstName"; case "ContactID": return "ContactID"; case "ContactName": return "ContactName"; default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter); } } } internal class ContactDeleteFactory : IDeleteFactory<int> { public DbCommand ConstructDeleteCommand(Database db, int contactID) { DbCommand command = db.GetStoredProcCommand("dbo.DeleteContact"); db.AddInParameter(command, "ContactID", DbType.Int32, contactID); return command; } public string MapDbParameterToBusinessEntityProperty(string dbParameter) { switch (dbParameter) { case "ContactID": return "ContactID"; default: throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter); } } } |
Published by Romagny13
-
dans
O-RM (Enterprise Library-CodeFluent-NHibernate)
commenter cet article …
commenter cet article …