You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* The attributes are no longer embed in your project by default, instead it will use the external dll. You can re-enable the embedding by setting `STRONGLY_TYPED_ID_EMBED_ATTRIBUTES`.
8
+
9
+
New Features:
10
+
11
+
* Improved approach to handling [InternalsVisibleTo] issues, by embedding the StronglyTypedId.Attributes.dll in the NuGet package directly.
<img src="https://raw.githubusercontent.com/andrewlock/StronglyTypedId/master/docs/strongly_typed_id.gif" alt="Generating a strongly-typed ID using the StronglyTypedId packages"/>
24
-
</picture>
21
+
<imgsrc="https://raw.githubusercontent.com/andrewlock/StronglyTypedId/master/docs/strongly_typed_id.gif"alt="Generating a strongly-typed ID using the StronglyTypedId packages"/>
@@ -58,7 +55,15 @@ To use the the [StronglyTypedId NuGet package](https://www.nuget.org/packages/St
58
55
*[Dapper](https://www.nuget.org/packages/Dapper/) (optional, only required if [generating a type mapper](https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-3/#interfacing-with-external-system-using-strongly-typed-ids))
59
56
*[EF Core](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore) (optional, only required if [generating an EF Core ValueConverter](https://andrewlock.net/strongly-typed-ids-in-ef-core-using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-4/))
60
57
61
-
To install the packages, add the references to your _csproj_ file so that it looks something like the following:
58
+
To install the packages, add the references to your _csproj_ file, for example by running
This adds a `<PackageReference>` to your project. You can additionally mark the package as `PrivateAsets="all"` and `ExcludeAssets="runtime"`.
65
+
66
+
> Setting `PrivateAssets="all"` means any projects referencing this one will not also get a reference to the _StronglyTypedId_ package. Setting `ExcludeAssets="runtime"` ensures the _StronglyTypedId.Attributes.dll_ file is not copied to your build output (it is not required at runtime).
62
67
63
68
```xml
64
69
<ProjectSdk="Microsoft.NET.Sdk">
@@ -68,8 +73,8 @@ To install the packages, add the references to your _csproj_ file so that it loo
@@ -124,38 +129,100 @@ var id = new FooId("my-id-value");
124
129
```
125
130
Currently supported values are `Guid` (the default), `int`, `long`, and `string`.
126
131
127
-
## Error CS0436 and [InternalsVisibleTo]
132
+
## Changing the defaults globally
128
133
129
-
The StronglyTypedId generator automatically adds the `[StronglyTypedId]` attributes to your compilation as `internal` attributes. If you add the source generator package to multiple projects, and use the `[InternalsVisibleTo]` attribute, you may experience errors when you build:
134
+
If you wish to change the converters, backing types, or implementations used by default for _all_ the `[StronglyTypedId]`-decorated IDs in your project, you can use the assembly attribute `[StronglyTypedIdDefaults]` to set all of these. For example, the following sets the default converter to a whole project to `[SystemTextJson]`, and changes the default backing-type to an `int`
130
135
131
-
```bash
132
-
warning CS0436: The type'StronglyTypedIdImplementations'in'StronglyTypedIds\StronglyTypedIds.StronglyTypedIdGenerator\StronglyTypedIdImplementations.cs' conflicts with the imported type'StronglyTypedIdImplementations'in'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Removing the `[InternalsVisibleTo]` attribute will resolve the problem, but if this is not possible you can disable the auto-generation of the `[StronglyTypedId]` marker attributes, and rely on the helper [`StronglyTypedId.Attributes` package instead](https://www.nuget.org/packages/StronglyTypedId.Attributes). This package contains the same attributes, but as they are in an external package, you can avoid the CS0436 error.
136
163
137
-
Add the package to your solution, ensuring you set `"PrivateAssets="All"` in the `<PackageReference>` (this will be done automatically when using the .NET CLI or an IDE). To disable the auto-generation of the marker attributes, define the constant `STRONGLY_TYPED_ID_EXCLUDE_ATTRIBUTES` in your project file. Your project file should look something like the following:
164
+
## Embedding the attributes in your project
165
+
166
+
By default, the `[StronglyTypedId]` attributes referenced in your application are contained in an external dll. It is also possible to embed the attributes directly in your project, so they appear in the dll when your project is built. If you wish to do this, you must do two things:
167
+
168
+
1. Define the MSBuild constant `STRONGLY_TYPED_ID_EMBED_ATTRIBUTES`. This ensures the attributes are embedded in your project
169
+
2. Add `compile` to the list of excluded assets in your `<PackageReference>` element. This ensures the attributes in your project are referenced, instead of the _StronglyTypedId.Attributes.dll_ library.
170
+
171
+
Your project file should look something like this:
<!-- ☝ Add compile to the list of excluded assets. -->
154
188
155
189
</Project>
156
190
```
157
191
158
-
The attribute library is only required at compile time, so it won't appear in your build output.
192
+
## Preserving usages of the `[StronglyTypedId]` attribute
193
+
194
+
The `[StronglyTypedId]` and `[StronglyTypedIdDefaults]` attributes are decorated with the `[Conditional]` attribute, [so their usage will not appear in the build output of your project](https://andrewlock.net/conditional-compilation-for-ignoring-method-calls-with-the-conditionalattribute/#applying-the-conditional-attribute-to-classes). If you use reflection at runtime on one of your IDs, you will not find `[StronglyTypedId]` in the list of custom attributes.
195
+
196
+
If you wish to preserve these attributes in the build output, you can define the `STRONGLY_TYPED_ID_USAGES` MSBuild variable. Note that this means your project will have a runtime-dependency on _StronglyTypedId.Attributes.dll_ so you need to ensure this is included in your build output.
197
+
198
+
```xml
199
+
<ProjectSdk="Microsoft.NET.Sdk">
200
+
201
+
<PropertyGroup>
202
+
<OutputType>Exe</OutputType>
203
+
<TargetFramework>net6.0</TargetFramework>
204
+
<!-- Define the MSBuild constant to preserve usages -->
<!-- ☝ You must not exclude the runtime assets in this case -->
211
+
212
+
</Project>
213
+
```
214
+
215
+
## Error CS0436 and [InternalsVisibleTo]
216
+
217
+
> In the latest version of StronglyTypedId, you should not experience error CS0436 by default.
218
+
219
+
In previous versions of the StronglyTypedId generator, the `[StronglyTypedId]` attributes were added to your compilation as `internal` attributes by default. If you added the source generator package to multiple projects, and used the `[InternalsVisibleTo]` attribute, you could experience errors when you build:
220
+
221
+
```bash
222
+
warning CS0436: The type'StronglyTypedIdImplementations'in'StronglyTypedIds\StronglyTypedIds.StronglyTypedIdGenerator\StronglyTypedIdImplementations.cs' conflicts with the imported type'StronglyTypedIdImplementations'in'MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
223
+
```
224
+
225
+
In the latest version of _StronglyTypedId_, the attributes are not embedded by default, so you should not experience this problem. If you see this error, compare your installation to the examples in the installation guide.
Copy file name to clipboardExpand all lines: releasenotes.props
+10Lines changed: 10 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,16 @@
5
5
6
6
Version 0.x of this library used the helper library CodeGeneration.Roslyn for build-time source generation. In version 1.x this approach has been completely replaced in favour of source generators, as these are explicitly supported in .NET 5+. As part of this change, there were a number of additional features added and breaking changes made.
* The attributes are no longer embed in your project by default, instead it will use the external dll. You can re-enable the embedding by setting `STRONGLY_TYPED_ID_EMBED_ATTRIBUTES`.
13
+
14
+
New Features:
15
+
16
+
* Improved approach to handling [InternalsVisibleTo] issues, by embedding the StronglyTypedId.Attributes.dll in the NuGet package directly.
0 commit comments