Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« VB.Net and Resharper and the Property generation thing.Net has a Set implementation since the 3.5 framework. Use it. »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

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.

About the Author

Christiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
Social SitingsTwitterLinkedInHomePageLTD RSS Feed
1542 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

4 comments

Comment from: SQLDenis [Member] Email
Interesting, I should try Resharper one of these days. Have you ever tried CodeSmith? Maybe you can do a post about your favorite .NET tools
26/08/08 @ 05:08
Comment from: chrissie1 [Member] Email
I tried CodeSmith many moons ago, I can't even remember what for. But like most code generation it lacks a certain thing. It's mostly generate and refactor. But at least it save you time.
26/08/08 @ 05:13
Comment from: Ilya Ryzhenkov [Visitor] · http://resharper.blogspot.com
****-
You have that strange generated names, because you didn't configure your naming style in options. Tell ReSharper that you prefix fields with underscore, and it will do much better job ;)
26/08/08 @ 14:56
Comment from: chrissie1 [Member] Email
Aha, thanks for that.
27/08/08 @ 00:57

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)