| Severity | Warning |
| Reported by | PostSharp |
| Message | The name of type <Type> does not match the "<Pattern>" pattern of the [NamingConvention] constraint applied to this type or to one of its base classes or interfaces. |
Cause
The NamingConventionAttribute constraint, applied to a class or an interface, enforces a naming convention: the name of this type, and the names of the classes derived from it or implementing it, must match a pattern. A pattern that contains * is a wildcard pattern, which must match the whole name; a pattern that starts with regex: is a regular expression, which only needs to match a part of the name.
PostSharp reports AR0110 on each type whose name does not match the pattern. It checks the short name of the type, without its namespace or the names of the types that contain it.
AR0110 is a warning by default. Its severity comes from the Severity property of the constraint, which you can set to Error.
In the following example, the types that implement IRepository must have a name that ends with Repository, but CustomerStore does not.
using System;
using System.Collections.Generic;
using PostSharp.Constraints;
namespace Errors.AR0110_Error;
public record Customer( int Id, string Name );
// The name of every type that implements IRepository must end with "Repository".
[NamingConvention( "*Repository" )]
public interface IRepository
{
Customer? Find( int id );
}
// The name of this class does not end with "Repository".
public class CustomerStore : IRepository
{
private readonly Dictionary<int, Customer> customers = new() { [1] = new Customer( 1, "Contoso" ) };
public Customer? Find( int id ) => this.customers.GetValueOrDefault( id );
}
public static class Program
{
public static void Main()
{
IRepository repository = new CustomerStore();
Console.WriteLine( $"{repository.GetType().Name} found customer 1: {repository.Find( 1 )?.Name}" );
}
}
warning AR0110: AR0110_Error.cs(17,14): The name of type Errors.AR0110_Error.CustomerStore does not match the "*Repository" pattern of the [NamingConvention] constraint applied to this type or to one of its base classes or interfaces.
Resolution
- Rename the type so that its name matches the pattern.
- When the convention is too strict, change the pattern of the constraint. For example, a regular expression such as
regex:(Repository|Store)$accepts two suffixes.
In the following example, CustomerStore is renamed to CustomerRepository.
using System;
using System.Collections.Generic;
using PostSharp.Constraints;
namespace Errors.AR0110_Fixed;
public record Customer( int Id, string Name );
// The name of every type that implements IRepository must end with "Repository".
[NamingConvention( "*Repository" )]
public interface IRepository
{
Customer? Find( int id );
}
// The class is renamed to respect the naming convention.
public class CustomerRepository : IRepository
{
private readonly Dictionary<int, Customer> customers = new() { [1] = new Customer( 1, "Contoso" ) };
public Customer? Find( int id ) => this.customers.GetValueOrDefault( id );
}
public static class Program
{
public static void Main()
{
IRepository repository = new CustomerRepository();
Console.WriteLine( $"{repository.GetType().Name} found customer 1: {repository.Find( 1 )?.Name}" );
}
}
CustomerRepository found customer 1: Contoso
See Also
Reference
NamingConventionAttribute
SeverityType
Other Resources