ReSharper for Data Access

Skip to end of metadata
Go to start of metadata

Redirection Notice

Icon

Regardless of which database and driver/ORM you use, the tasks for setting up data access in you application are very similar. From the creation of your entities, to the setting up of your repository, ReSharper is there to help you get the job done better and faster.

Entity Creation

Entities are at the core of working with data access, as they store the data from your database. There are many ways to create an entity quickly in ReSharper. One way is to use the Alt+Insert shortcut in the Solution Explorer, which will give you the familiar Generate menu:

Choosing Class in the above menu, you get prompted to give the class a name

And you automatically get a class called Person in a file called Person.cs.

As an alternative, you can get ReSharper to infer the class name and its members from usage. For example, you can type in a class declaration and then use a context menu to generate the class:

In much the same manner, ReSharper can help you generate class members. For example, you can try to assign a property called Name and ReSharper will offer the option to create the property:

ReSharper correctly guesses the type, and offers you options with respect to how you want the property implemented:

Another way to build your entity is to use templates. Since ReSharper automatically imported the Visual Studio templates upon installation, you can use, for example, the prop template to quickly add properties to your entity:

Entity Build-Up

Though in many cases you’ll want to keep your entities as simple as possible, there are situations when you need to add additional features to your entities. For example, an initializing constructor makes your entity easier to use. Luckily, for an occasion such as this, we have ReSharper’s Generate Menu (Alt+Ins). This menu lets you add various features to your existing type.

Let’s say you want to add a fully initializing constructor to our Person type. To do so, simply press Alt+Insert while in the class, and choose Constructor.

You will be presented with a dialogue asking which properties you want to initialize:

And if you pick all the properties and press Finish, your class will acquire the following generated constructor:

If you want your entity to be comparable and easily hostable in collections such as HashSet<T>, you need to implement equality members – a difficult task to get right! Luckily, once again, ReSharper’s Generate menu has a corresponding option, which lets you implement

  • Equality operators == and !=
  • Correct Equals() overloads and – optionally – the IEquatable<T> interface
  • A good GetHashCode() method

You can trust ReSharper to take special care with all your properties – it will perform all the right null checks, and will do property comparisons either with Equals() or == depending on the types being compared.

One last example (there are many more options available) is the automatic creation of a ToString() method. Once again, you get to choose which properties take part, and after you do, ReSharper produces a neat implementation:

Please note that these features are available to you even if your ORM has built the basic entity classes for you. Since the generated classes are typically partial, nothing prevents you from creating another part of the class and running Generate actions on it.

Infrastructure

After you have prepared your entities, it is time to set up the infrastructure useful for working with the database. The specifics of this depend on the ORM or driver you use, of course, but here we’ll look at the use of NoRM and MongoDB.

Say you’re starting out with the following code:

The first thing we can do is refactor out the database connection data. To do that, let’s use ReSharper’s In-Place Refactoring (Ctrl+Alt+R) with the new statement selected and choose the Extract Method option:

Consequently, ReSharper offers you the options for configuring the method. Let’s call it GetDatabase():

And here’s what ReSharper produces:

With the original call replaced by GetDatabase() of course.

  • No labels