| 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 | ||||||
|
||||||||||
Enterprise Library - Repository (partie 3)
|
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);
}
|
|
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);
}
}
|
|
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;
}
}
|
|
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; }
}
}
|
|
<?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>
|
|
private void button1_Click(object sender, EventArgs e)
{
List<Contact> contacts = Repositories.Contacts.GetAllContacts();
dataGridView1.DataSource = contacts;
}
|