Skip to content

Commit

Permalink
Merge pull request #53 from seesharper/use-key-attribute
Browse files Browse the repository at this point in the history
Use key attribute
  • Loading branch information
seesharper committed May 31, 2021
2 parents eb72df1 + 1da9249 commit 2de6ae1
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 25 deletions.
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public record Customer(string CustomerId, string CompanyName)
To define key "properties", we can decorate the constructor arguments with the `KeyAttribute`

```c#
public record Customer([Key]string CustomerId, string CompanyName)
public record Customer([attribute:Key]string CustomerId, string CompanyName)
```


> Note: The `[attribute:Key]` syntax simply tells the compiler to add the `KeyAttribute` to the generated property

## List Parameters

Expand Down Expand Up @@ -412,7 +412,7 @@ ON

## Keys

The only requirement with regards to metadata is that a class must declare a property that uniquely identifies an instance of this class. This information is used to determine if we should create a new instance of a class or retrieve it from the query cache. The query cache makes sure that we don't eagerly create new instances of classes that has already been read. The default convention here is that each class must declare a property named *Id*, *[classname]Id* or decorate the key property with the `KeyAttribute` from the `DbReader.Annotations` namespace.
The only requirement with regards to metadata is that a class must declare a property that uniquely identifies an instance of this class. This information is used to determine if we should create a new instance of a class or retrieve it from the query cache. The query cache makes sure that we don't eagerly create new instances of classes that has already been read. The default convention here is that each class must declare a property named *Id*, *[classname]Id* or decorate the key property with the `KeyAttribute` from the `System.ComponentModel.DataAnnotations` namespace.


```c#
Expand Down
2 changes: 1 addition & 1 deletion src/DbReader.Tests/DbReader.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="shouldly" Version="3.0.2" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.113.1" />
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Reflection.Emit.ILGeneration" Version="4.7.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/DbReader.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace DbReader.Tests
using Xunit;
using System.Text;
using System.Data.SQLite;
using DbReader.Annotations;
using System.ComponentModel.DataAnnotations;

public class IntegrationTests
{
Expand Down Expand Up @@ -592,10 +592,10 @@ public record CustomerRecord(string CustomerId, string CompanyName)
{
}

public record CustomerRecordWithOrders([Key] string CustomerId, string CompanyName, ICollection<OrderRecord> Orders)
public record CustomerRecordWithOrders([property: Key] string CustomerId, string CompanyName, ICollection<OrderRecord> Orders)
{
}

public record OrderRecord([Key] long OrderId, DateTime? OrderDate);
public record OrderRecord([property: Key] long OrderId, DateTime? OrderDate);
}
#endif
9 changes: 0 additions & 9 deletions src/DbReader/Annotations/KeyAttribute.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src/DbReader/DbReader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
<Import Project="../mono.props" />
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netstandard2.0;net462</TargetFrameworks>
<!-- <TargetFramework>netcoreapp2.0</TargetFramework> -->
<Version>2.4.0</Version>
<Version>2.5.0</Version>
<Authors>Bernhard Richter</Authors>
<PackageProjectUrl>https://github.com/seesharper/DbReader</PackageProjectUrl>
<RepositoryType>git</RepositoryType>
Expand All @@ -29,6 +28,8 @@
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />

<PackageReference Include="LightInject.Source" Version="6.3.3">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
10 changes: 3 additions & 7 deletions src/DbReader/DbReaderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Common;
using System.Linq;
Expand All @@ -12,7 +13,6 @@
using System.Threading;
using System.Threading.Tasks;
using Database;
using DbReader.Annotations;
using DbReader.Extensions;
using Interfaces;
using Selectors;
Expand All @@ -37,15 +37,11 @@ static DbReaderOptions()
{
KeyConvention = p =>
p.Name.Equals("Id", StringComparison.OrdinalIgnoreCase)
|| p.Name.Equals(p.DeclaringType.Name + "Id", StringComparison.OrdinalIgnoreCase) || p.IsDefined(typeof(KeyAttribute), true) || HasConstructorWithKeyParameterMatchingProperty(p);
|| p.Name.Equals(p.DeclaringType.Name + "Id", StringComparison.OrdinalIgnoreCase) || p.IsDefined(typeof(KeyAttribute), true);
ParameterParser = new RegExParameterParser(@"(:\w+)|(@\w+)", @"IN\s*\((\s*(?:@|:)\w+)\s*\)");
}

private static bool HasConstructorWithKeyParameterMatchingProperty(PropertyInfo property)
{
var constructors = property.DeclaringType.GetConstructors();
return constructors.SelectMany(c => c.GetParameters()).Any(c => c.Name.Equals(property.Name, StringComparison.Ordinal) && c.IsDefined(typeof(KeyAttribute), true));
}




Expand Down

0 comments on commit 2de6ae1

Please sign in to comment.