Skip to content

Commit

Permalink
Add Pure.DI.Abstractions library
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Jun 18, 2024
1 parent 1ede20d commit f7439ed
Show file tree
Hide file tree
Showing 21 changed files with 313 additions and 55 deletions.
20 changes: 20 additions & 0 deletions .run/Build and pack libraries.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Build and pack libraries" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/build/bin/Debug/net8.0/build.exe" />
<option name="PROGRAM_PARAMETERS" value="libs" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/build/build.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
20 changes: 20 additions & 0 deletions .run/Compatibility checks.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Compatibility checks" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/build/bin/Debug/net8.0/build.exe" />
<option name="PROGRAM_PARAMETERS" value="check" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/build/build.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
7 changes: 7 additions & 0 deletions Pure.DI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalWebAPI", "samples\Mi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SingleRootAvaloniaApp", "samples\SingleRootAvaloniaApp\SingleRootAvaloniaApp.csproj", "{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pure.DI.Abstractions", "src\Pure.DI.Abstractions\Pure.DI.Abstractions.csproj", "{136A2850-FDA0-4CDB-A798-21C4B4A01974}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -206,6 +208,10 @@ Global
{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8}.Release|Any CPU.Build.0 = Release|Any CPU
{136A2850-FDA0-4CDB-A798-21C4B4A01974}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{136A2850-FDA0-4CDB-A798-21C4B4A01974}.Debug|Any CPU.Build.0 = Debug|Any CPU
{136A2850-FDA0-4CDB-A798-21C4B4A01974}.Release|Any CPU.ActiveCfg = Release|Any CPU
{136A2850-FDA0-4CDB-A798-21C4B4A01974}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7C9E056B-CBA9-4548-9CDB-C5CE03C491B0} = {8163CDD7-7018-4301-A984-803C3807A6A6}
Expand All @@ -232,5 +238,6 @@ Global
{9A3E2271-3090-4BCE-BB48-6C0724CFBE44} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{60F18CFA-957B-488F-8292-467D92C17267} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{7FCFD4F6-AFB8-479C-A103-AEBC2FC282A8} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{136A2850-FDA0-4CDB-A798-21C4B4A01974} = {8163CDD7-7018-4301-A984-803C3807A6A6}
EndGlobalSection
EndGlobal
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ dotnet run
- [Member ordinal attribute](readme/member-ordinal-attribute.md)
- [Tag attribute](readme/tag-attribute.md)
- [Type attribute](readme/type-attribute.md)
- [Inject attribute](readme/inject-attribute.md)
- [Custom attributes](readme/custom-attributes.md)
- [Custom universal attribute](readme/custom-universal-attribute.md)
### Interception
Expand Down Expand Up @@ -823,11 +824,12 @@ Then documentation for the composition root:

## NuGet packages

| | | |
|-------------------|---------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------|
| Pure.DI | [![NuGet](https://buildstats.info/nuget/Pure.DI)](https://www.nuget.org/packages/Pure.DI) | DI Source code generator |
| Pure.DI.Templates | [![NuGet](https://buildstats.info/nuget/Pure.DI.Templates)](https://www.nuget.org/packages/Pure.DI.Templates) | Template Package you can call from the shell/command line. |
| Pure.DI.MS | [![NuGet](https://buildstats.info/nuget/Pure.DI.MS)](https://www.nuget.org/packages/Pure.DI.MS) | Tools for working with Microsoft DI |
| | | |
|----------------------|---------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------|
| Pure.DI | [![NuGet](https://buildstats.info/nuget/Pure.DI)](https://www.nuget.org/packages/Pure.DI) | DI Source code generator |
| Pure.DI.Abstractions | [![NuGet](https://buildstats.info/nuget/Pure.DI.Abstractions)](https://www.nuget.org/packages/Pure.DI.Abstractions) | Abstractions for Pure.DI |
| Pure.DI.Templates | [![NuGet](https://buildstats.info/nuget/Pure.DI.Templates)](https://www.nuget.org/packages/Pure.DI.Templates) | Template Package you can call from the shell/command line. |
| Pure.DI.MS | [![NuGet](https://buildstats.info/nuget/Pure.DI.MS)](https://www.nuget.org/packages/Pure.DI.MS) | Tools for working with Microsoft DI |

## Project template

Expand Down
11 changes: 10 additions & 1 deletion build/LibrariesTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ public Task<IReadOnlyCollection<Library>> RunAsync(CancellationToken cancellatio
.Where(i => i.Version.Major >= 7)
.Select(v => $"net{v.Version.Major}.{v.Version.Minor}")
.ToArray(),
["webapi"])
["webapi"]),

new Library(
"Pure.DI.Abstractions",
new Package(GetPackagePath("Pure.DI.Abstractions", settings.NextVersion), false),
sdk.Versions
.Where(i => i.Version.Major >= 7)
.Select(v => $"net{v.Version.Major}.{v.Version.Minor}")
.ToArray(),
["classlib"])
];

foreach (var library in libraries)
Expand Down
11 changes: 6 additions & 5 deletions readme/FooterTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,12 @@ Then documentation for the composition root:

## NuGet packages

| | | |
|-------------------|---------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------|
| Pure.DI | [![NuGet](https://buildstats.info/nuget/Pure.DI)](https://www.nuget.org/packages/Pure.DI) | DI Source code generator |
| Pure.DI.Templates | [![NuGet](https://buildstats.info/nuget/Pure.DI.Templates)](https://www.nuget.org/packages/Pure.DI.Templates) | Template Package you can call from the shell/command line. |
| Pure.DI.MS | [![NuGet](https://buildstats.info/nuget/Pure.DI.MS)](https://www.nuget.org/packages/Pure.DI.MS) | Tools for working with Microsoft DI |
| | | |
|----------------------|---------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------|
| Pure.DI | [![NuGet](https://buildstats.info/nuget/Pure.DI)](https://www.nuget.org/packages/Pure.DI) | DI Source code generator |
| Pure.DI.Abstractions | [![NuGet](https://buildstats.info/nuget/Pure.DI.Abstractions)](https://www.nuget.org/packages/Pure.DI.Abstractions) | Abstractions for Pure.DI |
| Pure.DI.Templates | [![NuGet](https://buildstats.info/nuget/Pure.DI.Templates)](https://www.nuget.org/packages/Pure.DI.Templates) | Template Package you can call from the shell/command line. |
| Pure.DI.MS | [![NuGet](https://buildstats.info/nuget/Pure.DI.MS)](https://www.nuget.org/packages/Pure.DI.MS) | Tools for working with Microsoft DI |

## Project template

Expand Down
49 changes: 49 additions & 0 deletions readme/inject-attribute.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#### Inject attribute

[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Attributes/InjectAttributeScenario.cs)

If you want to use attributes in your libraries but don't want to create your own, you can add this package to your projects:

[![NuGet](https://buildstats.info/nuget/Pure.DI.Abstractions)](https://www.nuget.org/packages/Pure.DI.Abstractions)

It contains attributes like `Inject` and `Inject<T>` that work for constructors and their arguments, methods and their arguments, properties and fields. They allow you to setup all injection parameters.


```c#
using Pure.DI.Abstractions;

interface IPerson;

class Person([Inject("NikName")] string name) : IPerson
{
private object? _state;

[Inject<int>]
internal object Id = "";

public void Initialize([Inject<Uri>("Person Uri", 1)] object state) =>
_state = state;

public override string ToString() => $"{Id} {name} {_state}";
}

DI.Setup(nameof(PersonComposition))
.Arg<int>("personId")
.Bind("Person Uri").To(_ => new Uri("https://github.com/DevTeam/Pure.DI"))
.Bind("NikName").To(_ => "Nik")
.Bind().To<Person>()

// Composition root
.Root<IPerson>("Person");

var composition = new PersonComposition(personId: 123);
var person = composition.Person;
person.ToString().ShouldBe("123 Nik https://github.com/DevTeam/Pure.DI");
```

This package should also be included in a project:

[![NuGet](https://buildstats.info/nuget/Pure.DI)](https://www.nuget.org/packages/Pure.DI)



4 changes: 1 addition & 3 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
<Import Project="..\Directory.Build.props"/>

<PropertyGroup>
<Description>$(BasePackageId) is not a framework or library, but a source code generator for creating object graphs. To make them accurate, the developer uses a set of intuitive hints from the Pure.DI API. During the compilation phase, Pure.DI determines the optimal graph structure, checks its correctness, and generates partial class code to create object graphs in the Pure DI paradigm using only basic language constructs. The resulting generated code is robust, works everywhere, throws no exceptions, does not depend on .NET library calls or .NET reflections, is efficient in terms of performance and memory consumption, and is subject to all optimizations. This code can be easily integrated into an application because it does not use unnecessary delegates, additional calls to any methods, type conversions, boxing/unboxing, etc.</Description>
<PackageIcon>common\icon.png</PackageIcon>
<TargetFramework>netstandard2.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\DevTeam.snk</AssemblyOriginatorKeyFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -15,7 +14,6 @@
<InternalsVisibleTo Include="DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7" />
<InternalsVisibleTo Include="Pure.DI.Tests, PublicKey=00240000048000009400000006020000002400005253413100040000010001003fa521b0b16e978a933ecce70646c632538351d320a226a64b2c93238b3ba699cb66233e5722c25dd64f816c2aef8d2f1426983ea8c4750902f4a8b03cb00da22e7c978f56cdcfc711ea0a3625016a2ec2238093912799a3cda4ee787592738c7d21f6eed5e3a6d1b03f657ac3880672f2394144bd2359fddf17e464abd947a0" />
<InternalsVisibleTo Include="Pure.DI.IntegrationTests, PublicKey=00240000048000009400000006020000002400005253413100040000010001003fa521b0b16e978a933ecce70646c632538351d320a226a64b2c93238b3ba699cb66233e5722c25dd64f816c2aef8d2f1426983ea8c4750902f4a8b03cb00da22e7c978f56cdcfc711ea0a3625016a2ec2238093912799a3cda4ee787592738c7d21f6eed5e3a6d1b03f657ac3880672f2394144bd2359fddf17e464abd947a0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(AnalyzerRoslynPackageVersion)" PrivateAssets="all" />
</ItemGroup>

</Project>
Expand Down
7 changes: 0 additions & 7 deletions src/Library.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,4 @@
<BaseOutputPath>bin</BaseOutputPath>
</PropertyGroup>

<ItemGroup>
<Compile Update="@(Compile)">
<Pack>true</Pack>
<PackagePath>contentFiles/cs</PackagePath>
</Compile>
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/Pure.DI.Abstractions/InjectAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable RedundantNameQualifier
// ReSharper disable InvalidXmlDocComment
#pragma warning disable CS9113 // Parameter is unread.
namespace Pure.DI.Abstractions;

/// <summary>
/// A universal DI attribute that allows to specify the tag and ordinal of an injection.
/// </summary>
/// <param name="tag">The injection tag. See also <see cref="IBinding.Tags"/></param>.
/// <param name="ordinal">The injection ordinal.</param>
[global::System.AttributeUsage(global::System.AttributeTargets.Constructor | global::System.AttributeTargets.Method | global::System.AttributeTargets.Parameter | global::System.AttributeTargets.Property | global::System.AttributeTargets.Field)]
public sealed class InjectAttribute(object? tag = default, int ordinal = default) : global::System.Attribute;
15 changes: 15 additions & 0 deletions src/Pure.DI.Abstractions/InjectAttribute`1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ReSharper disable ClassNeverInstantiated.Global
// ReSharper disable UnusedTypeParameter
// ReSharper disable RedundantNameQualifier
// ReSharper disable InvalidXmlDocComment
#pragma warning disable CS9113 // Parameter is unread.
namespace Pure.DI.Abstractions;

/// <summary>
/// A universal DI attribute that allows to specify the type, tag, and ordinal of an injection.
/// </summary>
/// <param name="tag">The injection tag. See also <see cref="IBinding.Tags"/></param>.
/// <param name="ordinal">The injection ordinal.</param>
/// <typeparam name="T">The injection type. See also <see cref="IConfiguration.Bind{T}"/> and <see cref="IBinding.Bind{T}"/></typeparam>
[global::System.AttributeUsage(global::System.AttributeTargets.Constructor | global::System.AttributeTargets.Method | global::System.AttributeTargets.Parameter | global::System.AttributeTargets.Property | global::System.AttributeTargets.Field)]
public sealed class InjectAttribute<T>(object? tag = default, int ordinal = default) : global::System.Attribute;
41 changes: 41 additions & 0 deletions src/Pure.DI.Abstractions/Pure.DI.Abstractions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net20;net35;net40;net45;net48;netstandard1.0;netstandard1.1;netstandard1.2;netstandard1.3;netstandard1.4;netstandard1.5;netstandard1.6;netstandard2.0;netstandard2.1;netcoreapp1.0;netcoreapp1.1;netcoreapp2.0;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<ImplicitUsings>false</ImplicitUsings>
<PackageId>$(BasePackageId).Abstractions</PackageId>
<Description>Abstractions of $(BasePackageId). $(Description)</Description>
<DefineConstants>$(DefineConstants);PUREDI_NET_ANY</DefineConstants>
<NoWarn>NU1902;NU1903;NU3005</NoWarn>
</PropertyGroup>

<ItemGroup>
<Compile Remove="any\Pure.DI\Abstractions\Composition.g.cs"/>
<Content Include="any\Pure.DI\Abstractions\Composition.g.cs" buildAction="Compile" >
<Pack>true</Pack>
<PackagePath>contentFiles/cs/any/Pure.DI/Abstractions</PackagePath>
</Content>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net20" Version="1.0.3" Condition="'$(TargetFramework)' == 'net20' ">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net35" Version="1.0.3" Condition="'$(TargetFramework)' == 'net35' ">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net40" Version="1.0.3" Condition="'$(TargetFramework)' == 'net40' ">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net45" Version="1.0.3" Condition="'$(TargetFramework)' == 'net45' ">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net48" Version="1.0.3" Condition="'$(TargetFramework)' == 'net48' ">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions src/Pure.DI.Abstractions/any/Pure.DI/Abstractions/Composition.g.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <auto-generated/>
// #pragma warning disable
#if !PUREDI_API_SUPPRESSION || PUREDI_API_V1
#pragma warning disable
namespace Pure.DI.Abstractions
{
internal static class Composition
{
[global::System.Diagnostics.Conditional("A2768DE22DE3E430C9653990D516CC9B")]
private static void Setup()
{
global::Pure.DI.DI.Setup(kind: global::Pure.DI.CompositionKind.Global)
.TagAttribute<global::Pure.DI.Abstractions.InjectAttribute<TT>>()
.OrdinalAttribute<global::Pure.DI.Abstractions.InjectAttribute<TT>>(1)
.TypeAttribute<global::Pure.DI.Abstractions.InjectAttribute<TT>>()
.TagAttribute<global::Pure.DI.Abstractions.InjectAttribute>()
.OrdinalAttribute<global::Pure.DI.Abstractions.InjectAttribute>(1);
}
}
}
#pragma warning restore
#endif
Loading

0 comments on commit f7439ed

Please sign in to comment.