ForeignKeyAttribute: can we have a params string[] constructor?
Since C# 6, we have the nameof operator which makes it great to pass the names of properties, methods, types, and so forth into attributes. The ForeignKeyAttribute, however, requires us to pass in a comma-separated string to specify multiple property names. Could we have a new constructor that accepts a params string[] and performs the concatenation, so that we can cleanly use the ForeignKeyAttribute with nameof to specify multiple names?
I assume you mean System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute, right?
That's the one, sorry 😸
Here is the gist of what I am proposing:
public ForeignKeyAttribute(params string[] names)
{
// Argument validation, etc.
Name = string.Join(",", names);
}
And the usage would look like this:
public class Employee
{
[Key]
public int Id { get; set; }
public int CompanyId { get; set; }
public int DepartmentId { get; set; }
[ForeignKey(nameof(CompanyId), nameof(DepartmentId))]
public Department Department { get; set; }
}
[EDIT] Added C# syntax highlight by @karelz
@lajones @divega @ajcvickers can you please review the API? There is pending implementation by @blackstarzes. Thanks!
Temporarily assigning to myself. @blackstarzes please ping me when you accept the invitation, so that I can assign the issue to you ...
@karelz I have accepted the invite. Thanks for that - I am keen to contribute! Sorry for the delay - I'm in the middle of moving apartments.
@karelz I haven't heard anything further on this. What are the next steps to actually make a contribution?
@blackstarzes @karelz was out of town. The next step is typically to formalize the API proposal to our semi standard format eg https://github.com/dotnet/corefx/issues/271 larger process described here https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/api-review-process.md Then @lajones @divega @ajcvickers as area owners give feedback. if it gets to a mutually agreed state they flip the label to api-ready-for-review and we pick it up at the api review meeting a week or two later. if it's approved, anyone can add it.
Clearly this shouldn't need a long writeup.
@danmosemsft thanks for the quick response. I'll give it a go in the format from dotnet/runtime#13933 using @tuespetre 's example:
Currently, the System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute attribute provides a single constructor that takes a string parameter. This is used to represent foreign keys using a single column as well as compound foreign keys using multiple columns. The latter is achieved by passing through comma-separated column names, resulting in less concise code when using this attribute. This attribute should expose a params string[] constructor to allow more concise code.
Rationale and Usage
Take a class Employee that has a property Department that needs to be referenced using a compound foreign key consisting of CompanyId and DepartmentId:
public class Employee
{
[Key]
public int Id { get; set; }
public int CompanyId { get; set; }
public int DepartmentId { get; set; }
public Department Department { get; set; }
}
Currently, the Department navigation property needs to be decorated using the following attribute:
[ForeignKey("CompanyId,DepartmentId")]
It would be more concise and compile-time safe to be able to decorate it as such:
[ForeignKey(nameof(CompanyId), nameof(DepartmentId))]
Proposed API
[System.AttributeUsageAttribute((System.AttributeTargets)(384), AllowMultiple = false)]
public partial class ForeignKeyAttribute : System.Attribute
{
public ForeignKeyAttribute(string name) { }
public ForeignKeyAttribute(params string[] names) { } // Proposed new constructor
public string Name { get { throw null; } }
}
Details
This is not really much else that can go here - this is quite a simple change.
Open questions
- Should we add a
Namesproperty that is stored as astring[]and use this as the underlying property for theNameproperty? corefx internal code could then use theNamesproperty internally rather than splitting up theNameproperty. AsNameis readonly (as is the backing field), there is only a need to worry about theset.
Pull Request
None
Updates
- Modified current foreign key decoration as @jnm2 pointed out an issue with it
Currently, the
Departmentnavigation property needs to be decorated using the following attribute:[ForeignKey($"{nameof(CompanyId)},{nameof(DepartmentId)}")]
And that's not even currently possible until we have https://github.com/dotnet/csharplang/issues/384.
Some questions:
- If anyone has code
new ForeignKeyAttribute(null);will this now fail to compile? If it does fail, does this then constitute a breaking change? - What are the behaviors if names is null or the empty list?
- It's unlikely that most of the consuming code will ever be updated, so regardless of whether or not the Names property is added it's important that consumers will still be able to get the names in the old way.
I also vote for a central collection Names property as @blackstarzes suggested (in "Proposed API").
Closing this as something we are not going to implement. If it can be done in a 100% non-breaking way, then we might consider a community PR. However, keep in mind that this will cause deviation in behavior between .NET Framework and .NET Core.