0

Before I get started please be warned I'm new to Resharper and may well be overlooking something.

I'm attempting to use Resharper to automate some of my boiler plate code. In particular I want to automate copying of DI constructor parameters to private fields. I'm for the most part following the patterns in Microsoft's Razor Pages examples.

public class CreateModel : PageModel
{
    private readonly AppDbContext _db;

    public CreateModel(AppDbContext db)
    {
        _db = db;
    }
}

Resharper does this, all good so far. But I also want to generate a throw on null propagation like so.

public class CreateModel : PageModel
{
    private readonly AppDbContext _db;

    public CreateModel(AppDbContext db)
    {
        _db = db ?? throw new ArgumentNullException(nameof(db));
    }
}

I've tried looking at Resharper's Null Checking options, Templates Explorer, and every other option I can find that sounds relevant. But I can't seem to find any way to modify the behavior or create my own. Code snippets look right except they generate on the cursor. Wrap snippets don't seem right either.

So my question is, how can I create this behavior in Resharper (or plain old Visual Studio)?

2
  • jetbrains.com/help/resharper/… generates ctors optionally with null check, maybe not exactly what you want. have a look Commented Mar 20, 2018 at 21:36
  • @DennisKuypers I might have to give up on the null-coalescing operator and use Resharper in that way to generate the null check and assign separately. Then ignore VS's suggestion to combine them. Or I guess I could let VS combine them as another step. Thank you for your reply. Commented Mar 22, 2018 at 13:02

1 Answer 1

2

Alright this is as close as I can get. I can't accomplish this in one hotkey but two is close enough.

Lets start off without our field or null check.

public class CreateModel : PageModel
{
    public CreateModel(AppDbContext db)
    {

    }
}

Using ctrl+. on db will result in an option to generate a readonly field to store the value. Press enter to accept that. Resharper/VS will now give you this.

public class CreateModel : PageModel
{
    private readonly AppDbContext _db;

    public CreateModel(AppDbContext db)
    {
        _db = db;
    }
}

Next use ctrl+. again on db. You're cursor should still be there. It will offer to generate the null check. I assumed that would use an if statement, wrong! Resharper will edit the assignment and add a null propagating operator into throw. You'll wind up with this.

public class CreateModel : PageModel
{
    private readonly AppDbContext _db;

    public CreateModel(AppDbContext db)
    {
        _db = db ?? throw new ArgumentNullException(nameof(db));
    }
}

Which is exactly what we wanted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.