ReSharper 2023.1 Help

ReSharper for Data Access

Regardless of which database and driver/ORM you use, the tasks for setting up data access in your 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 with the list of available file templates:

'ReSharper for data access' tutorial

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

'ReSharper for data access' tutorial

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 invocation and then press Alt+Enter to generate the class:

'ReSharper for data access' tutorial

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 for data access' tutorial

ReSharper correctly guesses the type, but lets you change it:

'ReSharper for data access' tutorial

Another way to build your entity is to use live 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:

'ReSharper for data access' tutorial

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+Insert. 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, press Alt+Insert while in the class, and choose Constructor:

ReSharper for Data Access

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

ReSharper for Data Access

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

public Person(string name, int age) { Name = name; Age = age; }

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 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:

public override string ToString() { return string.Format("Name: {0}, Age: {1}", Name, Age); }

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:

void AddPerson() { using (var m = new Mongo("personnel", "localhost", "27017", string.Empty)) { var coll = m.GetCollection<Person>("people"); var p = new Person("Jack", 20); coll.Insert(p); } }

The first thing we can do is refactor out the database connection data. To do that, let’s use ReSharper’s Refactor This shortcut Control+Shift+R with the new statement selected and choose the Extract Method option:

'ReSharper for data access' tutorial

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

'ReSharper for data access' tutorial

And here’s what ReSharper produces:

private static Mongo GetDatabase() { return new Mongo("personnel", "localhost", "27017", string.Empty); }

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

Last modified: 21 July 2021