Is It possible to use the second part of this code for repository patterns and generics

Posted by newToCSharp on Stack Overflow See other posts from Stack Overflow or by newToCSharp
Published on 2010-05-14T22:29:08Z Indexed on 2010/05/14 22:34 UTC
Read the original article Hit count: 211

Is there any issues in using version 2,to get the same results as version 1. Or is this just bad coding.

Any Ideas

public class Customer { public int CustomerID { get; set; } public string EmailAddress { get; set; } int Age { get; set; } }

public interface ICustomer
{
    void AddNewCustomer(Customer Customer);
    void AddNewCustomer(string EmailAddress, int Age);
    void RemoveCustomer(Customer Customer);
}

public class BALCustomer
{
    private readonly ICustomer dalCustomer;
    public BALCustomer(ICustomer dalCustomer)
    {
        this.dalCustomer = dalCustomer;
    }

    public void Add_A_New_Customer(Customer Customer)
    {
        dalCustomer.AddNewCustomer(Customer);
    }

    public void Remove_A_Existing_Customer(Customer Customer)
    {
        dalCustomer.RemoveCustomer(Customer);
    }

}

public class CustomerDataAccess : ICustomer
{

    public void AddNewCustomer(Customer Customer)
    {
        // MAKE DB CONNECTION AND EXECUTE
        throw new NotImplementedException();
    }

    public void AddNewCustomer(string EmailAddress, int Age)
    {
        // MAKE DB CONNECTION AND EXECUTE
        throw new NotImplementedException();
    }

    public void RemoveCustomer(Customer Customer)
    {
        // MAKE DB CONNECTION AND EXECUTE
        throw new NotImplementedException();
    }


}

//          VERSION 2 

public class Customer_New : DataRespository<CustomerDataAccess>
{
    public int CustomerID { get; set; }
    public string EmailAddress { get; set; }
    public int Age { get; set; }
}

public class DataRespository<T> 
    where T:class,new()
{

    private T item = new T();
    public T Execute { get { return item; } set { item = value; } }
    public void Update()
    {
        //TO BE CODED
    }

    public void Save()
    {
        //TO BE CODED
    }

    public void Remove()
    {
        //TO BE CODED
    }
}



class Program
{
    static void Main(string[] args)
    {
        Customer_New cus = new Customer_New()
        {
            Age = 10,
            EmailAddress = "[email protected]"
        };

        cus.Save();
        cus.Execute.RemoveCustomer(new Customer());

        // Repository Version

        Customer customer = new Customer()
        {
            EmailAddress = "[email protected]",
            CustomerID = 10
        };

        BALCustomer bal = new BALCustomer(new CustomerDataAccess());
        bal.Add_A_New_Customer(customer);
    }
}

}

© Stack Overflow or respective owner

Related posts about generics

Related posts about repository-pattern