none
Doubts about encapsulation RRS feed

  • Question

  • Hi Everybody,

    I'm some doubts about the encapsulation where I'd like to show some situations in which you can help or share your ideas. Lets see the scenarios:

    1 - Properties

    In C#, we can define properties where there can be set and get. In this case Is it havind encapsulation where the property has get and set?

    Ex: 

    public string Name{get; set;}

    And in the case of collections?

    public ILIst<string> Names{get; set;}

    What criteria do you use for bussiness or domain and dto's?

    2 - Binding...

    To bind components with and entity the entity has to have properties and then to create a bind with the specific control to specific property of entity.

     

    in this case, Is there a violation of encapsulation?

    3 - Methods Get and Set

    If I create get and set methods for some attributes I think is not correct, because it is the same you access the attribute directly. I gues the method must have another name that is close to the business, instand of SetCities for AddCities...

    So, What do you do in these cases??

    Thank you very much all the attention.

    Best regards,

    Carlos

     

    Friday, July 11, 2014 1:10 PM

Answers

  • I'm not 100% sure I'm following the question correctly but a property is just a mechanism to support getting a value by attempting to describe its role by the syntax, i.e. this is a detail of the object rather than a function you perform on it. IMO It's not really about encapsulation (although technically it is an example). The example of getting lists or collections is typically seen as an anti-pattern because you're exposing the underlying list that can be manipulated by the client and therefore removing the encapsulation. The "rule" is that you should always return a copy - now practically how many people do that, especially for internal solutions, is probably in doubt. I don't understand your 3rd point.

    http://pauliom.wordpress.com

    • Marked as answer by Carlos Marchi Monday, July 14, 2014 7:40 PM
    Monday, July 14, 2014 8:38 AM