Book a Demo

Author Topic: How do you model an attribute for a method/op?  (Read 4095 times)

Brad Hehe

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
How do you model an attribute for a method/op?
« on: September 10, 2008, 07:57:43 am »
I see a class can have attributes... which I thought were equivalent to C# class 'properties'. But then I found you couldn't mark an attribute as abstract - which I needed to model an abstract property in that case. So I looked to methods (ugh - I'll just make it an abstract function - a total hack/work-around and definitely NOT desirable)...

I then noticed later that you can make a method and give it a stereotype of property so perhaps that is the "right way" to do abstract properties (or properties in general) and I shouldn't be using attributes at all (for my particular purposes)...

So I thought I'm misusing attributes in this context but the EA help does indicate that they are essentially 'internal' members of the class (i.e. Customer class / CustomerName attribute)... And i can see where there is a checkbox for Property but the darn thing is never enabled to click!! I'm guessing  this is key to code gen creating getting a "m_customerName" as a private member and a public Getter/Setter created for the true property exposure...  

But this thought process isn't going so great when I can't even check the Property check-box for an Attribute and I have no clue what governs enabling it for selection...

So now my question becomes how do you add Attributes to a class as well as a method/property? I.e. the [Serializable] attribute for a class...

And for a method/property being decorated - I have a property that I want to mark as a dependency for an IoC container to provide via Dependency (Property Setter) Injection... Which means adding a C# attribute to the Method/Property declaration... Which I see no support for in this tool....

Help!
« Last Edit: September 10, 2008, 08:13:57 am by bhehe »

jkorman

  • EA User
  • **
  • Posts: 99
  • Karma: +0/-0
    • View Profile
Re: How do you model an attribute for a method/op?
« Reply #1 on: September 10, 2008, 11:17:42 pm »
Brad, try the following

Add an attribute to your class and select the save button
The property checkbox will activate allowing you to select this attribute as a property. Check the box.
A Create Property Implementation will appear. The Language should be set to
C#, assuming you've set the language in the class, otherwise select the C# button.
Note that you may now set this property to Abstract as you wish.

Jim

Richard Deverson

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: How do you model an attribute for a method/op?
« Reply #2 on: September 12, 2008, 07:25:37 pm »
Hi Jim,
You wouldn't by any chance know how one makes a property read only, when you have not created an attribute?
I.e. I have created an operation, set the stereotype to property. The property appears in my class when I generate C# code, but it has a setter and a getter. I just want a getter. I know how to do this if I have an attribute, but I don't want to have an attribute.

Richard
« Last Edit: September 12, 2008, 07:26:05 pm by richli808 »

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How do you model an attribute for a method/op?
« Reply #3 on: September 12, 2008, 10:32:24 pm »
Hi Richard,

That's not necessarily the 'best' way to go about this, but C# does not forbid the practice. So let's see if we can make it work.

Create a 'normal' property the way Jim et al have described. Ensure that you have set the property to read only (i.e. a getter, but no setter), and save the property. Now highlight the operation that corresponds to the property, but not the entire class. Open the Tagged Values window, or make it visible if you have it docked.

You should see two tagged values. One (attribute_name) points back to the attribute that the property sets and gets. The other (readonly) should have a value of true.

I suspect without proof that you should create the readonly tagged value for your 'operation-only' property.

It's anyone's guess how, or even if, EA will handle your property when you generate code. Please post back and let us know.

David
No, you can't have it!

Brad Hehe

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: How do you model an attribute for a method/op?
« Reply #4 on: September 16, 2008, 08:01:33 am »
Excellent. I did eventually figure out how to make a true "property" in the class via that technique... the whole "saving it first" part is what wasn't exactly intuitive for me...

Now I still have the question as to "decorating" a class/method with an "attribute" in the C# language sense...

i.e.

[highlight][Serializable]      <---- C# Attribute[/highlight]
Public Class MyModelClass
{

    public bool IsSomethingTrue;     <------ EA "Attribute"

    [highlight][MyMethodBasedAttribute]    <------ C# Attribute[/highlight]
    public void MyModelMethod() {};        <----- EA "Operation"

}


So how would I go about adding that to my model? Does UML even support this, much less EA?

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: How do you model an attribute for a method/op?
« Reply #5 on: September 16, 2008, 08:09:55 am »
You need to add a tagged value "Attribute" with the value of your attribute eg. "[Serializable]".

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: How do you model an attribute for a method/op?
« Reply #6 on: September 16, 2008, 12:56:09 pm »
Which is documented somewhere, and has been answered here (in the forum) before.

The main thing Brad, is that you are moving forwards again.
No, you can't have it!