Skip to content

ForEvolve/vs-snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Visual Studio snippets

C# Snippets

ArrangeActAssert

By typing a + TAB + TAB (or aaa + TAB for long) you end up with an empty failing test method body.

// Arrange
$end$

// Act


// Assert
throw new NotImplementedException();

XUnitFact

By typing fa + TAB + TAB (or fact + TAB for long) you end up with an empty failing test method.

The part be_a_test_case is selected and ready to be renamed. Once you renamed your test method and pressed Enter, the cursor jump under // Arrange to keep it as productive as possible.

[Fact]
public void Should_be_a_test_case()
{
    // Arrange


    // Act


    // Assert
    throw new NotImplementedException();
}

For an async test case, by typing af + TAB + TAB (or afact + TAB for long) you end up with an empty failing test method that return an async Task.

The same selection/cursor location behaviors applies.

[Fact]
public async Task Should_be_a_test_case()
{
    // Arrange


    // Act


    // Assert
    throw new NotImplementedException();
}

GuardClause (field)

I think that this is by far my favorite snippet.

If you define your field following the private readonly ISomeService _fieldName; and inject it in the controller like this public MyController(ISomeService fieldName){}, this snippet is for you!

public class MyClass
{
    private readonly ISomeService _fieldName;
    public MyClass(ISomeService fieldName){
        // Place your cursor here
    }
}

From there, copy (ctrl+c) fieldName, place your cursor in the constructor body and type g + TAB + TAB (or guard + TAB for long) then paste (ctrl+v). You should endup with the following in less than a second:

public class MyClass
{
    private readonly ISomeService _fieldName;
    public MyClass(ISomeService fieldName){
        _fieldName = fieldName ?? throw new ArgumentNullException(nameof(fieldName));
    }
}

The complete sequence (this should take ±2sec):

  1. dbl-click OR ctrl+click fieldName
  2. ctrl+c
  3. click inside the constructor body
  4. Type g
  5. Hit TAB twice
  6. ctrl+v
  7. ENTER

GuardClause (old)

Create a guard clause for an injected dependency.

if ($paramName$ == null) { throw new ArgumentNullException(nameof($paramName$)); }

GuardClause (property)

Create a guard clause for an injected dependency using the 2017 construct. This snippet offers two parameters: the property name and the parameter name.

$propName$ = $paramName$ ?? throw new ArgumentNullException(nameof($paramName$));

GuardClause (string)

Create a guard clause for an injected string dependency.

if (string.IsNullOrWhiteSpace($paramName$)) { throw new ArgumentNullException(nameof($paramName$)); }

ShouldBeTested

Create a failing XUnit test method named Should_be_tested. Shortcut: sbt.

[Fact]
public void Should_be_tested()
{
    throw new NotImplementedException();
}

Vertical Slice Architecture

Here are a few snippets that works great when implementing a Vertical Slice using the AutoMapper, FluentValidation, and MediatR stack.

All of those snippets should be used inside another class like:

public class MyUseCaseOrMyRazorPage
{
    use the snippet here
}

Vertical Command (no return value)

Creates all of the classes that are needed for a vertical slice command scenario (that does not return a value).

Generated classes:

  • Command
  • MapperProfile
  • Validator
  • Handler

Vertical Command with a return value

Creates all of the classes that are needed for a vertical slice command scenario that returns a value.

Generated classes:

  • Command
  • Result
  • MapperProfile
  • Validator
  • Handler

Vertical Query

Creates all of the classes that are needed for a vertical slice query scenario.

Generated classes:

  • Query
  • Result
  • MapperProfile
  • Validator
  • Handler

Vertical Razor Pages Form (Query and Command)

Creates all of the classes that are needed for a vertical slice query and command scenario inside a Razor Page (like an Edit page).

Generated classes:

  • Query
  • Command
  • CommandResult
  • MapperProfile
  • QueryValidator
  • CommandValidator
  • QueryHandler
  • CommandHandler

Vertical Razor Pages Query-only

Creates all of the classes that are needed for a vertical slice query-only scenario inside a Razor Page (like an Index page).

Generated classes:

  • Query
  • Result
  • MapperProfile
  • Validator
  • Handler

JavaScript Snippets

JSModuleDomRdy

Allow to create a jQuery ready JavaScript module.

; (function ($, window, document, undefined) {
    "use strict"
    $(function () {
        // DOM READY CODE HERE...
        ~end~
    });
}(jQuery, window, document));

Releases

No releases published

Packages

No packages published