How to represent a C# property in UML?

后端 未结 12 705
夕颜
夕颜 2020-12-08 19:03

Not quite an Attribute, not quite a Method. Stereotypes? <> <>?


I\'m retro-modelling an existin

相关标签:
12条回答
  • 2020-12-08 19:43

    properties are Get/Set methods wrapped up in some nicer syntax. Just put them in as methods, or create some new UML syntax for them :)

    0 讨论(0)
  • 2020-12-08 19:44

    I agree with workmad3. Properties are just a trick make a get/set methods a little bi nicer. For that reason I think it should be saved as a two different methods. What is more in this case you can set a different access permissions for them

    0 讨论(0)
  • 2020-12-08 19:44

    I use like this

    -memberThePropertyWillExpose
    +memberThePropertyIsExposing
    

    Well, comments on this are welcome if this is a right way !!

    0 讨论(0)
  • 2020-12-08 19:45

    I've been using <<get>> and <<set>> stereotypes next to property names so they look like fields, yet allows you to differentiate the access modifiers for the get or set:

    +=============================+
    | ClassName                   |
    +-----------------------------+
    | +<<get>> Id : int           |
    | -<<set>> Id : int           |
    | +<<get>> IsSomething : bool |
    +-----------------------------+
    | + Method1(arg1 : string)    |
    +=============================+
    

    Alternatively, if you don't want more than one occurrence of a property, this could work as well:

    +=============================+
    | ClassName                   |
    +-----------------------------+
    | +<<get>> -<<set>> Id : int  |
    

    And to reduce clutter, if the get and set have the same access modifier:

    +====================================+
    | ClassName                          |
    +------------------------------------+
    | +<<get, set>> Description : string |
    | +<<get>> -<<set>> Id : int         |
    

    This clearly communicates whether a property has a get or set, and if it is readonly (by way of no <<set>> existing in the class diagram). So basically what you said in your question.

    While properties are syntactic sugar for getter and setter methods, they are supposed to feel like fields, and I believe the UML diagram should reflect that fact, and at the same time also communicate what is public and what is private, and also whether a setter exists or not.

    0 讨论(0)
  • 2020-12-08 19:48

    You can represent properties the same way as fields. To specify additional info like readonly or writeonly you can use

    +Name:string {READONLY}

    0 讨论(0)
  • 2020-12-08 19:50

    Properties are just a convenient way of writing get_MyValue() and set_MyValue(value) allowing assignment rather than the normal method calling (using parenthesis).

    What you are accessing is actually a .NET property, C# has its own syntax for accessing these. Since under the skin the real get_ and set_ methods are created, so you could simply show those methods (to make your UML language independent - e.g. make your UML equally applicable to a VB.NET developer)

    ... or as you have suggested, introduce your own stereotype!

    0 讨论(0)
提交回复
热议问题