Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

XML Feeds

Tags: generate

All the LessThanDot Journals

VB.Net and Resharper and the Property generation thing

by chrissie1


Permalink 27 Aug 2008 01:15 , Categories: Microsoft Technologies, VB.NET Tags: generate, property, reshaper, vb.net

In my last blogpost I talked about property generation and how the naming for the generated property seemed a bit strange. Luckily the guys from JetBrains (Ilya Ryzhenkov) read our blog ;-) and gave a comment.

So I acted on it and found what he was talking about. I always use the prefix with underscore and have camelcasing because I have NHibernate setup like that too. And it becomes a habit. So I just had to add the underscore to the namingstyle, like shown in the picture.

And then this

  1. Private _color As String

Quickly turns into this.

  1. Public Property Color() As String
  2.   Get
  3.     Return _color
  4.   End Get
  5.   Set (ByVal value As String)
  6.     _color = value
  7.   End Set
  8. End Property

Isn’t that cool?

Leave a comment »Send a trackback » 372 views

Let Resharper do the heavy lifting for you.

by chrissie1


Permalink 26 Aug 2008 04:54 , Categories: Microsoft Technologies, VB.NET, C# Tags: equals, generate, gethashcode, properties, resharper, vb.net

Reshaper is a cool tool that can save you plenty of time (unless you start blogging about it of course ;-))

I will give the examples in VB.Net but this will also work in C#.

We all know what a DomainEntity looks like, right?

Ok so here is a simple example.

  1. Public Class person
  2.     Private _id As Guid
  3.     Private _name As String
  4. End Class

First thing to do is create properties for it.
For this we use the Reshaper generate option, which is available via the shortcut Alt+Ins or via the menu Resharper->Generate.

This brings up the following contextmenu:

We choose Read-only properties:

And get this as a result:

  1. Public Class person
  2.     Private _id As Guid
  3.  
  4.     Overridable Public ReadOnly Property _idProperty() As Guid
  5.         Get
  6.             Return _id
  7.         End Get
  8.     End Property
  9.  
  10.     Private _name As String
  11. End Class

Not ideal, but a timesaver none the less.

We do the same but then as a normal property for name.

And get this as a result:

  1. Public Class person
  2.     Private _id As Guid
  3.  
  4.     Overridable Public ReadOnly Property _idProperty() As Guid
  5.         Get
  6.             Return _id
  7.         End Get
  8.     End Property
  9.  
  10.     Private _name As String
  11.  
  12.     Overridable Public Property _nameProperty() As String
  13.         Get
  14.             Return _name
  15.         End Get
  16.         Set (ByVal value As String)
  17.             _name = value
  18.         End Set
  19.     End Property
  20. End Class

But now for the most important part. The overriding of the Equals and GetHashCode methods.

So we do Alt+Ins again and choose Equality members and get this dialog. We choose name and some other things that seem useful.

And we get this as a result:

  1. Public Class person
  2.     Implements IEquatable(Of person)
  3.     Private _id As Guid
  4.  
  5.     Overridable Public ReadOnly Property _idProperty() As Guid
  6.         Get
  7.             Return _id
  8.         End Get
  9.     End Property
  10.  
  11.     Private _name As String
  12.  
  13.     Public Overloads Function Equals (ByVal obj As person) as Boolean
  14.         If ReferenceEquals (Nothing, obj) Then Return False
  15.         if ReferenceEquals (Me, obj) Then Return True
  16.         Return Equals (obj._name, _name)
  17.  
  18.     End Function
  19.  
  20.     Public Overloads Overrides Function Equals (ByVal obj As Object) as Boolean
  21.         If ReferenceEquals (Nothing, obj) Then Return False
  22.         if ReferenceEquals (Me, obj) Then Return True
  23.         If Not Equals (obj.GetType(), GetType (person)) Then Return False
  24.         Return Equals (DirectCast (obj, person))
  25.  
  26.     End Function
  27.  
  28.     Public Overrides Function GetHashCode() as Integer
  29.         If _name IsNot Nothing Then Return _name.GetHashCode()
  30.         Return 0
  31.  
  32.     End Function
  33.  
  34.     Overridable Public Property _nameProperty() As String
  35.         Get
  36.             Return _name
  37.         End Get
  38.         Set (ByVal value As String)
  39.             _name = value
  40.         End Set
  41.     End Property
  42. End Class

Cool, right, in a few clicks we got a whole lot of code. And a whole lot of working code to boot. It isn’t pretty, but there are things you can do about that.

There is also Formatting members which creates a ToString.

Which creates something like this.

  1. Public Overrides Function ToString() as String
  2.         Return string.Format ("_id: {0}, _name: {1}", _id, _name)
  3.     End Function

I don’t know who came up with the names but they should look at Eclipse, it is much clearer there. And the property names aren’t what I would expect, a return to hungarian comes to mind.

4 comments »Send a trackback » 475 views