Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support [Column] #85

Merged

Conversation

DeagleGross
Copy link
Collaborator

Goal:

Support [System.ComponentModel.DataAnnotations.Schema.ColumnAttribute] as an additional way to override parameter name.
Requested by some users (stackoverflow)

Example:

To make override work, one need to specify both [Column] and [UseColumnAttribute] attributes, for example:

class MyType
{
    [UseColumnAttribute]
    [Column("myOtherName")]
    public string Name { get; set; }
}

Rules:

  1. If [Column] and either [UseColumnAttribute] or [UseColumnAttribute(true)] is defined, than override will work
  2. If [UseColumnAttribute(false)] is explicitly specified, then overriding will not happen
  3. If [Column] is specified, but [UseColumnAttribute] is missing, then DAP043 will be emitted to the user: "Attach the [UseColumnAttribute] attribute to make Dapper consider [Column]"
  4. If 1) is followed, but also [DbValue] with property Name is specified, then a warning message will be emitted to the user: DAP042 "A column name is specified via both [DbValue] and [Column]; '<dbvalue_name>' will be used"

Closes #36

@DeagleGross DeagleGross self-assigned this Nov 18, 2023
/// <summary>
/// Specifies whether to use [System.ComponentModel.DataAnnotations.Schema.ColumnAttribute] for additional behavioral configuration
/// </summary>
public class UseColumnAttributeAttribute : Attribute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the allowed usage here? can it be applied at the module level, or is it only respected when attached to the same type? are more than one allowed? can we add [AttributeUsage(...)] here accordingly

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added such an expression.
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]

let me know if such AttributeUsage is correct

@mgravell
Copy link
Member

looking good; fixed some minor nits:

  • avoid /en-us/ in docs links; if you cut it, it will use the user's locale settings
  • formatting in docs
  • used a different variant of that string equality fix

an open question on the new attribute, but: looking good!

@DeagleGross
Copy link
Collaborator Author

DeagleGross commented Nov 20, 2023

looking good; fixed some minor nits:

  • avoid /en-us/ in docs links; if you cut it, it will use the user's locale settings
  • formatting in docs
  • used a different variant of that string equality fix

an open question on the new attribute, but: looking good!

Thanks for the useful advices and fixes, Marc! Sorry for attribute usage - I added it. Ready for review

@mgravell mgravell merged commit 636fd63 into DapperLib:main Dec 13, 2023
1 check passed
@DeagleGross DeagleGross deleted the dmkorolev/support-column-attribute branch April 25, 2024 21:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support [Column]
2 participants