This is a migrated thread and some comments may be shown as answers.

Create Set/Get Property Customiztion

5 Answers 100 Views
Code Generation
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alisha
Top achievements
Rank 1
Alisha asked on 12 Mar 2011, 06:45 AM
Hi, is there a way to customize how the set/get property creates code?

Right now, if I have the following code...

private string _testProperty;


and I call the Set/Get Property, it generates the following....

public string TestProperty
        {
            get
            {
                return _testProperty;
            }
            set
            {
                _testProperty = value;
            }
        }


What I would like it to do instead is the following...

public string TestProperty
{
           get
           {
               return _testProperty;
           }
           set
           {
               if (_testProperty != value)
               {
                   _testProperty = value;
                   NotifyOfPropertyChange(() => TestProperty);
               }
           }
       }

Without me having to manually go and change it, is this at all possible, or is the "template" fixed?

5 Answers, 1 is accepted

Sort by
0
Deyan Varchev
Telerik team
answered on 16 Mar 2011, 02:30 PM
Hi Mark,

 Thank you for contacting us.Currently there is no way for you to customize the way "Create Set/Get Property" generates properties. However you can use a template in order to achieve this. Currently JustCode ships with predefined template for properties that you can expand by typing p and hitting Shift + Space. This will generate code for property similar to the one generated by the code generation.
 You can add your own templates and assign them acronyms that match your needs. This is done in JustCode's options - JustCode Menu | Options | Code Templates | C#. You can look at existing templates to see how to create one or read this blogpost.

 I prepared a code template that matches your needs :

public $1$ $2$
{
    get
    {
        return $3=SELECT_VARIABLE($1$)$;
    }
    set
    {
        if($3$ != value)
        {
            $3$ = value;
            NotifyOfPropertyChange(() =>  $2$);
        }
    }
}

 You can simply add it to your templates and assign it an acronym to be able to easily expand it - i.e. np.
 
 I hope this will help. If you have any other problems or questions, please, feel free to contact us again.

Kind regards,
Deyan
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Steele
Top achievements
Rank 1
answered on 04 Dec 2011, 07:40 AM

Along this same line of thinking,
I want to change the template used for expanding Add Stubs for Required Members (Ctrl +K, Ctrl + M)
This would be IMMENSELY useful to me.

For example:
I have this Interface:

Imports System.ComponentModel 
Imports System.Runtime.Serialization
Public Interface ICategoryInfo
    Inherits INotifyPropertyChangingINotifyPropertyChangedISerializable

It expands a bunch of stubs that I don't really like for one...
Secondly, this is entirely possible to implement 99% of what is needed, not just stubs with a proper Template.
Just Code currently fills in the Properties like this:
        Public Property Category() As ICategory Implements ICategoryInfo.Category
            Get
                ' TODO: Implement this property getter
                Throw New NotImplementedException()
            End Get
            Set
                ' TODO: Implement this property setter
                Throw New NotImplementedException()
            End Set
        End Property

VS2010's default behavior isn't much better. Which is just silly, I'd rather get a fully populated result like this:
        Public Property Category() As ICategory Implements ICategoryInfo.Category
             Get
                Return _Category
            End Get
            Set(value As ICategoryInfo.Category)
                 If Not _Category.Equals(value) Then
                    NotifyPropertyChanging("Category")
                    _Category = value
                    NotifyPropertyChanged("Category")
                End If
            End Set
        End Property
        Private _Category As ICategory

I can do this with FAR less typing/cutting/pasting if I use the VS2010 Snippets I've edited to do it by typing prop tab tab then type Category. I would also expect Implements Expansion to include the Imports/Usings required to Implement the Interface.
Also ISerializable it VERY Painful to have to type for every Class... think hundreds of Classes that need this expansion:
        Protected Sub New(Info As SerializationInfo, Context As StreamingContext)
            With Info
                _ID = .GetValue("ID"GetType(Object))
                _Created = .GetValue("Created"GetType(DateTime))
                _Modified = .GetValue("Modified"GetType(DateTime))
                _Description = .GetValue("Description"GetType(String))
                _Title = .GetValue("Title"GetType(String))
                _Categories = .GetValue("Categories"GetType(IEnumerable(Of ICategory)))
                _Enclosures = .GetValue("Enclosures"GetType(IEnumerable(Of IEnclosure)))
                _Link = .GetValue("Link"GetType(Uri))
                _Permalink = .GetValue("Permalink"GetType(Uri))
                _Data = .GetValue("Data"GetType(XElement))
            End With
        End Sub
        Public Sub GetObjectData(Info As SerializationInfo, Context As StreamingContextImplements System.Runtime.Serialization.ISerializable.GetObjectData
            With Info
                .AddValue("ID", _ID)
                .AddValue("Created", _Created)
                .AddValue("Modified", _Modified)
                .AddValue("Description", _Description)
                .AddValue("Title", _Title)
                .AddValue("Categories", _Categories)
                .AddValue("Enclosures", _Enclosures)
                .AddValue("Link", _Link)
                .AddValue("Permalink", _Permalink)
                .AddValue("Data", _Data)
            End With
        End Sub

Just Code, (un)helpfullygives me this:
        Public Sub GetObjectData(info As SerializationInfo, context As StreamingContextImplements ISerializable.GetObjectData
            ' TODO: Implement this method
            Throw New NotImplementedException()
        End Sub

with NO Constructor :-(

There is a way to do this with T4 and Snippets, but that seems like a silly solution if I could just edit the existing templates in JustCode.
I scramble for ANY solution, even resorting to T4 when I must.

As far as I can tell, there is no way to do this with a Code Template since it involves looping.
I understand you want them in a DLL for various reason, but having the ability to override things like this are exactly the reason I look to JustCode for solving.

Thanks for a great tool, it can be even better with options like this.

Steele Price
Microsoft MVP

0
Ivo Bratoev
Telerik team
answered on 12 Dec 2011, 05:30 PM
Hello Steele,

Let me start with that I completely get you. I have felt the same several times - I needed some special fix or refactoring that is too specific to be part of JustCode. What we do locally is just write our own. Internally, we don't have a template for these feature that can be edited. What we have is a nice set of API-s that you can use to build your own fix, refactoring or code generation.
The good news is that we plan to open up this API-s so you can use it to build your own specific features. The bad one is that we have no set in stone plans when we will release it to the public. What I can offer you is if you wish you can become a beta tester for this. If you are interested drop me a mail at ivaylo.bratoev@telerik.com.
About your specific examples, both of them are too specific to integrate in JustCode. But I really like the idea to generate a serialization constructor when implementing ISerializable. I added an item in PITS where you can vote or comment: http://www.telerik.com/support/pits.aspx#/public/justcode/8839.

Regards,

Ivo Bratoev
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Steele
Top achievements
Rank 1
answered on 12 Dec 2011, 06:07 PM
trying to send a reply...
your email address is getting rejected :-(
0
Ivo Bratoev
Telerik team
answered on 13 Dec 2011, 05:40 PM
Hi Steele,

 Hm... strange. I will check on our side. In the mean time I have already sent you a test mail.

Greetings,
Ivo Bratoev
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
Code Generation
Asked by
Alisha
Top achievements
Rank 1
Answers by
Deyan Varchev
Telerik team
Steele
Top achievements
Rank 1
Ivo Bratoev
Telerik team
Share this question
or