开发者

Modelling class

开发者 https://www.devze.com 2023-01-08 13:24 出处:网络
I try to model class where situation is similar to: public class Car { private List<Gratis> _gratises=new List<Gratis>();

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:

  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号