Our first change is to decouple controllers and business logic by manipulating interfaces rather than concrete implementation. The interface is then called a contract (SOA way). The concrete implementation will be injected lately by a dependency injection framework. Below is the procedure:
  1. Create an interface
  2. Create a concrete implementation of the interface

Right click on the folder model and create an interface IContactManagerService. The code below shows the content of the interface:

public interface IContactManagerService
{
    void CreateContact(Contact contactToCreate);
    List<Contact> ListContact();
    void UpdateContact(Contact contactToUpdate);
    void DeleteContact(Contact contactToDelete);
}

Right click on the folder model and create a ContactManagerLocal class that implement IContactManagerService. Copy CRUD routine written in the ContactManagerRepository class to the new created class as shown below:

public class ContactManagerLocal : IContactManagerService
{
    private List<Contact> listOfContact = null;
    public ContactManagerLocal()
    {
        //Initializing the list of contact
        if (listOfContact == null) listOfContact = GenerateData();
    }

    #region IContactManagerService Members

    public void CreateContact(ContactManager.data.util.Contact contactToCreate)
    {
        //Getting the max contactId
        long maxContactId = listOfContact.Max<Contact>().Id;

        //Initializing contactTosave
        contactToCreate.Id = ++maxContactId;

        //Inserting the contact into the list
        listOfContact.Add(contactToCreate.Clone() as Contact);
    }

    public List<ContactManager.data.util.Contact> ListContact()
    {
        return listOfContact;
    }

    public void UpdateContact(ContactManager.data.util.Contact contactToUpdate)
    {
        //Retrieving the old contact in the list            
        int index = listOfContact.IndexOf(contactToUpdate);

        //Updating the list
        listOfContact[index] = contactToUpdate;
    }

    public void DeleteContact(ContactManager.data.util.Contact contactToDelete)
    {
        listOfContact.Remove(contactToDelete);
    }
    #endregion

    #region Generating Data
    private List<Contact> GenerateData()
    {
        List<Contact> myList = new List<Contact>();

        myList.Add(CreateContact(1, "YACOUBOU", "Mohamadou", "+23799657392", "mohamadou11@gmail.com"));
        myList.Add(CreateContact(2, "DJOMGA TOUKO", "Peter Charles", "+23799727707", "djomgat@gmail.com"));
        myList.Add(CreateContact(3, "BAKE", "Bakeneghe", "1111111", "bbakeneghe@yahoo.fr"));
        myList.Add(CreateContact(4, "MAYENGUE", "Salomon", "+23799138606", "mayengue2008@yahoo.fr"));

        return myList;
    }

    private Contact CreateContact(long id, string firstName, string lastName, string phoneNumber, string email)
    {
        return new Contact { Id = id, FirstName = firstName, LastName = lastName, PhoneNumber = phoneNumber, Email = email };
    }
    #endregion
}

Last edited Jun 23, 2009 at 12:07 AM by koossery, version 1

Comments

No comments yet.