views:

56

answers:

2

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);
    }
}
A: 

I think while implementing object model of your application you just have to ask yourself a number of questions as though you are make object design review of your collegue code.

  • Why CustomerAccessLayer implements interface? Is there will be a number of layers implementing this Interface. Or maybe you are expecting any polymorph behaviour from classes implements this interface? Or maybe you will separate interface to standalone module and will provide its functionality though any kind of service?
  • Why do you need BALCustomer class? Why you could not make calls directly to CustomerAccesLayer? And, have i already spoke about codesyle? :)
  • If DataRepository have a generic behaviour and will provide a number of AccessLayers throw Execute property why it is have its own methods?

I think could be continued... I hope you've catch my point?

Vokinneberg
A: 

You have a lot of things going on that aren't making a lot of sense.

First of all, the names of properties should always be a noun (singular or plural) or a "being" verb like Is* or Has*. These are properties of an object, and should be similar to what you would say in response to a question like "Would you please describe your desk?" Execute is an operation, and should therefore be a method. Likewise, your naming conventions in Version 1 should be PascalCased which means no underscores and the first letter of all words should be capitalized. These aren't die-hard truths, but they are considered OOP standards.

Secondly, the code in your main method isn't actually implementing anything in your generic class. The only thing your class is actually doing is creating an instance of CustomerDataAccess. The Save() method won't do anything, unless you specifically are able to call item.Save() In order to use your Save, Update, Delete functionality on your generic class, your CustomerDataAccess class will have to implement an interface expected by your generic class. For instance:

public interface IDataAccess<T> : where T : YourBaseObject {
   public void Update(T item);
   public void Save(T item);
   public void Remove(T item);
}

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

public class CustomerDataAccess : 
    DataRespository<IDataAccess<Customer>> {
    public void PerformCustomerOnlyAction(Customer customer) { 
     /* do stuff */
    }
}

Now, you can create a generic class that handles basic CRUD functionality, and all other functionality is accessible through the BaseRepository property.

/* e.g. T = IDataAccess<Customer>, K = Customer */
public class DataRespository<T>
   where T : IDataAccess<K>, new()
   where K : YourBaseObject, new()
{

    private T _base;
    public T BaseRepository { 
          get { 
             if(_base == null)
                 _base = Activator.CreateInstance<T>(); 
             return _base;
          } 
    }
    public void Update(K item) { /* functionality for YourBaseObject */ }
    public void Save(K item) { /* functionality for YourBaseObject */ }
    public void Remove(K item) {  /* functionality for YourBaseObject */ }
}   

class Program
{
    static void Main(string[] args)
    {
        var repository = new CustomerDataAccess();
        Customer c = new Customer {
            Age = 10,
            EmailAddress = "[email protected]"
        };
        repository.Save(c);
        // This pass-through is no longer needed, but shown as example
        // repository.BaseRepository.PerformCustomerOnlyAction(c);
        repository.PerformCustomerOnlyAction(c);
    }
}

NOTE I did the above code from scratch/memory. The generic type constraints may not work exactly as I have them.

ASP.NET 3.5 Unleashed by Stephen Walther has a couple of chapters on creating a repository pattern which is setup similarly to what you're trying to accomplish in Version 2. He also splits processing up between a business logic layer and a data access layer. Although the book is huge (nearly 2000 pages) and many of the code examples are redundant or better left as part of the CD, he goes pretty in-depth for beginner-to-intermediate range. It's available used on Amazon for around $25.

Jim Schubert