Book a Demo

Author Topic: Problems with EA and C#  (Read 4809 times)

phurst

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Problems with EA and C#
« on: May 01, 2013, 01:02:26 am »
When I reverse-engineer C# classes, the automatic properties are treated as operations (with the “Property” stereotype). Can anyone explain why they are treated as operations and not attributes? Is it because properties in C# combine behavior with their primary purpose of storing state?

Next I create an object diagram using these classes and want to set the Run State of the objects, in particular the run state of the auto properties – which is where my objects store their state. I find I can’t set the RunState for operations, only for “variables”, i.e. attributes. So again, why were the auto properties treated as operations and not attributes?

In desperation I wrote an add-in to convert all the operations with “Property” stereotype to attributes, and now I can set their run state in an object diagram.

The snag is, unless I’m missing something, there seems to be no way of indicating a call to the setter of the auto property in a sequence diagram.

I’m beginning to think EA is equipped to do modeling of C# only if you discard some of the key  features of the language. Has anyone else encountered this kind of problem with EA and C#? Any solutions?

Is there any documentation from SPARX (or anyone else) that shows how to model C# effectively? (I can’t find any.)
« Last Edit: May 01, 2013, 01:24:53 am by phurst »

webfox

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: Problems with EA and C#
« Reply #1 on: May 29, 2013, 11:18:19 pm »
Auto-properties in .NET are operations. When using them in your code, the compiler automatically creates a private member variable (= attribute) for your convenience. You can easily check this out by inspecting the MSIL using e.g. .NET Reflector.

Therefore, semantically, auto-properties must be treated the same way as operations in UML models. If you want to model state, you will have no other option but to specify the attributes holding that state explicitly in your code. Unfortunately, this means that you cannot use auto-properties for that purpose.

Hope this helps.

Cheers,

Till

phurst

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Problems with EA and C#
« Reply #2 on: May 30, 2013, 02:43:00 am »
Yes it is truly obvious that the compiler creates a backing variable for the auto property. Obviously, it has to have somewhere to put the state  exposed by the auto property but it hides the backing variale from normal view because it wants the property to appear like a field.

Ignoring such implementation details, the auto property behaves like a field. It stores state and is getable and settable by direct assignment, without using getter and setter semantics. To a C# programmer an auto property is basically a field.

Given that, I don't see the justification for your comment "Therefore, semantically, auto-properties must be treated the same way as operations...". It looks like a field, it smells like a field, it quacks like a field, it should be treated as a field.

Modeling state is secondary to what I do, which is write software in C#. Eschewing auto properties just because EA can't handle them would let the tail very much wag the dog.

C# programmers use auto properties as a matter of course, and think of them as glorified fields. I'd be very surprised if you could find a single C# program of any size written in the last 4 years that does not use them. (There are some other reasons for using them, particularly in WPF view models, that I won't go into here.)

If SPARX are unwilling to accommodate auto properties in EA, then I clearly have to accept that. But it does make EA much less attractive to me as a modeling tool. I imagine the same is true of most C# programmers when they discover EA can't model auto properties in an intuitive way.

I like EA and use it a lot, but this issue is a real pain in the neck...

« Last Edit: May 30, 2013, 03:05:50 am by phurst »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Problems with EA and C#
« Reply #3 on: May 30, 2013, 04:10:08 pm »

webfox

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: Problems with EA and C#
« Reply #4 on: May 31, 2013, 05:41:15 pm »
Guys,

one can be divided over this one, but there is a difference, it is subtle, but it exists:

Auto-properties control access to the get and set operations independently, e.g. I could declare the get operation public and the set operation protected. So an auto-property is clearly more than just a field.

I agree with you that it is a pain in the neck that some .NET features are not entirely supported in EA. This is one of the drawbacks of a tool that supports more than one programming language.

Note that I do not categorically refuse auto-properties. I use them as well, yet selectively. All I am saying is that they are not fields and therefore cannot be treated as such in models (that was your original problem, wasn't it?). If you want to use them as state holders in models, well, you are screwed, at least for now. For you this might be an "unintuitive" way of handling them, for others it is not, that's my point.

So long,

Till