Skip to content

Commit

Permalink
docs: Add sample project about native AOT (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 authored Mar 16, 2024
1 parent a221c5e commit 8df856a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
14 changes: 10 additions & 4 deletions SimpleResults.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleResults.Example", "sa
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleResults.Example.AspNetCore", "samples\SimpleResults.Example.AspNetCore\SimpleResults.Example.AspNetCore.csproj", "{982B1B0B-913C-4704-8BED-530CB1993825}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleResults.Example.NativeAOT", "samples\SimpleResults.Example.NativeAOT\SimpleResults.Example.NativeAOT.csproj", "{0D139C5D-CEDB-4A5F-8FBB-5A39B8599E3F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleResults.Example.Web.Tests", "samples\SimpleResults.Example.Web.Tests\SimpleResults.Example.Web.Tests.csproj", "{AD21137D-445E-4206-A631-28E9ACA5EC3F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleResults.Tests", "tests\SimpleResults.Tests.csproj", "{4DEA2EC9-C0CC-4196-9B14-FA2333D4656A}"
Expand All @@ -29,6 +31,10 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E197E10F-F783-4BC4-A08A-627302FC2F47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E197E10F-F783-4BC4-A08A-627302FC2F47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E197E10F-F783-4BC4-A08A-627302FC2F47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E197E10F-F783-4BC4-A08A-627302FC2F47}.Release|Any CPU.Build.0 = Release|Any CPU
{DF0D8C28-5DDF-4DF3-B443-D2C55B5C7F1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF0D8C28-5DDF-4DF3-B443-D2C55B5C7F1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF0D8C28-5DDF-4DF3-B443-D2C55B5C7F1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -53,10 +59,10 @@ Global
{4DEA2EC9-C0CC-4196-9B14-FA2333D4656A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DEA2EC9-C0CC-4196-9B14-FA2333D4656A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DEA2EC9-C0CC-4196-9B14-FA2333D4656A}.Release|Any CPU.Build.0 = Release|Any CPU
{E197E10F-F783-4BC4-A08A-627302FC2F47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E197E10F-F783-4BC4-A08A-627302FC2F47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E197E10F-F783-4BC4-A08A-627302FC2F47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E197E10F-F783-4BC4-A08A-627302FC2F47}.Release|Any CPU.Build.0 = Release|Any CPU
{0D139C5D-CEDB-4A5F-8FBB-5A39B8599E3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0D139C5D-CEDB-4A5F-8FBB-5A39B8599E3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0D139C5D-CEDB-4A5F-8FBB-5A39B8599E3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0D139C5D-CEDB-4A5F-8FBB-5A39B8599E3F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 7 additions & 0 deletions samples/SimpleResults.Example.NativeAOT/CustomContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SimpleResults.Example.NativeAOT;

[JsonSourceGenerationOptions(
WriteIndented = true,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(ListedResult<User>))]
public partial class CustomContext : JsonSerializerContext { }
6 changes: 6 additions & 0 deletions samples/SimpleResults.Example.NativeAOT/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global using SimpleResults;
global using SimpleResults.Example;
global using SimpleResults.Example.NativeAOT;
global using System.Globalization;
global using System.Text.Json;
global using System.Text.Json.Serialization;
27 changes: 27 additions & 0 deletions samples/SimpleResults.Example.NativeAOT/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en");
var users = new List<User>
{
new() { Id = "1", Name = "Bob" },
new() { Id = "2", Name = "Alice" }
};
var userService = new UserService(users);
ListedResult<User> result = userService.GetAll();

var json = JsonSerializer.Serialize(
result,
CustomContext.Default.ListedResultUser);

Console.WriteLine(json);

var deserializedResult = JsonSerializer.Deserialize(
json,
CustomContext.Default.ListedResultUser);

Console.WriteLine();
Console.WriteLine("Status: " + deserializedResult.Status);
foreach(var user in deserializedResult.Data)
{
Console.WriteLine($"Id: {user.Id}, Name: {user.Name}");
}

Console.ReadLine();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SimpleResults.Example\SimpleResults.Example.csproj" />
</ItemGroup>

</Project>

0 comments on commit 8df856a

Please sign in to comment.