Naming conventions
editEnable the conventions
The conventions are opt-in. Enable them in the .editorconfig file of your solution:
[*.cs]
taggedvalues.infer_tags_from_names = trueMost ids then need no attribute:
public sealed class Customer
{
public Guid Id { get; set; } // a CustomerId, from the name of the type
public Guid AccountId { get; set; } // an AccountId, from the name of the property
}
public sealed class CustomerService
{
public void Delete(Guid customerId) { } // a CustomerId, from the name of the parameter
public void Archive(Customer id) { } // a CustomerId, from the type of the parameter
public void M(Customer customer)
{
Delete(customer.Id); // ok
Delete(customer.AccountId); // MFTV0002
}
}The conventions
- A property or a field named
Idtakes the name of its declaring type:IdonOrderis an"OrderId", andIdon the interfaceIEntityis an"EntityId". When the member is inherited, it is also an id of every type between the receiver and the declaring type. When it overrides or implements anId, it is also an id of the base type or of the interface. - A property, a field, or a parameter named
xxxIdtakes its name:projectIdis a"projectId", which is the same tag as"ProjectId", as tags are compared ignoring case. Thes_,t_, andm_prefixes and the leading underscores of fields are ignored:s_projectIdand_projectIdare both"projectId". - A parameter named
idtakes the name of its type:void Load(Order id)is an"OrderId", andvoid Load(OrderId id)is an"OrderId"too. A parameter namedidwhose type is a primitive, astring, aGuid, an enum, or another type of theSystemnamespace such asInt128orDateTimeOffsetis not tagged, as its type does not say what it identifies.
Limits
- Explicit tags always win over the conventions.
- Collections and indexers are not tagged by convention.
- The conventions only apply to the declarations of the project, not to the declarations of referenced projects or assemblies. Tag the members a referenced project exposes explicitly when its consumers must see the tags.
Rules specific to the conventions
- Two types with the same name in different namespaces give their
Idthe same tag, for instanceSales.Order.IdandBilling.Order.Id. The analyzer reports it (MFTV0006): add an explicit tag to at least one of them. - A [ValueTag] attribute that declares the tag the convention already infers is redundant (MFTV0007): the code fix removes it.