Friday, February 12, 2010

Data Access Layer in C# using Repository and Data Mapper – Part 3


Mapping the data elements in the domain model and relational model for the customer entity functionality is implemented as part of the CustomerMapper class which inherits from the BaseMapper created in the previous sample. The customer mapper class contains code to convert data between these data models.
public override Customer Construct(IDataRecord record)
{
    Customer customer = ConstructCommonData(record);

    int index = record.GetOrdinal("FirstName");
    customer.FirstName = record.IsDBNull(index) ? string.Empty : record.GetString(index);
    index = record.GetOrdinal("LastName");
    customer.LastName = record.IsDBNull(index) ? string.Empty : record.GetString(index);
    index = record.GetOrdinal("Address");
    customer.Address = record.IsDBNull(index) ? string.Empty : record.GetString(index);

    return customer;
}
The overridden Construct method is responsible for this data conversion. Once the data mapper is ready, we’ll use this in the repository for data access. The data mapper is not exposed outside the repository.
Creating the repository for the sample
Martin fowler writes:
"A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers."
Each entity in a domain layer defines a repository for data access operations. In our sample we have one entity (Customer). We’ll define a repository for the customer entity and write some test cases to demonstrate the functionality. The repository interface defines the CRUD operations required for our sample.
    T GetById(Key id);
    IList GetAll();
    void Insert(T entity);
    T Update(T entity);
    void Remove(Key id);
I have created a base repository class which will be used as the base class for entity specific repositories and implements the common functionality.
    protected T FindOne(DbCommand command)
    {
        T entity = new T();
        using (IDataReader reader = ___Database.ExecuteReader(command))
        {
            if (reader.Read())
                entity = Mapper.Construct(reader);
        }
        return entity;
    }

    protected IList Find(DbCommand command)
    {
        IList entityCollection = default(List);
        using (IDataReader reader = ___Database.ExecuteReader(command))
        {
            entityCollection = Mapper.ConstructCollection(reader);               
        }
        return entityCollection;
    }

The source for BaseRepository can be downloaded from this link
Next we’ll create the customer repository.

7 comments:

Anonymous said...

hi,

i find your post about the repository and mapper patterns very useful.

Unfortunately some links to the code are broken.

Can you please update these links again?

Thank you

regards marc

Imran Zahid said...

Nice article .... can you attach a sample project as I am new to design patterns

Thanks

Unknown said...

Can you Please share the entire code for this sample. I am new to this repository pattern and i feel debugging the code would help me understand the code better. Thanks
1986gansa@yahoo.com

Unknown said...

Can you Please share the entire code for this sample. I am new to this repository pattern and i feel debugging the code would help me understand the code better. Thanks
1986gansa@yahoo.com

Unknown said...

Can you Please share the entire code for this sample. I am new to this repository pattern and i feel debugging the code would help me understand the code better. Thanks
1986gansa@yahoo.com

Unknown said...

Can you Please share the entire code for this sample. I am new to this repository pattern and i feel debugging the code would help me understand the code better. Thanks
1986gansa@yahoo.com

Unknown said...

Can you Please share the entire code for this sample. I am new to this repository pattern and i feel debugging the code would help me understand the code better. Thanks
1986gansa@yahoo.com