DapperLib / DapperLib/Dapper.Contrib
Contrib : HasDefault, AllowNulls([True, False])
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 293
- Forks
- 109
- PR merge metrics
- No merged PRs in 30d
Description
Hi guys,
How do you all feel about two more attributes being added to Contrib to handle database schemes that have "default values" and "allow nulls"?
There are two scenarios which we have come up against, which these two attributes should resolve.
- A database table has a column which has a default value.
- A database table has a column with allowNull set to false.
1a) Currently if you do an insert using a full model and the target table has a column with a default value (and the models value is null), Contrib has no way to know this and so will pass in a parameterized value instead. If this were an insert to a table, then the result would be 'null' and not the default set for the column. Even worse in the case where the target table has a default value and "Allow Null" is set to false. In this scenario, you get an exception because contrib is sending an explicit "null" which overrides SQL's default value.
If we apply a [HasDefault] attribute, and the scenario is an insert, it would be a trivial thing for Contrib to remove any values which are "null" and so removing it from the insert and allowing the database to apply its default.
2a) When a table column has "Allow Null" set to false, and the model has a null value set the update will run, again causing an exception on the connection. If we apply a [AllowNull(False)] or [NotNullable] attribute to the field we can let Contrib handle this issue before it gets put on the wire and sent over to SQL to error.
I'm happy to make a pull request for this work :)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inspecting Dapper.Contrib's attribute definitions and the insert and update paths that build SQL parameters. Determine how HasDefault and AllowNull/NotNullable should interact with null model values, then add coverage for database defaults and rejected nulls; done means inserts preserve column defaults and invalid updates fail before reaching SQL.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100