ReSharper for Data Access
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.
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:
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 invocation and then press Alt+Enter 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, but lets you change it:
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:
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
You will be presented with a dialog 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:
Equals()overloads and – optionally – the
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
== 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:
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.
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 Refactor This shortcut Ctrl+Shift+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
And here’s what ReSharper produces:
With the original call replaced by
GetDatabase() of course.