tags:

views:

191

answers:

2

Hi there

I am exploring this and see if this one make sense. For instance I have 2 abstract objects called: Customer and Tender. The relationship is that Customer can have many Tenders.

My question is how to achieve like this on the TestClient app:

1) customer.InTender[0].ID = ???

What method to handle to handle this? Do I need to pass CustomerID into Customer constructor to achieve this or ... ?

2) If I want to get all tenders for that particular customer should I do this:

customer.InTender.Get()

3) HOw do I differentiate between All Tender VS All Customer Tender (point 3). I guess it will be like this. One with ID of CUstomer and the other one without?

inTender.Get()

I appreciate your feedback.

===========================================

public abstract class Customer
{
    protected Int64 id;
    protected string name;

    protected ArrayList tender;

    public abstract ArrayList Tender
    {
       get;
       set;
    }  

    public abstract Int64 ID
    {
       get;
       set;
    }  

    public abstract string Name
    {
       get;
       set;
    }  


    public abstract bool Update();

    public abstract bool Add();

    public abstract bool Delete();
}

public class CorporateCustomer : Customer
{
    public CorporateCustomer ()
    {}

        public override ArrayList Tender
        {
            get
            {
                return tender
            }
            set
            {
                tender = value;
            }
        }   

    public override Int64 ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public override string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

    public override bool Update()
    {
    return true;
    }

    public override bool Add()
    {
    return true;
    }

    public override bool Delete()
    {
    return true;
    }
}


public abstract class Tender
{
    protected Int64 id;
    protected string name;

    public abstract bool Update();

    public abstract bool Add();

    public abstract bool Delete();

}

public class InTender : Tender
{
    public InTender ()
    {}


    public override Int64 ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public override string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

    public override bool Update()
    {
    return true;
    }

    public override bool Add()
    {
    return true;
    }

    public override bool Delete()
    {
    return true;
    }
}
+2  A: 

1) Don't use ArrayList, it was depreciated as of .net 2.0. You should use List, IList, or Dictionary.

Also, Customer sure seems like concrete type. Are you going to have multiple Customer classes that all inherit from it? If not, drop the Abstract. Same goes for your other classes.

2) Look up Repository objects and LazyLoading. Davy Bryon has a good series on building your own DAL. http://davybrion.com/blog/2009/08/build-your-own-data-access-layer-lazy-loading/

But either the customer should have all of the Tenders right away, or you should have a service that gets them for you. I'm not in favor of having Entities know about their persistence.

Anyway, the general approach is to have a separate Repository class that has the methods needed to get the data you need.

public class CustomerRepository
{
    public List<Customer> GetAllCustomers() { .... }
    public List<Tenders> GetTendersForCustomer(Customer customer) { .... }

}
Chris Brandsma
Great answer Chris.
Erik Mork
1) I am starting to use the Abstrac in my classes. I used to code directly on implementation of the class.2) With the code that you give me, let say if I want to test this class so in myTestApp:List<Tenders> tenderColl;CustomerRepository customer = new CustomerRepository ();tenderColl = customer.GetTenders();BUT I want to have in such away that:CorporateCUstomer customer = new Customer(21);Int64 tenderID = customer.Tender[0].ID; //TenderID of array 1string tenderName = customer.Tender[0].Name //TenderName of array 1
dewacorp.alliances
You are looking for an ActiveRecord style pattern here. Traditionally that is bad design because you are mixing responsibilities in your objects. Single Responsibility is the key. An object either holds its data, or it know how to persist itself. Try not to mix the two. The end result is that you get code that is very hard to test, but also hard to modify -- there is too much stuff going on in the code.
Chris Brandsma
A: 

I think a standard Tender class and a standard Customer class with a property List < Tender > should suffice. I don't see why you need the abstract classes and the inheritance.

class Tender {}

class Customer { List < Tender > tenders; // would be null if customer has no tenders .... }

Preets
There are possibility that tender could be "PreTender", "PostTender", "InTender"
dewacorp.alliances