Skip to content

Commit 837f067

Browse files
committed
Initial version
1 parent 3e783ec commit 837f067

File tree

139 files changed

+64992
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+64992
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,5 @@ FodyWeavers.xsd
398398

399399
# JetBrains Rider
400400
*.sln.iml
401+
.idea
402+

CodeFirstApi.sln

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.35821.62 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeFirstApi.Generator", "src\CodeFirstApi.Generator\CodeFirstApi.Generator.csproj", "{7DAA6C1C-F0F8-4C1A-B7DE-889C4895155F}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeFirstApi.Core", "src\CodeFirstApi.Core\CodeFirstApi.Core.csproj", "{99170F5C-7544-4AA3-BEF9-DDF8FB8479DA}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3D04D386-924F-4806-B518-24A043A0BE12}"
11+
EndProject
12+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{5F077FC5-A9E9-4AE8-BED3-058F580C0087}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp.Shared", "samples\BlazorApp.Shared\BlazorApp.Shared.csproj", "{C9255E21-4733-40B3-BA9C-1F2EBC2D9759}"
15+
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp", "samples\BlazorApp\BlazorApp.csproj", "{646F61A1-20FC-4922-9181-D72DA69F50DA}"
17+
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp.Client", "samples\BlazorApp.Client\BlazorApp.Client.csproj", "{F614D232-D1A6-49C7-AC52-56E969345E65}"
19+
EndProject
20+
21+
Global
22+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
23+
Debug|Any CPU = Debug|Any CPU
24+
Release|Any CPU = Release|Any CPU
25+
EndGlobalSection
26+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
27+
{7DAA6C1C-F0F8-4C1A-B7DE-889C4895155F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28+
{7DAA6C1C-F0F8-4C1A-B7DE-889C4895155F}.Debug|Any CPU.Build.0 = Debug|Any CPU
29+
{7DAA6C1C-F0F8-4C1A-B7DE-889C4895155F}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{7DAA6C1C-F0F8-4C1A-B7DE-889C4895155F}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{99170F5C-7544-4AA3-BEF9-DDF8FB8479DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{99170F5C-7544-4AA3-BEF9-DDF8FB8479DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{99170F5C-7544-4AA3-BEF9-DDF8FB8479DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{99170F5C-7544-4AA3-BEF9-DDF8FB8479DA}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{C9255E21-4733-40B3-BA9C-1F2EBC2D9759}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{C9255E21-4733-40B3-BA9C-1F2EBC2D9759}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{C9255E21-4733-40B3-BA9C-1F2EBC2D9759}.Release|Any CPU.ActiveCfg = Release|Any CPU
38+
{C9255E21-4733-40B3-BA9C-1F2EBC2D9759}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{646F61A1-20FC-4922-9181-D72DA69F50DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40+
{646F61A1-20FC-4922-9181-D72DA69F50DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
41+
{646F61A1-20FC-4922-9181-D72DA69F50DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{646F61A1-20FC-4922-9181-D72DA69F50DA}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{F614D232-D1A6-49C7-AC52-56E969345E65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44+
{F614D232-D1A6-49C7-AC52-56E969345E65}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{F614D232-D1A6-49C7-AC52-56E969345E65}.Release|Any CPU.ActiveCfg = Release|Any CPU
46+
{F614D232-D1A6-49C7-AC52-56E969345E65}.Release|Any CPU.Build.0 = Release|Any CPU
47+
EndGlobalSection
48+
GlobalSection(SolutionProperties) = preSolution
49+
HideSolutionNode = FALSE
50+
EndGlobalSection
51+
GlobalSection(ExtensibilityGlobals) = postSolution
52+
SolutionGuid = {83BF86F4-3141-4EEB-8255-4BDF0D9F2908}
53+
EndGlobalSection
54+
GlobalSection(NestedProjects) = preSolution
55+
{99170F5C-7544-4AA3-BEF9-DDF8FB8479DA} = {3D04D386-924F-4806-B518-24A043A0BE12}
56+
{7DAA6C1C-F0F8-4C1A-B7DE-889C4895155F} = {3D04D386-924F-4806-B518-24A043A0BE12}
57+
{C9255E21-4733-40B3-BA9C-1F2EBC2D9759} = {5F077FC5-A9E9-4AE8-BED3-058F580C0087}
58+
{646F61A1-20FC-4922-9181-D72DA69F50DA} = {5F077FC5-A9E9-4AE8-BED3-058F580C0087}
59+
{F614D232-D1A6-49C7-AC52-56E969345E65} = {5F077FC5-A9E9-4AE8-BED3-058F580C0087}
60+
EndGlobalSection
61+
EndGlobal

readme.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# CodeFirstApi
2+
3+
Mainly a generator project, to create REST API and appropriate interfaces for easy working in Blazor InteractiveAuto render mode.
4+
- Based on interface definition in Shared project
5+
- Create Controller based REST API, to be used in Backend (respecting Authorize attribute).
6+
- Create a client to be used in Razor Components in WebAssembly Render mode, which calls the REST API above.
7+
- Add wrapper around the service implmentation in Backend for use in ServerSide Render mode, so it is protected as the REST API is.
8+
- For the wrappers used when running on the server and in browser, to automatically implement State Persistance for use in Prerendering (to remove the flicker on mode change with prerendering)
9+
10+
# Usage
11+
See the project in the `sample` folder.
12+
13+
# Version info
14+
This is only pre-alpha version for evaluating the idea, and not yet ready for production use.
15+
Nuget package will not be released later on when the code is a bit more stable.
16+
17+
18+
## Next TODOs
19+
20+
- add route attribute support
21+
- use arguments that are mentioned in route direct instead of JSON
22+
- better handling of state persistance key for arguments (using JSON of arguments now, which is not optimal)
23+
- add Verb attribute (can be combined with route)
24+
- add allow anonymous attribute
25+
- release as Nuget package
26+
27+
28+
29+
## Similar
30+
31+
# https://github.com/reactiveui/refit
32+
- Generate a REST client from code
33+
- ie code first but only for REST Client, not for server side
34+
- Seems alternative to Swagger somehow
35+
36+
# https://github.com/canton7/RestEase
37+
- Inspired by Refit
38+
- Seems generally the same, generate clients
39+
40+
41+
https://github.com/bpawluk/EasyApi
42+
- Handles both client and server side
43+
- But each request is a separate class
44+
- A bit too verbose due to that
45+
- And due to the class per method, quite a lot needs to be injected to use adjecent functionality
46+
- the `@inject ICall<AddComment, Guid> AddComment` is a good example of that
47+
- you need to know what request class you want to use and what it should return.
48+
- This seems unnecessary.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using BlazorApp.Shared;
2+
using CodeFirstApi.Core;
3+
4+
// CodeFirstApi
5+
[assembly: GenerateHttpClients(typeof(IClass1))]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
8+
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.3"/>
13+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="9.0.3"/>
14+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.4" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\src\CodeFirstApi.Core\CodeFirstApi.Core.csproj" />
19+
<ProjectReference Include="..\..\src\CodeFirstApi.Generator\CodeFirstApi.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
20+
<ProjectReference Include="..\BlazorApp.Shared\BlazorApp.Shared.csproj" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@inherits LayoutComponentBase
2+
3+
<div class="page">
4+
<div class="sidebar">
5+
<NavMenu/>
6+
</div>
7+
8+
<main>
9+
<div class="top-row px-4">
10+
<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
11+
</div>
12+
13+
<article class="content px-4">
14+
@Body
15+
</article>
16+
</main>
17+
</div>
18+
19+
<div id="blazor-error-ui" data-nosnippet>
20+
An unhandled error has occurred.
21+
<a href="." class="reload">Reload</a>
22+
<span class="dismiss">🗙</span>
23+
</div>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
.page {
2+
position: relative;
3+
display: flex;
4+
flex-direction: column;
5+
}
6+
7+
main {
8+
flex: 1;
9+
}
10+
11+
.sidebar {
12+
background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%);
13+
}
14+
15+
.top-row {
16+
background-color: #f7f7f7;
17+
border-bottom: 1px solid #d6d5d5;
18+
justify-content: flex-end;
19+
height: 3.5rem;
20+
display: flex;
21+
align-items: center;
22+
}
23+
24+
.top-row ::deep a, .top-row ::deep .btn-link {
25+
white-space: nowrap;
26+
margin-left: 1.5rem;
27+
text-decoration: none;
28+
}
29+
30+
.top-row ::deep a:hover, .top-row ::deep .btn-link:hover {
31+
text-decoration: underline;
32+
}
33+
34+
.top-row ::deep a:first-child {
35+
overflow: hidden;
36+
text-overflow: ellipsis;
37+
}
38+
39+
@media (max-width: 640.98px) {
40+
.top-row {
41+
justify-content: space-between;
42+
}
43+
44+
.top-row ::deep a, .top-row ::deep .btn-link {
45+
margin-left: 0;
46+
}
47+
}
48+
49+
@media (min-width: 641px) {
50+
.page {
51+
flex-direction: row;
52+
}
53+
54+
.sidebar {
55+
width: 250px;
56+
height: 100vh;
57+
position: sticky;
58+
top: 0;
59+
}
60+
61+
.top-row {
62+
position: sticky;
63+
top: 0;
64+
z-index: 1;
65+
}
66+
67+
.top-row.auth ::deep a:first-child {
68+
flex: 1;
69+
text-align: right;
70+
width: 0;
71+
}
72+
73+
.top-row, article {
74+
padding-left: 2rem !important;
75+
padding-right: 1.5rem !important;
76+
}
77+
}
78+
79+
#blazor-error-ui {
80+
color-scheme: light only;
81+
background: lightyellow;
82+
bottom: 0;
83+
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2);
84+
box-sizing: border-box;
85+
display: none;
86+
left: 0;
87+
padding: 0.6rem 1.25rem 0.7rem 1.25rem;
88+
position: fixed;
89+
width: 100%;
90+
z-index: 1000;
91+
}
92+
93+
#blazor-error-ui .dismiss {
94+
cursor: pointer;
95+
position: absolute;
96+
right: 0.75rem;
97+
top: 0.5rem;
98+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
@implements IDisposable
2+
3+
@inject NavigationManager NavigationManager
4+
5+
<div class="top-row ps-3 navbar navbar-dark">
6+
<div class="container-fluid">
7+
<a class="navbar-brand" href="">BlazorApp</a>
8+
</div>
9+
</div>
10+
11+
<input type="checkbox" title="Navigation menu" class="navbar-toggler"/>
12+
13+
<div class="nav-scrollable" onclick="document.querySelector('.navbar-toggler').click()">
14+
<nav class="nav flex-column">
15+
<div class="nav-item px-3">
16+
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
17+
<span class="bi bi-house-door-fill-nav-menu" aria-hidden="true"></span> Home
18+
</NavLink>
19+
</div>
20+
21+
<div class="nav-item px-3">
22+
<NavLink class="nav-link" href="counter">
23+
<span class="bi bi-plus-square-fill-nav-menu" aria-hidden="true"></span> Counter
24+
</NavLink>
25+
</div>
26+
27+
<div class="nav-item px-3">
28+
<NavLink class="nav-link" href="weather">
29+
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Weather
30+
</NavLink>
31+
</div>
32+
33+
<div class="nav-item px-3">
34+
<NavLink class="nav-link" href="auth">
35+
<span class="bi bi-lock-nav-menu" aria-hidden="true"></span> Auth Required
36+
</NavLink>
37+
</div>
38+
39+
<AuthorizeView>
40+
<Authorized>
41+
<div class="nav-item px-3">
42+
<NavLink class="nav-link" href="Account/Manage">
43+
<span class="bi bi-person-fill-nav-menu" aria-hidden="true"></span> @context.User.Identity?.Name
44+
</NavLink>
45+
</div>
46+
<div class="nav-item px-3">
47+
<form action="Account/Logout" method="post">
48+
<AntiforgeryToken/>
49+
<input type="hidden" name="ReturnUrl" value="@currentUrl"/>
50+
<button type="submit" class="nav-link">
51+
<span class="bi bi-arrow-bar-left-nav-menu" aria-hidden="true"></span> Logout
52+
</button>
53+
</form>
54+
</div>
55+
</Authorized>
56+
<NotAuthorized>
57+
<div class="nav-item px-3">
58+
<NavLink class="nav-link" href="Account/Register">
59+
<span class="bi bi-person-nav-menu" aria-hidden="true"></span> Register
60+
</NavLink>
61+
</div>
62+
<div class="nav-item px-3">
63+
<NavLink class="nav-link" href="Account/Login">
64+
<span class="bi bi-person-badge-nav-menu" aria-hidden="true"></span> Login
65+
</NavLink>
66+
</div>
67+
</NotAuthorized>
68+
</AuthorizeView>
69+
</nav>
70+
</div>
71+
72+
@code {
73+
private string? currentUrl;
74+
75+
protected override void OnInitialized()
76+
{
77+
currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
78+
NavigationManager.LocationChanged += OnLocationChanged;
79+
}
80+
81+
private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
82+
{
83+
currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
84+
StateHasChanged();
85+
}
86+
87+
public void Dispose()
88+
{
89+
NavigationManager.LocationChanged -= OnLocationChanged;
90+
}
91+
92+
}

0 commit comments

Comments
 (0)