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;}
publ开发者_JAVA技巧ic 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
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(); } }
It looks pretty good, 2 considerations:
Why is
_gratises
public? it is accessible throughGratises
, it should probably be privateDo you mind if someone replaces the "gratises" list in its entirety? If you do, I would remove the setter and add
AddGratis(Gratis)
andRemoveGratis(Gratis)
methods that proxy to the underlying List.
精彩评论