Skip to content

Productivity

Anrijs Vitolins edited this page May 21, 2021 · 4 revisions

Developer productivity features

Logging

Much of it is described in separate Logging page. In essence, when you have configured logging in your application/api, package functionalities picks that up and are able to write debug and diagnostics log statements to it (in development mode or trace/verbose mode.)

Package emits database connection, transaction open/close events, Query execution events (with timing), so developers can easily see where something is not working as expected by looking over logged statements. It also add up Hashes for those objects to see whether object reuse is in place or vice-versa - new object created during business operation (DI problems).

Getting information during development

Package is using many features targeted to ease development and debugging when using package functionalities.

SQL Statement retrieval

During debugging, there is a property RealSqlStatement on IQuery or ICommand through which developer can get full resulting SQL code, which can be copied into SQL Management Studio for manual run: RealSqlStatement property

Timings for SQL statement execution

The ICommandQueryContext has exposed property ExecutionTime (TimeSpan), which stores timing for the last ran SQL statement (either Query or Command). You can use it right after executing SQL to read it and save somewhere for any purpose - performance statistics, additional logging, whatever the need may be. Note, that if you have debugging logging - the log statement shows that timing in human readable format (not technically usable).

Intellisense

Package code is documented on class, method and parameters, so Visual Studio Intellisense picks that up providing quick help on any of those usages:

Debug object information

Package classes uses development feature to add default Debug information for classes to display when doing debugging code sessions. This gives you insight into most relevant information on objects in debug "Local"/"Watch" windows. Debug Window information

Sure, it is not that important for logic classes. Really it shines for POCO objects, where instead of seeing <Person object> you would immediately get Name Surname (ID: 77), so you do not have to open object properties and search for this data.

But as this is pure your application concern, I just may give you hints on how to achieve this when creating these POCO classes.

using System.Diagnostics; // Namespace for this feature

// This attribute is saying, use "DebuggerDisplay" property on this class (nq = No Quotes!) to show in debug window
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    // This does the magic of immediate diplaying of your chosen properties
    // in debug window for this object instance.
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private string DebuggerDisplay => 
        $"{this.FirstName} {this.LastName} (ID: {this.Id})";
}    

If you use provided templates (see below), then there is a template of creating this boilerplate POCO class with these features pre-added.

Templates, Snippets

As Dapper.CQRS involves some boilerplate files to be created, it could become handy just to predefine a template for some of them. Here are information and references to prepared template files for you to use when developing with this package

General information on templates

Visual Studio stores default C# new item templates in this directory (in case you want to modify those):

C:\Program Files (x86)\Microsoft Visual Studio\2019\<Flavor>\Common7\IDE\ItemTemplates\CSharp\Code\1033

Custom user templates are to be prepared in specific way as a ZIP file and placed in this path:

%USERPROFILE%\Documents\Visual Studio 2019\Templates\ItemTemplates

Provided templates

Code repository contain few predefined ZIP templates, which you can download and put in custom user template folder (see above). Suggestion is to add these ZIP files into new subfolder, so they are not shuffled into existing item templates:

Template copy location

After you do this way - [Visual Studio restart required] they should appear as available choice for "Add new item...".

Visual Studio New Item

Location of templates - REPO

IQuery templates

There are three IQuery implementation templates provided:

Single record query

To be used when you are interested into getting one particular record from database (like record by ID). After creating file from template - change DbPoco object to actual object with necessary project/namespace references.

Multiple record query

To be used when you are interested into getting multiple records (list of records) from database, based on some filtering through passing filter parameter(s) to class. After creating file from template - change DbPoco object to actual object with necessary project/namespace references.

Add record query

To be used when you are interested into getting all records from concrete database table or view. This class does not expect any parameters, so constructor is omitted. After creating file from template - change DbPoco object to actual object with necessary project/namespace references.

ICommand templates

There are three templates to create boilerplate classes for each of data modification operations:

  • INSERT
  • UPDATE
  • DELETE

POCO class

Template to create database POCO class with Debug Information features added.

XUnit templates

Few templates to create:

  • DatabaseTestsBase class

A class in your test project, which serves as a base class for integration/assembly tests to inherit from. Also adds another class of defining collection attribute for xUnit test collection feature. This template needs to be used only once, as only one base class should exist in test project.

  • Query/Command Syntax tests

A two test classes in one file, testing SQL statements syntax in your project. There should be one such class in your test project.

  • Data tests

A test class with two tests - contract/table matching test and simple write/read/compare test. This template can be used multiple times for each of your database/Contract object pairs.

Modifying templates

In case your project needs something specific or you want to add some things in template to be available right away - you can modify these templates locally.

For this just open ZIP file and modify contents of *.CS file there. Then put modified file back to ZIP, restart VS and you will get your modified version.

Code generation

TBD - I am still exploring possibility to generate C# code from database structures.