tags:

views:

35

answers:

2

I try to model class where situation is similar to:

public class Car
{
private List<Gratis> _gratises=new List<Gratis>();

public List<Gratis> Gratises
  {
          get { return _gratises; }
          set { _gratises= value; }
       }

public class Gratis
{
public string Name {get;set;}

public int ID {get;set;}
}

In one place user manages list of gratises. He can Add, Remove, or Edit gratises.

In second module user can manages cars:

He can add or remove gratis to car.

Is my model ok ?

If is, how implement this?

I have datagridview and:

get list of gratis from database, and next get a list of gratis of car ?

Or maybe add IsChecked field ?

Sql Server 2005. asmx, and WinForms

+1  A: 

It looks pretty good, 2 considerations:

  1. Why is _gratises public? it is accessible through Gratises, it should probably be private

  2. Do you mind if someone replaces the "gratises" list in its entirety? If you do, I would remove the setter and add AddGratis(Gratis) and RemoveGratis(Gratis) methods that proxy to the underlying List.

Brenton Alker
Why is _gratises public? -it was mistake :) Conception with Add and RemoveGratis(gratis) looks fine :)
+2  A: 

Look at M.Fowler refactoring approach Encapsulate Collection

Also note that in get properties return read-only collection:

public List<Gratis> Gratises

{ get { return _gratises.AsReadOnly(); } }

Arseny