autofac / autofac/Autofac.Analyzers

Analyzer Proposal - Delegate Registration should have an As<TService>

Open
#3 7 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C#
Stars
1
Forks
1
PR merge metrics
No merged PRs in 30d

Description

Based on the current Autofac guidance, [here](https://autofac.readthedocs.io/en/latest/best-practices/index.html#use-as-t-in-delegate-registrations), delegate registrations should have an ``As`` call to inform the limit type.

## Proposed Behaviour

Where a registration is declared from a delegate, using one of the two ``Register`` extension methods, if that same registration is determined to not have an As call, a warning will be raised.

For example:
```csharp
class MyClass : IMyService { }

interface IMyService { }

static void Main(string[] args)
{
var builder = new ContainerBuilder();

// Warning Generated on this registration call.
builder.Register(c => new MyClass());

// No Warning Generated on this registration call.
builder.Register(c => new MyClass()).As();
}
```

## Possible Intelligent Detection

- [ ] Only raise the warning if the class derives from an abstract class or interface?

@nblumhardt, @tillig for comments.

Contributor guide

Open the contributing guide

Research direction

Start with the linked Autofac guidance on As in delegate registrations, then inspect the analyzer's handling of the two Register extension methods. Define the warning for registrations without an As call, including whether abstract classes and interfaces affect detection; done means the described examples produce and avoid the warning as specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.