Skip to content

Commit

Permalink
Adds Blazor app samples
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Oct 24, 2023
1 parent 1f71207 commit 4b04b5c
Show file tree
Hide file tree
Showing 82 changed files with 3,091 additions and 25 deletions.
23 changes: 23 additions & 0 deletions Pure.DI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "readme", "readme", "{24E449
readme\WinFormsAppNetCorePageTemplate.md = readme\WinFormsAppNetCorePageTemplate.md
readme\WinFormsAppPageTemplate.md = readme\WinFormsAppPageTemplate.md
readme\AvaloniaPageTemplate.md = readme\AvaloniaPageTemplate.md
readme\BlazorServerAppPageTemplate.md = readme\BlazorServerAppPageTemplate.md
readme\BlazorWebAssemblyAppPageTemplate.md = readme\BlazorWebAssemblyAppPageTemplate.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pure.DI.Templates", "src\Pure.DI.Templates\Pure.DI.Templates.csproj", "{5A925B99-3FF8-4ED0-81C2-7F0D3AD52642}"
Expand All @@ -86,6 +88,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinFormsAppNetCore", "sampl
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaApp", "samples\AvaloniaApp\AvaloniaApp.csproj", "{E4274A56-868E-427E-87FE-E2D46749CAE6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorServerApp", "samples\BlazorServerApp\BlazorServerApp.csproj", "{6C95BC87-DA77-4CE7-8CAB-9BEC254CEF51}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorWebAssemblyApp", "samples\BlazorWebAssemblyApp\BlazorWebAssemblyApp.csproj", "{D1C043C4-ED8F-45C5-8077-EF10AA71B951}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WeatherForecast", "samples\WeatherForecast\WeatherForecast.csproj", "{9A3E2271-3090-4BCE-BB48-6C0724CFBE44}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -172,6 +180,18 @@ Global
{E4274A56-868E-427E-87FE-E2D46749CAE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4274A56-868E-427E-87FE-E2D46749CAE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4274A56-868E-427E-87FE-E2D46749CAE6}.Release|Any CPU.Build.0 = Release|Any CPU
{6C95BC87-DA77-4CE7-8CAB-9BEC254CEF51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C95BC87-DA77-4CE7-8CAB-9BEC254CEF51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C95BC87-DA77-4CE7-8CAB-9BEC254CEF51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C95BC87-DA77-4CE7-8CAB-9BEC254CEF51}.Release|Any CPU.Build.0 = Release|Any CPU
{D1C043C4-ED8F-45C5-8077-EF10AA71B951}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1C043C4-ED8F-45C5-8077-EF10AA71B951}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1C043C4-ED8F-45C5-8077-EF10AA71B951}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1C043C4-ED8F-45C5-8077-EF10AA71B951}.Release|Any CPU.Build.0 = Release|Any CPU
{9A3E2271-3090-4BCE-BB48-6C0724CFBE44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A3E2271-3090-4BCE-BB48-6C0724CFBE44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A3E2271-3090-4BCE-BB48-6C0724CFBE44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A3E2271-3090-4BCE-BB48-6C0724CFBE44}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7C9E056B-CBA9-4548-9CDB-C5CE03C491B0} = {8163CDD7-7018-4301-A984-803C3807A6A6}
Expand All @@ -193,5 +213,8 @@ Global
{BFC61486-B6A3-40DA-BE7C-F7C737E29EF8} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{AFC20CA4-8A42-4692-BAEE-52272803356A} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{E4274A56-868E-427E-87FE-E2D46749CAE6} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{6C95BC87-DA77-4CE7-8CAB-9BEC254CEF51} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{D1C043C4-ED8F-45C5-8077-EF10AA71B951} = {FA80D231-C641-4A49-99C6-0C065D818B07}
{9A3E2271-3090-4BCE-BB48-6C0724CFBE44} = {FA80D231-C641-4A49-99C6-0C065D818B07}
EndGlobalSection
EndGlobal
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ dotnet run
- [Web](readme/WebApp.md)
- [Web API service](readme/WebAPI.md)
- [gRPC service](readme/GrpcService.md)
- [Blazor Server](readme/BlazorServerApp.md)
- [Blazor WebAssembly](readme/BlazorWebAssemblyApp.md)

## Generated Code

Expand Down
63 changes: 63 additions & 0 deletions readme/BlazorServerApp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#### Wep API

[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](/samples/BlazorServerApp)

This example demonstrates the creation of a [Blazor server](https://learn.microsoft.com/en-us/aspnet/core/blazor/hosting-models#blazor-server) application in the pure DI paradigm using the _Pure.DI_ code generator.

Composition setup file is [Composition.cs](/samples/BlazorServerApp/Composition.cs):

```c#
internal partial class Composition: ServiceProviderFactory<Composition>
{
private static void Setup() =>
DI.Setup(nameof(Composition))
.DependsOn(Base)
.Bind<IWeatherForecastService>()
.As(Singleton)
.To<WeatherForecastService>()
.Root<IWeatherForecastService>()
.Bind<ICounterService>()
.To<CounterService>()
.Root<ICounterService>()
.Bind<IErrorModel>()
.To<ErrorModel>()
.Root<IErrorModel>();
}
```

The composition class inherits from the `ServiceProviderFactory<T>` class, where T is the composition class itself. It depends on the `Base` setup.

Te web application entry point is in the [Program.cs](/samples/BlazorServerApp/Program.cs) file:

```c#
var builder = WebApplication.CreateBuilder(args);

// Uses Composition as an alternative IServiceProviderFactory
builder.Host.UseServiceProviderFactory(new Composition());
```

The [project file](/samples/BlazorServerApp/BlazorServerApp.csproj) looks like this:

```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pure.DI" Version="2.0.21">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pure.DI.MS" Version="2.0.21" />
</ItemGroup>
</Project>
```

It contains 2 additional links to NuGet packages:

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

[![NuGet](https://buildstats.info/nuget/Pure.DI.MS)](https://www.nuget.org/packages/Pure.DI.MS)
63 changes: 63 additions & 0 deletions readme/BlazorServerAppPageTemplate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#### Wep API

[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](/samples/BlazorServerApp)

This example demonstrates the creation of a [Blazor server](https://learn.microsoft.com/en-us/aspnet/core/blazor/hosting-models#blazor-server) application in the pure DI paradigm using the _Pure.DI_ code generator.

Composition setup file is [Composition.cs](/samples/BlazorServerApp/Composition.cs):

```c#
internal partial class Composition: ServiceProviderFactory<Composition>
{
private static void Setup() =>
DI.Setup(nameof(Composition))
.DependsOn(Base)
.Bind<IWeatherForecastService>()
.As(Singleton)
.To<WeatherForecastService>()
.Root<IWeatherForecastService>()
.Bind<ICounterService>()
.To<CounterService>()
.Root<ICounterService>()
.Bind<IErrorModel>()
.To<ErrorModel>()
.Root<IErrorModel>();
}
```

The composition class inherits from the `ServiceProviderFactory<T>` class, where T is the composition class itself. It depends on the `Base` setup.

Te web application entry point is in the [Program.cs](/samples/BlazorServerApp/Program.cs) file:

```c#
var builder = WebApplication.CreateBuilder(args);

// Uses Composition as an alternative IServiceProviderFactory
builder.Host.UseServiceProviderFactory(new Composition());
```

The [project file](/samples/BlazorServerApp/BlazorServerApp.csproj) looks like this:

```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pure.DI" Version="$(version)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pure.DI.MS" Version="$(version)" />
</ItemGroup>
</Project>
```

It contains 2 additional links to NuGet packages:

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

[![NuGet](https://buildstats.info/nuget/Pure.DI.MS)](https://www.nuget.org/packages/Pure.DI.MS)
60 changes: 60 additions & 0 deletions readme/BlazorWebAssemblyApp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#### Wep API

[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](/samples/BlazorServerApp)

This example demonstrates the creation of a [Blazor WebAssembly](https://learn.microsoft.com/en-us/aspnet/core/blazor/hosting-models#blazor-webassembly) application in the pure DI paradigm using the _Pure.DI_ code generator.

Composition setup file is [Composition.cs](/samples/BlazorWebAssemblyApp/Composition.cs):

```c#
internal partial class Composition: ServiceProviderFactory<Composition>
{
private static void Setup() =>
DI.Setup(nameof(Composition))
.DependsOn(Base)
.Bind<IWeatherForecastService>()
.As(Singleton)
.To<WeatherForecastService>()
.Root<IWeatherForecastService>()
.Bind<ICounterService>()
.To<CounterService>()
.Root<ICounterService>();
}
```

The composition class inherits from the `ServiceProviderFactory<T>` class, where T is the composition class itself. It depends on the `Base` setup.

Te web application entry point is in the [Program.cs](/samples/BlazorWebAssemblyApp/Program.cs) file:

```c#
var builder = WebAssemblyHostBuilder.CreateDefault(args);

// Uses Composition as an alternative IServiceProviderFactory
builder.ConfigureContainer(new Composition());
```

The [project file](/samples/BlazorWebAssemblyApp/BlazorWebAssemblyApp.csproj) looks like this:

```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pure.DI" Version="2.0.21">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pure.DI.MS" Version="2.0.21" />
</ItemGroup>
</Project>
```

It contains 2 additional links to NuGet packages:

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

[![NuGet](https://buildstats.info/nuget/Pure.DI.MS)](https://www.nuget.org/packages/Pure.DI.MS)
60 changes: 60 additions & 0 deletions readme/BlazorWebAssemblyAppPageTemplate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#### Wep API

[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](/samples/BlazorServerApp)

This example demonstrates the creation of a [Blazor WebAssembly](https://learn.microsoft.com/en-us/aspnet/core/blazor/hosting-models#blazor-webassembly) application in the pure DI paradigm using the _Pure.DI_ code generator.

Composition setup file is [Composition.cs](/samples/BlazorWebAssemblyApp/Composition.cs):

```c#
internal partial class Composition: ServiceProviderFactory<Composition>
{
private static void Setup() =>
DI.Setup(nameof(Composition))
.DependsOn(Base)
.Bind<IWeatherForecastService>()
.As(Singleton)
.To<WeatherForecastService>()
.Root<IWeatherForecastService>()
.Bind<ICounterService>()
.To<CounterService>()
.Root<ICounterService>();
}
```

The composition class inherits from the `ServiceProviderFactory<T>` class, where T is the composition class itself. It depends on the `Base` setup.

Te web application entry point is in the [Program.cs](/samples/BlazorWebAssemblyApp/Program.cs) file:

```c#
var builder = WebAssemblyHostBuilder.CreateDefault(args);

// Uses Composition as an alternative IServiceProviderFactory
builder.ConfigureContainer(new Composition());
```

The [project file](/samples/BlazorWebAssemblyApp/BlazorWebAssemblyApp.csproj) looks like this:

```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pure.DI" Version="$(version)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pure.DI.MS" Version="$(version)" />
</ItemGroup>
</Project>
```

It contains 2 additional links to NuGet packages:

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

[![NuGet](https://buildstats.info/nuget/Pure.DI.MS)](https://www.nuget.org/packages/Pure.DI.MS)
2 changes: 2 additions & 0 deletions readme/FooterTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- [Web](readme/WebApp.md)
- [Web API service](readme/WebAPI.md)
- [gRPC service](readme/GrpcService.md)
- [Blazor Server](readme/BlazorServerApp.md)
- [Blazor WebAssembly](readme/BlazorWebAssemblyApp.md)

## Generated Code

Expand Down
5 changes: 4 additions & 1 deletion readme/WebAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ internal partial class Composition: ServiceProviderFactory<Composition>
private static void Setup() =>
DI.Setup(nameof(Composition))
.DependsOn(Base)
.Root<HomeController>();
.Bind<IWeatherForecastService>()
.As(Singleton)
.To<WeatherForecastService>()
.Root<WeatherForecastController>();
}
```

Expand Down
5 changes: 4 additions & 1 deletion readme/WebAPIPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ internal partial class Composition: ServiceProviderFactory<Composition>
private static void Setup() =>
DI.Setup(nameof(Composition))
.DependsOn(Base)
.Root<HomeController>();
.Bind<IWeatherForecastService>()
.As(Singleton)
.To<WeatherForecastService>()
.Root<WeatherForecastController>();
}
```

Expand Down
12 changes: 12 additions & 0 deletions samples/BlazorServerApp/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
15 changes: 15 additions & 0 deletions samples/BlazorServerApp/BlazorServerApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<ItemGroup>
<ProjectReference Include="..\WeatherForecast\WeatherForecast.csproj"/>
<ProjectReference Include="..\..\src\Pure.DI.Core\Pure.DI.Core.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\..\src\Pure.DI\Pure.DI.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
<Compile Include="..\..\src\Pure.DI.MS\ServiceCollectionFactory.g.cs">
<Link>ServiceCollectionFactory.g.cs</Link>
</Compile>
<Compile Include="..\..\src\Pure.DI.MS\ServiceProviderFactory.g.cs">
<Link>ServiceProviderFactory.g.cs</Link>
</Compile>
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions samples/BlazorServerApp/Composition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// ReSharper disable UnusedMember.Local

namespace BlazorServerApp;

using Models;
using Pure.DI;
using Pure.DI.MS;
using WeatherForecast;
using static Pure.DI.Lifetime;

internal partial class Composition: ServiceProviderFactory<Composition>
{
private static void Setup() =>
DI.Setup(nameof(Composition))
.DependsOn(Base)
.Bind<IWeatherForecastService>()
.As(Singleton)
.To<WeatherForecastService>()
.Root<IWeatherForecastService>()
.Bind<ICounterService>()
.To<CounterService>()
.Root<ICounterService>()
.Bind<IErrorModel>()
.To<ErrorModel>()
.Root<IErrorModel>();
}
Loading

0 comments on commit 4b04b5c

Please sign in to comment.