Skip to content

Commit

Permalink
Merge pull request #45 from Takasaki-Studio/dotnet8-docs
Browse files Browse the repository at this point in the history
Update docs and paginate page max size
  • Loading branch information
Takasakiii authored Nov 16, 2023
2 parents ed791c5 + 40eaa24 commit 4db7a2e
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 44 deletions.
57 changes: 27 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ A framework to simplify application creation by improving dependency injection,

```csharp
using Config.Net;
using Lina.AutoDependencyInjection;
using Lina.AutoDependencyInjection.Attributes;
using Lina.LoaderConfig;
using TakasakiStudio.Lina.AutoDependencyInjection;
using TakasakiStudio.Lina.AutoDependencyInjection.Attributes;
using TakasakiStudio.Lina.Utils.LoaderConfig;
using Microsoft.Extensions.DependencyInjection;

var serviceCollection = new ServiceCollection();
Expand All @@ -43,7 +43,7 @@ public interface IFooService
public void PrintAppName();
}

[Service(typeof(IFooService))]
[Service<IFooService>]
public class FooService : IFooService
{
private readonly IAppConfig _appConfig;
Expand All @@ -70,7 +70,7 @@ public interface IAppConfig

```csharp
using Config.Net;
using Lina.LoaderConfig;
using TakasakiStudio.Lina.Utils.LoaderConfig;
using Microsoft.Extensions.DependencyInjection;

var serviceCollection = new ServiceCollection();
Expand All @@ -94,9 +94,9 @@ public interface IAppConfig

```csharp
using Config.Net;
using Lina.Database;
using Lina.Database.Models;
using Lina.LoaderConfig;
using TakasakiStudio.Lina.Database;
using TakasakiStudio.Lina.Database.Models;
using TakasakiStudio.Lina.Utils.LoaderConfig;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -134,8 +134,8 @@ public class UserConfiguration : IEntityTypeConfiguration<User>

```csharp
using FluentValidation;
using Lina.Database.Models;
using Lina.ViewModels;
using TakasakiStudio.Lina.Common;
using TakasakiStudio.Lina.Database.Models;

var user = new User()
{
Expand Down Expand Up @@ -166,7 +166,7 @@ public class UserValidation : AbstractValidator<User>
}
}

public record UserViewModel() : BaseViewModel<UserViewModel, UserViewModelValidation>
public record UserViewModel() : BaseValidationRecord<UserViewModel, UserViewModelValidation>
{
public string Name { get; set; } = string.Empty;
}
Expand All @@ -185,16 +185,17 @@ public class UserViewModelValidation : AbstractValidator<UserViewModel>
```csharp
using Config.Net;
using FluentValidation;
using Lina.Database;
using Lina.Database.Interfaces;
using Lina.Database.Models;
using Lina.Database.Repositories;
using Lina.AutoDependencyInjection.Attributes;
using Lina.LoaderConfig;
using Lina.ViewModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.DependencyInjection;
using TakasakiStudio.Lina.AutoDependencyInjection;
using TakasakiStudio.Lina.AutoDependencyInjection.Attributes;
using TakasakiStudio.Lina.Common;
using TakasakiStudio.Lina.Database;
using TakasakiStudio.Lina.Database.Interfaces;
using TakasakiStudio.Lina.Database.Models;
using TakasakiStudio.Lina.Database.Repositories;
using TakasakiStudio.Lina.Utils.LoaderConfig;

var serviceCollection = new ServiceCollection();

Expand All @@ -204,7 +205,7 @@ serviceCollection.AddLinaDbContext<Program>((builder, assembly) =>
builder.UseMySql(config.DatabaseUrl, ServerVersion.AutoDetect(config.DatabaseUrl),
optionsBuilder => optionsBuilder.MigrationsAssembly(assembly)));

public interface IAppConfig
public interface IAppConfig
{
[Option(DefaultValue = "Server=localhost;Database=test;User Id=root;Password=root;")]
public string DatabaseUrl { get; }
Expand All @@ -216,15 +217,15 @@ public class User : BaseValidateBaseEntity<User, UserValidation, int>

public static implicit operator User(UserViewModel viewModel)
{
return new User()
return new User
{
Name = viewModel.Name
};
}

public static implicit operator UserViewModel(User user)
{
return new UserViewModel()
return new UserViewModel
{
Name = user.Name
};
Expand All @@ -249,7 +250,7 @@ public class UserConfiguration : IEntityTypeConfiguration<User>
}
}

public record UserViewModel() : BaseViewModel<UserViewModel, UserViewModelValidation>
public record UserViewModel : BaseValidationRecord<UserViewModel, UserViewModelValidation>
{
public string Name { get; set; } = string.Empty;
}
Expand All @@ -266,7 +267,7 @@ public interface IUserRepository : IBaseRepository<User, int>
{
}

[Repository(typeof(IUserRepository))]
[Repository<IUserRepository>]
public class UserRepository : BaseRepository<User, int>, IUserRepository
{
public UserRepository(DbContext dbContext) : base(dbContext)
Expand All @@ -278,7 +279,7 @@ public interface IUserService
{
}

[Service(typeof(IUserService))]
[Service<IUserService>]
public class UserService : IUserService
{
private readonly IUserRepository _userRepository;
Expand All @@ -290,11 +291,8 @@ public class UserService : IUserService

public async Task<UserViewModel?> Add(UserViewModel userViewModel)
{
if (!await userViewModel.IsValid())
{
throw new Exception("Not valid");
}

if (!await userViewModel.IsValid()) throw new Exception("Not valid");

User user = userViewModel;
await user.Validate();

Expand All @@ -311,4 +309,3 @@ public class UserService : IUserService
- [Config.Net](https://github.com/aloneguid/config)
- [FluentValidation](https://docs.fluentvalidation.net/en/latest/)
- [EntityFramework](https://learn.microsoft.com/en-us/ef/core/)
- [AutoMapper](https://docs.automapper.org/en/stable/) (Obsolete)
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@

namespace TakasakiStudio.Lina.AspNet.ActionResults;

/// <summary>
/// Blazor Component render result
/// </summary>
/// <param name="componentParams">Params for Blazor Component</param>
/// <typeparam name="T">Blazor Component type</typeparam>
public class BlazorComponentRenderActionResult<T>(object? componentParams = null) : IActionResult
where T : IComponent
{
/// <summary>
/// Execute render
/// </summary>
/// <param name="context">Request context</param>
public async Task ExecuteResultAsync(ActionContext context)
{
var blazorRender = componentParams is null
Expand Down
9 changes: 9 additions & 0 deletions TakasakiStudio.Lina.AspNet/Controllers/PageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

namespace TakasakiStudio.Lina.AspNet.Controllers;

/// <summary>
/// Custom controller for render Blazor Components
/// </summary>
public abstract class PageController : Controller
{
/// <summary>
/// Return render Blazor Component
/// </summary>
/// <param name="componentParams">Params for Blazor Component</param>
/// <typeparam name="T">Blazor Component type</typeparam>
/// <returns></returns>
protected static IActionResult RenderComponent<T>(object? componentParams = null)
where T : IComponent
=> new BlazorComponentRenderActionResult<T>(componentParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.0-rc.1</Version>
<Version>2.0.0-rc.2</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.AspNet</PackageId>
<Authors>TakasakiStudio</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace TakasakiStudio.Lina.AutoDependencyInjection.Attributes;
/// }
/// </code>
/// <code>
/// [Adapter(typeof(IFooAdapter))]
/// [Adapter&lt;IFooAdapter&gt;]
/// class FooAdapter : IFooAdapter {
/// /*...*/
/// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace TakasakiStudio.Lina.AutoDependencyInjection.Attributes;
/// }
/// </code>
/// <code>
/// [Dependency(typeof(IFooDependency), LifeTime.Scoped)]
/// [Dependency&lt;IFooDependency&gt;(LifeTime.Scoped)]
/// class FooDependency : IFooDependency {
/// /*...*/
/// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace TakasakiStudio.Lina.AutoDependencyInjection.Attributes;
/// }
/// </code>
/// <code>
/// [HttpClient(typeof(IFooHttpClient))]
/// [HttpClient&lt;IFooHttpClient&gt;]
/// class FooHttpClient : IFooHttpClient {
/// /*...*/
/// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace TakasakiStudio.Lina.AutoDependencyInjection.Attributes;
/// }
/// </code>
/// <code>
/// [Middleware(typeof(IFooMiddleware))]
/// [Middleware&lt;IFooMiddleware&gt;]
/// class FooMiddleware : IFooMiddleware {
/// /*...*/
/// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace TakasakiStudio.Lina.AutoDependencyInjection.Attributes;
/// }
/// </code>
/// <code>
/// [Repository(typeof(IFooRepository))]
/// [Repository&lt;IFooRepository&gt;]
/// class FooRepository : IFooRepository {
/// /*...*/
/// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace TakasakiStudio.Lina.AutoDependencyInjection.Attributes;
/// }
/// </code>
/// <code>
/// [Service(typeof(IFooService))]
/// [Service&lt;IFooService&gt;]
/// class FooService : IFooService {
/// /*...*/
/// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.0-rc.1</Version>
<Version>2.0.0-rc.2</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.AutoDependencyInjection</PackageId>
<Authors>TakasakiStudio</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.0-rc.1</Version>
<Version>2.0.0-rc.2</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.Common</PackageId>
<Title>TakasakiStudio.Lina.Common</Title>
Expand Down
5 changes: 5 additions & 0 deletions TakasakiStudio.Lina.Database/Models/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public abstract class BaseEntity<TPkType>
/// </summary>
public TPkType Id { get; set; } = default!;

/// <summary>
/// Create a clone of value
/// </summary>
/// <typeparam name="T">Value type</typeparam>
/// <returns></returns>
public T Clone<T>()
where T: BaseEntity<TPkType>
{
Expand Down
11 changes: 11 additions & 0 deletions TakasakiStudio.Lina.Database/Models/BaseValidateBaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@ public abstract class BaseValidateBaseEntity<TModel, TValidationClass, TPkKey> :
IBaseValidate<TModel, TValidationClass>
where TValidationClass: IValidator<TModel>
{
/// <summary>
/// Validate entity and throw if invalid
/// </summary>
public virtual async ValueTask Validate()
{
await GetBaseImplementationInstance().BaseValidate();
}

/// <summary>
/// Get validation errors
/// </summary>
/// <returns>Errors</returns>
public virtual async Task<ValidationResult> GetErrors()
{
return await GetBaseImplementationInstance().BaseGetErrors();
}

/// <summary>
/// Entity is valid
/// </summary>
/// <returns>Is valid</returns>
public virtual async ValueTask<bool> IsValid()
{
return await GetBaseImplementationInstance().BaseIsValid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.0-rc.1</Version>
<Version>2.0.0-rc.2</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>TakasakiStudio.Lina.Database</PackageId>
<Title>TakasakiStudio.Lina.Database</Title>
Expand Down
23 changes: 23 additions & 0 deletions TakasakiStudio.Lina.Utils/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ namespace TakasakiStudio.Lina.Utils.Extensions;

public static class EnumExtensions
{
/// <summary>
/// Convert enum as option
/// </summary>
/// <typeparam name="T">Enum type</typeparam>
/// <returns>Options</returns>
public static IDictionary<string, string> GetOptions<T>()
where T : struct, Enum
{
Expand All @@ -23,12 +28,24 @@ public static IDictionary<string, string> GetOptions<T>()
.ToDictionary(x => x.Item2.ToString()!, y => y.Item1.ToString());
}

/// <summary>
/// Get enum value
/// </summary>
/// <param name="value">Enum</param>
/// <typeparam name="T">Enum type</typeparam>
/// <returns>Value</returns>
public static string GetValue<T>(this T? value)
where T : struct, Enum
{
return value is null ? "" : Convert.ToInt32(value).ToString();
}

/// <summary>
/// Unify flag list to unique value
/// </summary>
/// <param name="list">Flag list</param>
/// <typeparam name="T">Enum type</typeparam>
/// <returns>Enum value</returns>
public static T ToFlags<T>(this IEnumerable<T> list)
where T : struct, Enum
{
Expand All @@ -46,6 +63,12 @@ public static T ToFlags<T>(this IEnumerable<T> list)
return (T)Enum.ToObject(typeof(T), Convert.ChangeType(result, underlyingType));
}

/// <summary>
/// Split enum flag into list
/// </summary>
/// <param name="flag">Enum value</param>
/// <typeparam name="T">Enum type</typeparam>
/// <returns>Flag list</returns>
public static IEnumerable<T> ToEnumerable<T>(this T flag)
where T : struct, Enum
{
Expand Down
4 changes: 2 additions & 2 deletions TakasakiStudio.Lina.Utils/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class QueryableExtensions
/// <param name="pageSize">Page size</param>
/// <typeparam name="T">Model type</typeparam>
/// <returns>Query execution</returns>
public static IQueryable<T> Paginate<T>(this IQueryable<T> query, int page, int pageSize)
public static IQueryable<T> Paginate<T>(this IQueryable<T> query, int page, int pageSize = 20)
{
if (page == 0) page = 1;

Expand All @@ -25,4 +25,4 @@ public static IQueryable<T> Paginate<T>(this IQueryable<T> query, int page, int

return query.Skip((page - 1) * pageSize).Take(pageSize);
}
}
}
Loading

0 comments on commit 4db7a2e

Please sign in to comment.