tags:

views:

59

answers:

1

Hello, I'm wondering how you guys would solve this problem. I have following class

public class MyTask 
{
    public int CustomerID { get; set; }
    public int ProjectID { get; set; }
}

public List<Project> AllProjects { get; set; }
public class Project 
{
    public string ProjectName { get; set; }
    public int CustomerID { get; set; }
}

Now at program-start, I load all available Projects into "AllProjects". Then I bind the Collection to a ComboBox, where a User first has to enter the CustomerID and depending on that, the ComboBox for Projects changes. What's in your opinion the best way to do that? Using a CollectionView for each MyTask?

What I'm doing right now is have in MyTask a List AvailableProjects, which gets changed each time when the MyTask.CustomerID is changed.. e.g.

public int CustomerID { 
    get { return _customerID; }
    set { _customerID = value; UpdateAvailableProjects(); }
}
private void UpdateAvailableProjects()
{
   //Loop trough static.Main.AllProjects and check if Project.CustomerID == this.CustomerID); 
}

Thanks for any help.

Cheers

A: 

Why not make a class Customer and from there get all associated projects which can then be loaded in the combo.

public class Customer
{
    private int customerId = 0;
    public int CustomerId
    {
        get
        {
            return customerId;
        }
        set
        {
            customerId = value;
        }
    }

    public List<Project> availableProjects
    {
        get
        {
            return getCustomerRelatedProjects();
        }
    }

    //you change the accessor here according to the needs
    private List<Project> getCustomerRelatedProjects()
    {
        //do the processing here for Database hit or from the list you load at the program load etc.
        return new List<Project>();
    }

}

public class Project 
{
    int projectId = 0;

    public int ProjectId
    {
        get
        {
            return projectId;
        }
        set
        {
            projectId = value;
        }
    }

    string projName = string.Empty;
    public string ProjectName
    {
        get
        {
            return projName;
        }
        set
        {
            projName = value;
        }
    }
}
Beginner
I would recommend against this approach. You are blending concerns and creating couplings between classes that do not (and maybe should not) be coupled. The active record pattern, is used for the Customer class, is an anti-pattern when you consider the principals of Separation of Concerns and Single Responsibility.
jrista