Question

This is the current design pattern I employ for my ASP.NET web application, I've seen people using repositories but I don't get the implementation of the repository design pattern.

Can anyone point out how I can implement the repository pattern in my design? And also what kind of benefits I will get if I implement a repository IN MY CASE.

Also, any good use of interfaces that I should implement in my design?

Customer class (Customer.cs)

public class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
}

DBManager class (DBManager.cs)

public class DBManager
{
    private const string connectionString = "some connection string";

    public static IEnumerable<Customer> getAllCustomers()
    {
        List<Customer> cust = new List<Customer>();

        try
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();

                string cmdText = "SELECT * FROM Customer";
                SqlCommand comm = new SqlCommand(cmdText, conn);
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    cust.Add(new Customer 
                    {
                        CustomerID = int.Parse((string)dr["customerID"]),
                        Name = (string)dr["name"],
                        Address1 = (string)dr["address1"],
                        Address2 = (string)dr["address2"]
                    });
                }
            }
        }
        catch (SqlException e)
        {
            throw e;
        }

        return cust;
    }
}

Index page code-behind (index.aspx.cs)

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IEnumerable<Customer> cust = DBManager.getAllCustomers();

        // some codes to display IEnum customers in page
    }
}
Was it helpful?

Solution

In your case, your DBManager is very near a repository pattern in itself. In the world of Domain Driven Design a Repository is typically created for each Aggregate root that you find your domain as in the explanation by Martin Fowler.

http://martinfowler.com/bliki/DDD_Aggregate.html

For each Entity/Aggregate root that you deal with in your domain you would create a Repository that would handle the CRUD operations (Create, Read, Update, and Delete). So in your case you could have a Customer Repository.

Edit: The benefit is that the implementation of how the data is touched and how it works is abstracted from the rest of your code. Then you can simply call the methods on your repository to make the code easier to read. Also, if you implement your Repository as an interface, you can start to use things like Dependency Injection, which will make your code more maintainable, testable, and other *ables. ;-)

A typical Repository interface in C# would look something like this (obviously can be a little different depending on your needs.

public interface ICustomerRepository : IDisposable
{
    IEnumerable<Customer> GetCustomers();
    Customer GetCustomerByID(int customerId);
    void InsertCustomer(Customer customer);
    void DeleteCustomer(int customerId);
    void UpdateCustomer(Customer customer);
    void Save();
}

A good resource for learning more about this can be found on MSDN. The example is using MVC and Entity Framework, but that can obviously be tweaked for your application.

http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top