Compare commits

..

1 Commits

Author SHA1 Message Date
a21cd0cea2 Merge commit '47cbdc21c1bf95d78f86760bf6ec85dab0ee4774' 2025-07-11 14:36:35 +08:00
26 changed files with 101 additions and 783 deletions

View File

@ -1,15 +1,16 @@
namespace OptixServe.Api.Configuration;
public record ApiConfiguration
public record OptixServeSettings
{
public ApiSettings? Api { get; set; } = new();
public DatabaseSettings? Database { get; set; } = new();
public JwtSettings? Jwt { get; set; } = new();
}
public record ApiSettings
{
public string? Listen { get; set; }
public int? Port { get; set; }
public string? Listen { get; set; } = "127.0.0.1";
public int? Port { get; set; } = 10086;
}
public record JwtSettings
@ -19,3 +20,15 @@ public record JwtSettings
public string Audience { get; set; } = "OptixServeUsers";
public int TokenExpirationMinutes { get; set; } = 60;
}
public enum DatabaseType
{
Sqlite,
MySQL
}
public record DatabaseSettings
{
public DatabaseType Type { get; set; } = DatabaseType.Sqlite;
public string? Host { get; set; }
}

View File

@ -1,18 +1,18 @@
using Microsoft.Extensions.Configuration;
using System;
namespace OptixServe.Infrastructure.Configuration;
namespace OptixServe.Api.Configuration;
public static class ConfigurationHelper
{
public static IConfigurationBuilder CreateDefaultBuilder(string basePath)
public static IConfigurationBuilder CreateDefaultBuilder()
{
var aspEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var netEnv = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
// Console.WriteLine($"ASPNETCORE_ENVIRONMENT: {aspEnv}, DOTNET_ENVIRONMENT: {netEnv}");
var env = aspEnv ?? netEnv ?? null;
var builder = new ConfigurationBuilder()
// .SetBasePath(Directory.GetCurrentDirectory())
.SetBasePath(basePath)
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile("config.json", optional: true);
@ -24,9 +24,4 @@ public static class ConfigurationHelper
return builder;
}
public static IConfigurationBuilder CreateDefaultBuilder()
{
return CreateDefaultBuilder(Directory.GetCurrentDirectory());
}
}
}

View File

@ -1,7 +1,8 @@
using System.Text.Json.Serialization;
using OptixServe.Application.Services;
using OptixServe.Core.Services;
using OptixServe.Api.Dtos;
using OptixServe.Api.Services;
using Microsoft.AspNetCore.Authorization;
namespace OptixServe.Api.Endpoints;
@ -9,7 +10,7 @@ namespace OptixServe.Api.Endpoints;
[JsonSerializable(typeof(UserDto))]
[JsonSerializable(typeof(IEnumerable<UserDto>))]
[JsonSerializable(typeof(LoginRequestDto))]
[JsonSerializable(typeof(LoginResponseDto))]
[JsonSerializable(typeof(LoginResponseDto))] // For returning the token string
public partial class UserJsonContext : JsonSerializerContext { }
public static class UserEndpoint

View File

@ -1,6 +1,6 @@
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using OptixServe.Infrastructure.Configuration;
using OptixServe.Api.Configuration;
using OptixServe.Api.Dtos;
namespace OptixServe.Api.Endpoints;
@ -22,14 +22,14 @@ public static class VersionEndpoint
var group = parentGroup.MapGroup("/version");
group.MapGet("/", () => "v1");
group.MapGet("/test/dbconfig", (IOptions<InfrastructureConfiguration> infrastructureConfig) =>
group.MapGet("/test/dbconfig", (IOptions<OptixServeSettings> appSettings) =>
{
var dbType = infrastructureConfig.Value.Database?.Type;
var dbHost = infrastructureConfig.Value.Database?.Host;
var dbType = appSettings.Value.Database?.Type;
var dbHost = appSettings.Value.Database?.Host;
return Results.Ok(new CommonErrorDto
{
Message = $"Set up {dbType} database on {dbHost}"
});
});
}
}
}

View File

@ -2,13 +2,16 @@
<ItemGroup>
<ProjectReference Include="..\OptixServe.Core\OptixServe.Core.csproj" />
<ProjectReference Include="..\OptixServe.Application\OptixServe.Application.csproj" />
<ProjectReference Include="..\OptixServe.Infrastructure\OptixServe.Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.6" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25306.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.6" />
</ItemGroup>
<PropertyGroup>
@ -16,7 +19,7 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<PublishAot>true</PublishAot>
<PublishAot>false</PublishAot>
</PropertyGroup>
</Project>

View File

@ -5,10 +5,9 @@ using Microsoft.IdentityModel.Tokens;
using OptixServe.Api.Configuration;
using OptixServe.Api.Endpoints;
using OptixServe.Api.Services;
using OptixServe.Application.Services;
using OptixServe.Infrastructure.Configuration;
using OptixServe.Infrastructure.Data;
using OptixServe.Infrastructure.Utilites;
using OptixServe.Core.Data;
using OptixServe.Core.Services;
using OptixServe.Api.Utilites;
class Program
{
@ -90,7 +89,12 @@ static class StartupHelper
public static void AddConfigurationWithCommand(this WebApplicationBuilder builder, FileInfo? file)
{
// Configure configuration sources in specified order
var configurationBuilder = ConfigurationHelper.CreateDefaultBuilder();
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
.AddJsonFile("config.json", optional: true)
.AddJsonFile($"config.{builder.Environment.EnvironmentName}.json", optional: true);
// Add command-line specified config file if provided
if (file != null)
@ -119,17 +123,13 @@ static class StartupHelper
/// <param name="builder">WebApplicationBuilder instance</param>
public static void RegisterServices(this WebApplicationBuilder builder)
{
// Add configuration classes
var apiConfigSection = builder.Configuration.GetSection("OptixServe:Api");
builder.Services.Configure<ApiConfiguration>(apiConfigSection);
var apiConfig = apiConfigSection.Get<ApiConfiguration>();
var infraConfigSection = builder.Configuration.GetSection("OptixServe:Infrastructure");
builder.Services.Configure<InfrastructureConfiguration>(infraConfigSection);
var infraConfig = infraConfigSection.Get<InfrastructureConfiguration>();
// Add configuration class
var optixSettigns = builder.Configuration.GetSection("OptixServe");
var onConfigSettings = optixSettigns.Get<OptixServeSettings>();
builder.Services.Configure<OptixServeSettings>(optixSettigns);
// Add DBContext class
builder.Services.AddAppDatabase(infraConfig?.Database!);
builder.Services.AddAppDatabase(onConfigSettings?.Database!);
builder.Services.AddScoped<DbInitializer>();
// Application services
@ -140,7 +140,7 @@ static class StartupHelper
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
var jwtSettings = apiConfig?.Jwt ?? throw new ArgumentNullException(nameof(builder), "JWT settings are not configured.");
var jwtSettings = onConfigSettings?.Jwt ?? throw new ArgumentNullException(nameof(builder), "JWT settings are not configured.");
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
@ -178,4 +178,4 @@ static class StartupHelper
VersionEndpoint.Register(rootGroup);
}
}
}

View File

@ -13,9 +13,9 @@ public interface ITokenService
public string GenerateToken(User user);
}
public class TokenService(IOptions<ApiConfiguration> apiConfiguration) : ITokenService
public class TokenService(IOptions<OptixServeSettings> optixServeSettings) : ITokenService
{
private readonly JwtSettings _jwtSettings = apiConfiguration.Value.Jwt ?? throw new ArgumentNullException(nameof(apiConfiguration), "JWT settings are not configured.");
private readonly JwtSettings _jwtSettings = optixServeSettings.Value.Jwt ?? throw new ArgumentNullException(nameof(optixServeSettings), "JWT settings are not configured.");
public string GenerateToken(User user)
{
@ -41,4 +41,4 @@ public class TokenService(IOptions<ApiConfiguration> apiConfiguration) : ITokenS
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
}
}

View File

@ -1,8 +1,8 @@
using Microsoft.EntityFrameworkCore;
using OptixServe.Infrastructure.Configuration;
using OptixServe.Infrastructure.Data.CompiledModels;
using OptixServe.Api.Configuration;
using OptixServe.Core.Data;
namespace OptixServe.Infrastructure.Utilites;
namespace OptixServe.Api.Utilites;
public static class DatabaseHelper
{
@ -23,8 +23,7 @@ public static class DatabaseHelper
var dbPath = dbSettings.Host ?? "optixserve.db";
var connectionString = $"Data Source={dbPath}";
options.UseSqlite(connectionString, b => b.MigrationsAssembly("OptixServe.Infrastructure"))
.UseModel(AppDbContextModel.Instance);
options.UseSqlite(connectionString, b => b.MigrationsAssembly("OptixServe.Api"));
}
else
@ -32,4 +31,4 @@ public static class DatabaseHelper
throw new NotImplementedException("Only SQLite database is currently supported");
}
}
}
}

View File

@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using OptixServe.Api.Configuration;
using OptixServe.Core.Data;
namespace OptixServe.Api.Utilites;
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext(string[] args)
{
var configuration = ConfigurationHelper.CreateDefaultBuilder().Build();
var dbSettings = configuration.GetSection("OptixServe:Database").Get<DatabaseSettings>()!;
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
DatabaseHelper.ConfigureDbContext(optionsBuilder, dbSettings);
return new AppDbContext(optionsBuilder.Options);
}
}

View File

@ -9,19 +9,17 @@
"OptixServe": {
"Api": {
"Listen": "0.0.0.0",
"Port": "54321",
"Jwt": {
"Secret": "YOUR_SECRET_KEY_HERE_DO_NOT_SHARE_THIS_AND_MAKE_IT_LONG_ENOUGH",
"Issuer": "OptixServe",
"Audience": "OptixServeUsers",
"TokenExpirationMinutes": 60
}
"Port": "54321"
},
"Infrastructure": {
"Database": {
"Type": "Sqlite",
"Host": "optixserve.db"
}
"Database": {
"Type": "Sqlite",
"Host": "optixserve.db"
},
"Jwt": {
"Secret": "YOUR_SECRET_KEY_HERE_DO_NOT_SHARE_THIS_AND_MAKE_IT_LONG_ENOUGH",
"Issuer": "OptixServe",
"Audience": "OptixServeUsers",
"TokenExpirationMinutes": 60
}
}
}
}

View File

@ -1,18 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\OptixServe.Core\OptixServe.Core.csproj" />
<ProjectReference Include="..\OptixServe.Infrastructure\OptixServe.Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.6" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -1,7 +1,7 @@
using Microsoft.EntityFrameworkCore;
using OptixServe.Core.Models;
namespace OptixServe.Infrastructure.Data;
namespace OptixServe.Core.Data;
public class AppDbContext(DbContextOptions options) : DbContext(options)
{
@ -18,4 +18,4 @@ public class AppDbContext(DbContextOptions options) : DbContext(options)
new() {Id = "1", UserName = "admin", Password = "admin12345"}
]);
}
}
}

View File

@ -1,4 +1,4 @@
namespace OptixServe.Infrastructure.Data;
namespace OptixServe.Core.Data;
public class DbInitializer(AppDbContext dbContext)
{
@ -8,4 +8,4 @@ public class DbInitializer(AppDbContext dbContext)
{
_context.Database.EnsureCreated();
}
}
}

View File

@ -6,5 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.6" />
</ItemGroup>
</Project>

View File

@ -1,7 +1,7 @@
using OptixServe.Core.Data;
using OptixServe.Core.Models;
using OptixServe.Infrastructure.Data;
namespace OptixServe.Application.Services;
namespace OptixServe.Core.Services;
public interface IUserService
{
@ -28,4 +28,4 @@ public class UserService(AppDbContext dbContext) : IUserService
{
return _dbContext.Users.AsEnumerable();
}
}
}

View File

@ -1,18 +0,0 @@
namespace OptixServe.Infrastructure.Configuration;
public record InfrastructureConfiguration
{
public DatabaseSettings? Database { get; set; } = new();
}
public enum DatabaseType
{
Sqlite,
MySQL
}
public record DatabaseSettings
{
public DatabaseType Type { get; set; } = DatabaseType.Sqlite;
public string? Host { get; set; }
}

View File

@ -1,9 +0,0 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore.Infrastructure;
using OptixServe.Infrastructure.Data;
using OptixServe.Infrastructure.Data.CompiledModels;
#pragma warning disable 219, 612, 618
#nullable disable
[assembly: DbContextModel(typeof(AppDbContext), typeof(AppDbContextModel))]

View File

@ -1,47 +0,0 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
#pragma warning disable 219, 612, 618
#nullable disable
namespace OptixServe.Infrastructure.Data.CompiledModels
{
[DbContext(typeof(AppDbContext))]
public partial class AppDbContextModel : RuntimeModel
{
private static readonly bool _useOldBehavior31751 =
System.AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue31751", out var enabled31751) && enabled31751;
static AppDbContextModel()
{
var model = new AppDbContextModel();
if (_useOldBehavior31751)
{
model.Initialize();
}
else
{
var thread = new System.Threading.Thread(RunInitialization, 10 * 1024 * 1024);
thread.Start();
thread.Join();
void RunInitialization()
{
model.Initialize();
}
}
model.Customize();
_instance = (AppDbContextModel)model.FinalizeModel();
}
private static AppDbContextModel _instance;
public static IModel Instance => _instance;
partial void Initialize();
partial void Customize();
}
}

View File

@ -1,99 +0,0 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Update.Internal;
#pragma warning disable 219, 612, 618
#nullable disable
namespace OptixServe.Infrastructure.Data.CompiledModels
{
public partial class AppDbContextModel
{
private AppDbContextModel()
: base(skipDetectChanges: false, modelId: new Guid("3214a553-d69f-4844-a587-080f6d463671"), entityTypeCount: 1)
{
}
partial void Initialize()
{
var user = UserEntityType.Create(this);
UserEntityType.CreateAnnotations(user);
AddAnnotation("ProductVersion", "9.0.6");
AddRuntimeAnnotation("Relational:RelationalModelFactory", () => CreateRelationalModel());
}
private IRelationalModel CreateRelationalModel()
{
var relationalModel = new RelationalModel(this);
var user = FindEntityType("OptixServe.Core.Models.User")!;
var defaultTableMappings = new List<TableMappingBase<ColumnMappingBase>>();
user.SetRuntimeAnnotation("Relational:DefaultMappings", defaultTableMappings);
var optixServeCoreModelsUserTableBase = new TableBase("OptixServe.Core.Models.User", null, relationalModel);
var idColumnBase = new ColumnBase<ColumnMappingBase>("Id", "TEXT", optixServeCoreModelsUserTableBase);
optixServeCoreModelsUserTableBase.Columns.Add("Id", idColumnBase);
var passwordColumnBase = new ColumnBase<ColumnMappingBase>("Password", "TEXT", optixServeCoreModelsUserTableBase)
{
IsNullable = true
};
optixServeCoreModelsUserTableBase.Columns.Add("Password", passwordColumnBase);
var privilegeGroupColumnBase = new ColumnBase<ColumnMappingBase>("PrivilegeGroup", "INTEGER", optixServeCoreModelsUserTableBase);
optixServeCoreModelsUserTableBase.Columns.Add("PrivilegeGroup", privilegeGroupColumnBase);
var userNameColumnBase = new ColumnBase<ColumnMappingBase>("UserName", "TEXT", optixServeCoreModelsUserTableBase);
optixServeCoreModelsUserTableBase.Columns.Add("UserName", userNameColumnBase);
relationalModel.DefaultTables.Add("OptixServe.Core.Models.User", optixServeCoreModelsUserTableBase);
var optixServeCoreModelsUserMappingBase = new TableMappingBase<ColumnMappingBase>(user, optixServeCoreModelsUserTableBase, null);
optixServeCoreModelsUserTableBase.AddTypeMapping(optixServeCoreModelsUserMappingBase, false);
defaultTableMappings.Add(optixServeCoreModelsUserMappingBase);
RelationalModel.CreateColumnMapping((ColumnBase<ColumnMappingBase>)idColumnBase, user.FindProperty("Id")!, optixServeCoreModelsUserMappingBase);
RelationalModel.CreateColumnMapping((ColumnBase<ColumnMappingBase>)passwordColumnBase, user.FindProperty("Password")!, optixServeCoreModelsUserMappingBase);
RelationalModel.CreateColumnMapping((ColumnBase<ColumnMappingBase>)privilegeGroupColumnBase, user.FindProperty("PrivilegeGroup")!, optixServeCoreModelsUserMappingBase);
RelationalModel.CreateColumnMapping((ColumnBase<ColumnMappingBase>)userNameColumnBase, user.FindProperty("UserName")!, optixServeCoreModelsUserMappingBase);
var tableMappings = new List<TableMapping>();
user.SetRuntimeAnnotation("Relational:TableMappings", tableMappings);
var usersTable = new Table("Users", null, relationalModel);
var idColumn = new Column("Id", "TEXT", usersTable);
usersTable.Columns.Add("Id", idColumn);
idColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<string>(idColumn);
var passwordColumn = new Column("Password", "TEXT", usersTable)
{
IsNullable = true
};
usersTable.Columns.Add("Password", passwordColumn);
passwordColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<string>(passwordColumn);
var privilegeGroupColumn = new Column("PrivilegeGroup", "INTEGER", usersTable);
usersTable.Columns.Add("PrivilegeGroup", privilegeGroupColumn);
privilegeGroupColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<int>(privilegeGroupColumn);
var userNameColumn = new Column("UserName", "TEXT", usersTable);
usersTable.Columns.Add("UserName", userNameColumn);
userNameColumn.Accessors = ColumnAccessorsFactory.CreateGeneric<string>(userNameColumn);
relationalModel.Tables.Add(("Users", null), usersTable);
var usersTableMapping = new TableMapping(user, usersTable, null);
usersTable.AddTypeMapping(usersTableMapping, false);
tableMappings.Add(usersTableMapping);
RelationalModel.CreateColumnMapping(idColumn, user.FindProperty("Id")!, usersTableMapping);
RelationalModel.CreateColumnMapping(passwordColumn, user.FindProperty("Password")!, usersTableMapping);
RelationalModel.CreateColumnMapping(privilegeGroupColumn, user.FindProperty("PrivilegeGroup")!, usersTableMapping);
RelationalModel.CreateColumnMapping(userNameColumn, user.FindProperty("UserName")!, usersTableMapping);
var pK_Users = new UniqueConstraint("PK_Users", usersTable, new[] { idColumn });
usersTable.PrimaryKey = pK_Users;
pK_Users.SetRowKeyValueFactory(new SimpleRowKeyValueFactory<string>(pK_Users));
var pK_UsersKey = RelationalModel.GetKey(this,
"OptixServe.Core.Models.User",
new[] { "Id" });
pK_Users.MappedKeys.Add(pK_UsersKey);
RelationalModel.GetOrCreateUniqueConstraints(pK_UsersKey).Add(pK_Users);
usersTable.UniqueConstraints.Add("PK_Users", pK_Users);
return relationalModel.MakeReadOnly();
}
}
}

View File

@ -1,227 +0,0 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Json;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using OptixServe.Core.Models;
#pragma warning disable 219, 612, 618
#nullable disable
namespace OptixServe.Infrastructure.Data.CompiledModels
{
[EntityFrameworkInternal]
public partial class UserEntityType
{
public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType baseEntityType = null)
{
var runtimeEntityType = model.AddEntityType(
"OptixServe.Core.Models.User",
typeof(User),
baseEntityType,
propertyCount: 4,
keyCount: 1);
var id = runtimeEntityType.AddProperty(
"Id",
typeof(string),
propertyInfo: typeof(User).GetProperty("Id", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(User).GetField("<Id>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly),
afterSaveBehavior: PropertySaveBehavior.Throw);
id.SetGetter(
string (User entity) => UserUnsafeAccessors.Id(entity),
bool (User entity) => UserUnsafeAccessors.Id(entity) == null,
string (User instance) => UserUnsafeAccessors.Id(instance),
bool (User instance) => UserUnsafeAccessors.Id(instance) == null);
id.SetSetter(
(User entity, string value) => UserUnsafeAccessors.Id(entity) = value);
id.SetMaterializationSetter(
(User entity, string value) => UserUnsafeAccessors.Id(entity) = value);
id.SetAccessors(
string (InternalEntityEntry entry) => UserUnsafeAccessors.Id(((User)(entry.Entity))),
string (InternalEntityEntry entry) => UserUnsafeAccessors.Id(((User)(entry.Entity))),
string (InternalEntityEntry entry) => entry.ReadOriginalValue<string>(id, 0),
string (InternalEntityEntry entry) => entry.ReadRelationshipSnapshotValue<string>(id, 0),
object (ValueBuffer valueBuffer) => valueBuffer[0]);
id.SetPropertyIndexes(
index: 0,
originalValueIndex: 0,
shadowIndex: -1,
relationshipIndex: 0,
storeGenerationIndex: -1);
id.TypeMapping = SqliteStringTypeMapping.Default;
id.SetCurrentValueComparer(new EntryCurrentValueComparer<string>(id));
var password = runtimeEntityType.AddProperty(
"Password",
typeof(string),
propertyInfo: typeof(User).GetProperty("Password", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(User).GetField("<Password>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly),
nullable: true);
password.SetGetter(
string (User entity) => UserUnsafeAccessors.Password(entity),
bool (User entity) => UserUnsafeAccessors.Password(entity) == null,
string (User instance) => UserUnsafeAccessors.Password(instance),
bool (User instance) => UserUnsafeAccessors.Password(instance) == null);
password.SetSetter(
(User entity, string value) => UserUnsafeAccessors.Password(entity) = value);
password.SetMaterializationSetter(
(User entity, string value) => UserUnsafeAccessors.Password(entity) = value);
password.SetAccessors(
string (InternalEntityEntry entry) => UserUnsafeAccessors.Password(((User)(entry.Entity))),
string (InternalEntityEntry entry) => UserUnsafeAccessors.Password(((User)(entry.Entity))),
string (InternalEntityEntry entry) => entry.ReadOriginalValue<string>(password, 1),
string (InternalEntityEntry entry) => entry.GetCurrentValue<string>(password),
object (ValueBuffer valueBuffer) => valueBuffer[1]);
password.SetPropertyIndexes(
index: 1,
originalValueIndex: 1,
shadowIndex: -1,
relationshipIndex: -1,
storeGenerationIndex: -1);
password.TypeMapping = SqliteStringTypeMapping.Default;
var privilegeGroup = runtimeEntityType.AddProperty(
"PrivilegeGroup",
typeof(PrivilegeGroup),
propertyInfo: typeof(User).GetProperty("PrivilegeGroup", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(User).GetField("<PrivilegeGroup>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
privilegeGroup.SetGetter(
PrivilegeGroup (User entity) => UserUnsafeAccessors.PrivilegeGroup(entity),
bool (User entity) => object.Equals(((object)(UserUnsafeAccessors.PrivilegeGroup(entity))), ((object)(PrivilegeGroup.Admin))),
PrivilegeGroup (User instance) => UserUnsafeAccessors.PrivilegeGroup(instance),
bool (User instance) => object.Equals(((object)(UserUnsafeAccessors.PrivilegeGroup(instance))), ((object)(PrivilegeGroup.Admin))));
privilegeGroup.SetSetter(
(User entity, PrivilegeGroup value) => UserUnsafeAccessors.PrivilegeGroup(entity) = value);
privilegeGroup.SetMaterializationSetter(
(User entity, PrivilegeGroup value) => UserUnsafeAccessors.PrivilegeGroup(entity) = value);
privilegeGroup.SetAccessors(
PrivilegeGroup (InternalEntityEntry entry) => UserUnsafeAccessors.PrivilegeGroup(((User)(entry.Entity))),
PrivilegeGroup (InternalEntityEntry entry) => UserUnsafeAccessors.PrivilegeGroup(((User)(entry.Entity))),
PrivilegeGroup (InternalEntityEntry entry) => entry.ReadOriginalValue<PrivilegeGroup>(privilegeGroup, 2),
PrivilegeGroup (InternalEntityEntry entry) => entry.GetCurrentValue<PrivilegeGroup>(privilegeGroup),
object (ValueBuffer valueBuffer) => valueBuffer[2]);
privilegeGroup.SetPropertyIndexes(
index: 2,
originalValueIndex: 2,
shadowIndex: -1,
relationshipIndex: -1,
storeGenerationIndex: -1);
privilegeGroup.TypeMapping = IntTypeMapping.Default.Clone(
comparer: new ValueComparer<PrivilegeGroup>(
bool (PrivilegeGroup v1, PrivilegeGroup v2) => object.Equals(((object)(v1)), ((object)(v2))),
int (PrivilegeGroup v) => ((object)v).GetHashCode(),
PrivilegeGroup (PrivilegeGroup v) => v),
keyComparer: new ValueComparer<PrivilegeGroup>(
bool (PrivilegeGroup v1, PrivilegeGroup v2) => object.Equals(((object)(v1)), ((object)(v2))),
int (PrivilegeGroup v) => ((object)v).GetHashCode(),
PrivilegeGroup (PrivilegeGroup v) => v),
providerValueComparer: new ValueComparer<int>(
bool (int v1, int v2) => v1 == v2,
int (int v) => v,
int (int v) => v),
mappingInfo: new RelationalTypeMappingInfo(
storeTypeName: "INTEGER"),
converter: new ValueConverter<PrivilegeGroup, int>(
int (PrivilegeGroup value) => ((int)(value)),
PrivilegeGroup (int value) => ((PrivilegeGroup)(value))),
jsonValueReaderWriter: new JsonConvertedValueReaderWriter<PrivilegeGroup, int>(
JsonInt32ReaderWriter.Instance,
new ValueConverter<PrivilegeGroup, int>(
int (PrivilegeGroup value) => ((int)(value)),
PrivilegeGroup (int value) => ((PrivilegeGroup)(value)))));
privilegeGroup.SetSentinelFromProviderValue(0);
var userName = runtimeEntityType.AddProperty(
"UserName",
typeof(string),
propertyInfo: typeof(User).GetProperty("UserName", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(User).GetField("<UserName>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
userName.SetGetter(
string (User entity) => UserUnsafeAccessors.UserName(entity),
bool (User entity) => UserUnsafeAccessors.UserName(entity) == null,
string (User instance) => UserUnsafeAccessors.UserName(instance),
bool (User instance) => UserUnsafeAccessors.UserName(instance) == null);
userName.SetSetter(
(User entity, string value) => UserUnsafeAccessors.UserName(entity) = value);
userName.SetMaterializationSetter(
(User entity, string value) => UserUnsafeAccessors.UserName(entity) = value);
userName.SetAccessors(
string (InternalEntityEntry entry) => UserUnsafeAccessors.UserName(((User)(entry.Entity))),
string (InternalEntityEntry entry) => UserUnsafeAccessors.UserName(((User)(entry.Entity))),
string (InternalEntityEntry entry) => entry.ReadOriginalValue<string>(userName, 3),
string (InternalEntityEntry entry) => entry.GetCurrentValue<string>(userName),
object (ValueBuffer valueBuffer) => valueBuffer[3]);
userName.SetPropertyIndexes(
index: 3,
originalValueIndex: 3,
shadowIndex: -1,
relationshipIndex: -1,
storeGenerationIndex: -1);
userName.TypeMapping = SqliteStringTypeMapping.Default;
var key = runtimeEntityType.AddKey(
new[] { id });
runtimeEntityType.SetPrimaryKey(key);
return runtimeEntityType;
}
public static void CreateAnnotations(RuntimeEntityType runtimeEntityType)
{
var id = runtimeEntityType.FindProperty("Id");
var password = runtimeEntityType.FindProperty("Password");
var privilegeGroup = runtimeEntityType.FindProperty("PrivilegeGroup");
var userName = runtimeEntityType.FindProperty("UserName");
var key = runtimeEntityType.FindKey(new[] { id });
key.SetPrincipalKeyValueFactory(KeyValueFactoryFactory.CreateSimpleNullableFactory<string, int>(key));
key.SetIdentityMapFactory(IdentityMapFactoryFactory.CreateFactory<string>(key));
runtimeEntityType.SetOriginalValuesFactory(
ISnapshot (InternalEntityEntry source) =>
{
var entity = ((User)(source.Entity));
return ((ISnapshot)(new Snapshot<string, string, PrivilegeGroup, string>((source.GetCurrentValue<string>(id) == null ? null : ((ValueComparer<string>)(((IProperty)id).GetValueComparer())).Snapshot(source.GetCurrentValue<string>(id))), (source.GetCurrentValue<string>(password) == null ? null : ((ValueComparer<string>)(((IProperty)password).GetValueComparer())).Snapshot(source.GetCurrentValue<string>(password))), ((ValueComparer<PrivilegeGroup>)(((IProperty)privilegeGroup).GetValueComparer())).Snapshot(source.GetCurrentValue<PrivilegeGroup>(privilegeGroup)), (source.GetCurrentValue<string>(userName) == null ? null : ((ValueComparer<string>)(((IProperty)userName).GetValueComparer())).Snapshot(source.GetCurrentValue<string>(userName))))));
});
runtimeEntityType.SetStoreGeneratedValuesFactory(
ISnapshot () => Snapshot.Empty);
runtimeEntityType.SetTemporaryValuesFactory(
ISnapshot (InternalEntityEntry source) => Snapshot.Empty);
runtimeEntityType.SetShadowValuesFactory(
ISnapshot (IDictionary<string, object> source) => Snapshot.Empty);
runtimeEntityType.SetEmptyShadowValuesFactory(
ISnapshot () => Snapshot.Empty);
runtimeEntityType.SetRelationshipSnapshotFactory(
ISnapshot (InternalEntityEntry source) =>
{
var entity = ((User)(source.Entity));
return ((ISnapshot)(new Snapshot<string>((source.GetCurrentValue<string>(id) == null ? null : ((ValueComparer<string>)(((IProperty)id).GetKeyValueComparer())).Snapshot(source.GetCurrentValue<string>(id))))));
});
runtimeEntityType.Counts = new PropertyCounts(
propertyCount: 4,
navigationCount: 0,
complexPropertyCount: 0,
originalValueCount: 4,
shadowCount: 0,
relationshipCount: 1,
storeGeneratedCount: 0);
runtimeEntityType.AddAnnotation("Relational:FunctionName", null);
runtimeEntityType.AddAnnotation("Relational:Schema", null);
runtimeEntityType.AddAnnotation("Relational:SqlQuery", null);
runtimeEntityType.AddAnnotation("Relational:TableName", "Users");
runtimeEntityType.AddAnnotation("Relational:ViewName", null);
runtimeEntityType.AddAnnotation("Relational:ViewSchema", null);
Customize(runtimeEntityType);
}
static partial void Customize(RuntimeEntityType runtimeEntityType);
}
}

View File

@ -1,25 +0,0 @@
// <auto-generated />
using System;
using System.Runtime.CompilerServices;
using OptixServe.Core.Models;
#pragma warning disable 219, 612, 618
#nullable disable
namespace OptixServe.Infrastructure.Data.CompiledModels
{
public static class UserUnsafeAccessors
{
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Id>k__BackingField")]
public static extern ref string Id(User @this);
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Password>k__BackingField")]
public static extern ref string Password(User @this);
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<PrivilegeGroup>k__BackingField")]
public static extern ref PrivilegeGroup PrivilegeGroup(User @this);
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<UserName>k__BackingField")]
public static extern ref string UserName(User @this);
}
}

View File

@ -1,82 +0,0 @@
# EFCore Intergration
## Introduction
This app relies on EF Core to access the database.
However, it is NOT so smooth when work with NativeAOT.
To use EF Core, see below.
## DesignTimeDbContextFactory
To work with NativeAOT, a code-defined way to create DbContext instance is a MUST in nearly ALL operations of EF Core, letting ef tool discover the DbContext.
In this project, `OptixServe.Infrastructure/Utilites/DesignTimeDbContextFactory.cs` is set up to do this.
This class implements a simple commandline arguments parser, enabling passing arguments along with `dotnet ef`.
Currently, there are two options, `--config/-c` and `--data-dir/-d` are supported. See documents there.
To pass arguments/options to `CreateDbContext` static method, pass the arguments following with `dotnet ef` command, with two dashes splitting.
For example:
```bash
dotnet ef database update -- -c ../data/appsettings.Development.json -d ../data/
```
## Workflow
To make the code work, following the steps:
1. Implement other parts
2. Compile the data models, with `--nativeaot` option.
3. Add migration
4. Update database
5. Build project, and Run!
Here are the details.
### Requirements
See [https://learn.microsoft.com/en-us/ef/core/performance/nativeaot-and-precompiled-queries]()
### Compile Models
```bash
dotnet ef dbcontext optimize --output-dir Data/CompiledModels --precompile-queries --nativeaot -- -c ../data_dev/appsettings.Development.json -d ../data_dev/
```
If this is the first time to run `dbcontext optimize`, DO NOT forget to `UseModel` in DbContext construction.
```cs OptixServe.Infrastructure/Utilites/DatabaseHelper.cs
public static void ConfigureDbContext(DbContextOptionsBuilder options)
{
// ...
options.UseSqlite(connectionString, b => b.MigrationsAssembly("OptixServe.Infrastructure"))
.UseModel(AppDbContextModel.Instance);
// ...
}
```
### Add Migration
```bash
dotnet ef migrations add InitialCreate -- -c ../data_dev/appsettings.Development.json
```
### Update Database
```bash
dotnet ef database update -- -c ../data_dev/appsettings.Development.json -d ../data_dev/
```
### Build Project
```bash
dotnet publish -c Release -r linux-x64
```

View File

@ -1,2 +0,0 @@
# Migrations are ignored in development
*.cs

View File

@ -1,30 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\OptixServe.Core\OptixServe.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tasks" Version="9.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.6" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<InterceptorsNamespaces>$(InterceptorsNamespaces);Microsoft.EntityFrameworkCore.GeneratedInterceptors</InterceptorsNamespaces>
</PropertyGroup>
</Project>

View File

@ -1,129 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using OptixServe.Infrastructure.Configuration;
using OptixServe.Infrastructure.Data;
namespace OptixServe.Infrastructure.Utilites;
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
private record CommandParseResult
{
public string? ConfigPath { get; set; } = null;
public string? DataDir { get; set; } = null;
}
private static CommandParseResult ParseArgs(string[] args)
{
var result = new CommandParseResult();
bool configSet = false;
bool dataDirSet = false;
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if (arg == "--config" || arg == "-c")
{
if (configSet)
{
throw new ArgumentException("The --config/-c option can only be specified once.");
}
if (i + 1 >= args.Length)
{
throw new ArgumentException("Missing value for --config/-c option.");
}
result.ConfigPath = args[i + 1];
configSet = true;
i++;
}
else if (arg == "--data-dir" || arg == "-d")
{
if (dataDirSet)
{
throw new ArgumentException("The --data-dir/-d option can only be specified once.");
}
if (i + 1 >= args.Length)
{
throw new ArgumentException("Missing value for --data-dir/-d option.");
}
result.DataDir = args[i + 1];
dataDirSet = true;
i++;
}
else
{
throw new ArgumentException($"Unknown argument: {arg}");
}
}
return result;
}
/// <summary>
/// Creates DbContext instance in Design-Time
///
/// <para>Custom arguments should be passed in <i>dotnet</i> command </para>
///
/// <b>Example:</b><br/>
/// <example><c>dotnet ef database update -- -c ../data/appsettings.Development.json -d ../data/`</c></example>
///
/// </summary>
/// <param name="args">
/// <list type="bullet">
/// <item>
/// --config/-c: App configuration file to load with database connection settings.
/// </item>
/// <item>
/// --data-dir/-d: App data dir to work with.
/// Currently this only affects finding SQLite database when relative path
/// is specified in database settings.
/// </item>
/// </list>
/// </param>
/// <returns></returns>
public AppDbContext CreateDbContext(string[] args)
{
var parsedArgs = ParseArgs(args);
IConfigurationRoot configuration;
if (!string.IsNullOrEmpty(parsedArgs.ConfigPath))
{
var absConfigPath = Path.GetFullPath(parsedArgs.ConfigPath);
var configDirectory = Path.GetDirectoryName(absConfigPath);
var configFileName = Path.GetFileName(absConfigPath);
// Use the provided config file path
configuration = new ConfigurationBuilder()
.SetBasePath(configDirectory ?? "")
.AddJsonFile(configFileName, optional: false)
.Build();
}
else
{
// Default search configuration path
configuration = ConfigurationHelper.CreateDefaultBuilder().Build();
}
var dbSettings = configuration.GetSection("OptixServe:Infrastructure:Database").Get<DatabaseSettings>()!;
// Resolve SQLite database if `--data-dir` is specified and `dbSettings.Host` is relative
if (parsedArgs.DataDir != null)
{
if (dbSettings.Type == DatabaseType.Sqlite && !Path.IsPathFullyQualified(dbSettings.Host!))
{
var dbRelPath = Path.Combine(parsedArgs.DataDir, dbSettings.Host!);
var dbAbsPath = Path.GetFullPath(dbRelPath);
dbSettings.Host = dbAbsPath;
}
}
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
DatabaseHelper.ConfigureDbContext(optionsBuilder, dbSettings);
return new AppDbContext(optionsBuilder.Options);
}
}

View File

@ -7,10 +7,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OptixServe.Core", "OptixSer
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OptixServe.Api", "OptixServe.Api\OptixServe.Api.csproj", "{52559B29-A255-4BDC-8F2B-A984DEE69E7E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OptixServe.Infrastructure", "OptixServe.Infrastructure\OptixServe.Infrastructure.csproj", "{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OptixServe.Application", "OptixServe.Application\OptixServe.Application.csproj", "{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -45,30 +41,6 @@ Global
{52559B29-A255-4BDC-8F2B-A984DEE69E7E}.Release|x64.Build.0 = Release|Any CPU
{52559B29-A255-4BDC-8F2B-A984DEE69E7E}.Release|x86.ActiveCfg = Release|Any CPU
{52559B29-A255-4BDC-8F2B-A984DEE69E7E}.Release|x86.Build.0 = Release|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Debug|x64.ActiveCfg = Debug|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Debug|x64.Build.0 = Debug|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Debug|x86.ActiveCfg = Debug|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Debug|x86.Build.0 = Debug|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Release|Any CPU.Build.0 = Release|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Release|x64.ActiveCfg = Release|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Release|x64.Build.0 = Release|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Release|x86.ActiveCfg = Release|Any CPU
{E90B4BE9-BCE3-48AC-B60E-1ADF2D328408}.Release|x86.Build.0 = Release|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Debug|x64.ActiveCfg = Debug|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Debug|x64.Build.0 = Debug|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Debug|x86.ActiveCfg = Debug|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Debug|x86.Build.0 = Debug|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Release|Any CPU.Build.0 = Release|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Release|x64.ActiveCfg = Release|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Release|x64.Build.0 = Release|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Release|x86.ActiveCfg = Release|Any CPU
{71D84B31-775B-4EF8-9D0B-411A8CE1CC3A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE