runtime icon indicating copy to clipboard operation
runtime copied to clipboard

ForeignKeyAttribute: can we have a params string[] constructor?

Open tuespetre opened this issue 9 years ago • 14 comments

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?

tuespetre avatar Jun 25 '17 20:06 tuespetre

I assume you mean System.ComponentModel.DataAnnotations.Schema.ForeignKeyAttribute, right?

karelz avatar Jun 26 '17 07:06 karelz

That's the one, sorry 😸

tuespetre avatar Jun 26 '17 12:06 tuespetre

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

tuespetre avatar Jun 29 '17 05:06 tuespetre

@lajones @divega @ajcvickers can you please review the API? There is pending implementation by @blackstarzes. Thanks!

karelz avatar Jul 29 '17 00:07 karelz

Temporarily assigning to myself. @blackstarzes please ping me when you accept the invitation, so that I can assign the issue to you ...

karelz avatar Jul 29 '17 01:07 karelz

@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.

blackstarzes avatar Jul 29 '17 23:07 blackstarzes

@karelz I haven't heard anything further on this. What are the next steps to actually make a contribution?

blackstarzes avatar Aug 11 '17 12:08 blackstarzes

@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.

danmoseley avatar Aug 11 '17 16:08 danmoseley

Clearly this shouldn't need a long writeup.

danmoseley avatar Aug 11 '17 16:08 danmoseley

@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 Names property that is stored as a string[] and use this as the underlying property for the Name property? corefx internal code could then use the Names property internally rather than splitting up the Name property. As Name is readonly (as is the backing field), there is only a need to worry about the set.

Pull Request

None

Updates

  • Modified current foreign key decoration as @jnm2 pointed out an issue with it

blackstarzes avatar Aug 12 '17 03:08 blackstarzes

Currently, the Department navigation 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.

jnm2 avatar Aug 12 '17 17:08 jnm2

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.

ajcvickers avatar Aug 16 '17 16:08 ajcvickers

I also vote for a central collection Names property as @blackstarzes suggested (in "Proposed API").

weitzhandler avatar Sep 18 '17 22:09 weitzhandler

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.

ajcvickers avatar Feb 22 '19 23:02 ajcvickers