Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bbonkr/kr.bbon.core
Helper for .NET
https://github.com/bbonkr/kr.bbon.core
dotnet dotnet5 dotnet6 hacktoberfest hacktoberfest2021 net5 net6 net7 nuget
Last synced: 25 days ago
JSON representation
Helper for .NET
- Host: GitHub
- URL: https://github.com/bbonkr/kr.bbon.core
- Owner: bbonkr
- Created: 2021-06-15T10:22:43.000Z (over 3 years ago)
- Default Branch: dev
- Last Pushed: 2024-03-18T08:14:51.000Z (8 months ago)
- Last Synced: 2024-04-09T21:56:08.090Z (7 months ago)
- Topics: dotnet, dotnet5, dotnet6, hacktoberfest, hacktoberfest2021, net5, net6, net7, nuget
- Language: C#
- Homepage: https://www.nuget.org/packages/kr.bbon.Core
- Size: 116 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
Awesome Lists containing this project
README
# kr.bbon.Core Package
[![](https://img.shields.io/nuget/v/kr.bbon.Core)](https://www.nuget.org/packages/kr.bbon.Core) [![](https://img.shields.io/nuget/dt/kr.bbon.Core)](https://www.nuget.org/packages/kr.bbon.Core) [![publish to nuget](https://github.com/bbonkr/kr.bbon.Core/actions/workflows/build-tag.yaml/badge.svg)](https://github.com/bbonkr/kr.bbon.Core/actions/workflows/build-tag.yaml)
## π’ Overview
.NET μμ νλ‘μ νΈλ₯Ό μμν λ, λ°λ³΅μ μΌλ‘ μμ±νλ μ¬νμ ν¨ν€μ§λ‘ μ 리νμ΅λλ€.
## π Namespace
### kr.bbon.Core λ€μμ€νμ΄μ€
κΈ°λ³Έ λ€μμ€νμ΄μ€λ‘ kr.bbon.Core λ₯Ό μ¬μ©ν©λλ€.
### kr.bbon.Core.Converters λ€μμ€νμ΄μ€
κ° λ³νμ μν κΈ°λ₯μ μ 곡νλ λ€μμ€νμ΄μ€μ λλ€.
## π― Features
### Exception classes
#### ApiException class
API μμΈλ₯Ό νννκΈ° μν΄ μ¬μ©λ©λλ€.
```csharp
public Task SomeFeatureAsync()
{
if(NotValid){
var model = new ErrorModel("It's invalid message");
throw new ApiException(400, model);
}
// ...
}
```#### HttpStatusException class
> ApiException class λ₯Ό μ¬μ©νμμμ€.
HTTP μμΈλ₯Ό νννκΈ° μν΄ μ¬μ©λ©λλ€.
```csharp
// Exception handling in action method
try
{
// ...
}
catch(HttpStatusException ex)
{
return StatusCode(ex.StatusCode, ex.Message, ex.GetDetails());
}
catch(Exception ex)
{
// ...
}
```#### HttpStatusException class
> ApiException class λ₯Ό μ¬μ©νμμμ€.
HTTP μμΈμ μμΈ μ 보λ₯Ό νννκΈ° μν΄ μ¬μ©λ©λλ€.
```csharp
throw new HttpStatusException(HttpStatusCode.BadRequest, new SomeDetails
{
Id = "err111",
Message = "AAA μμ±μ κ°μ΄ μ μκ° μλλλ€.",
});
```#### SomethingWrongException
> ApiException class λ₯Ό μ¬μ©νμμμ€.
μ¬μ©μ μ μ μμΈλ₯Ό νννκΈ° μν΄ μ¬μ©ν©λλ€.
```csharp
// Exception handling in action method
try
{
// ...
}
catch(SomethingWrongException ex)
{
return StatusCode(HttpStatusCode.Forbidden, ex.Message, ex.GetDetails());
}
catch(Exception ex)
{
// ...
}
```#### SomethingWrongException
> ApiException class λ₯Ό μ¬μ©νμμμ€.
μ¬μ©μ μ μ μμΈμ μμΈ μ 보λ₯Ό νννκΈ° μν΄ μ¬μ©ν©λλ€.
```csharp
throw new SomethingWrongException("λ°μ΄ν°λ₯Ό μ²λ¦¬νμ§ λͺ»νμ΅λλ€.", new SomeDetails
{
Id = "err111",
Message = "AAA μμ±μ κ°μ΄ μ μκ° μλλλ€.",
});
```### Extension methods
#### Object.ToJson() method
Object ν΄λμ€μ ToJson λ©μλλ₯Ό μΆκ°ν©λλ€.
κ°μ²΄μ μΈμ€ν΄μ€λ₯Ό JSON λ¬Έμμ΄λ‘ μ§λ ¬ννλ κΈ°λ₯μ μ 곡ν©λλ€.
```csharp
var sample = getSampleModel();
sample.ToJson();
```JsonSerializerOptions 맀κ°λ³μκ° μ§μ λμ§ μμ κ²½μ° μλ κΈ°λ³Έκ°μ μ¬μ©ν©λλ€.
```csharp
var defaultOptions = new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
```### kr.bbon.Core.Converters namespace
#### JavascriptDateConverter class
##### ToDateTimeOffset method
μλ°μ€ν¬λ¦½νΈ λ°λ¦¬μ΄ κ°μ DateTimeOffset μΌλ‘ λ³νν©λλ€.
##### ToJavascriptDateMilliseconds method
DateTimeOffset κ°μ μλ°μ€ν¬λ¦½νΈ λ°λ¦¬μ΄ κ°μΌλ‘ λ³νν©λλ€.
```csharp
JavascriptDateConverter converter = new JavascriptDateConverter();
var javascriptDateValue = 1624165031491;
var datetimeOffsetValue = converter.ToDateTimeOffset(javascriptDateValue);
var milliseconds = converter.ToJavascriptDateMilliseconds(datetimeOffsetValue);Assert.Equal(javascriptDateValue, milliseconds.Value);
```### kr.bbon.Core.Reflection namespace
### ReflectionHelper class
#### CollectAssembly method
νμ¬ AppDomain μ μ΄μ λΈλ¦¬μ€ μ λ ₯λ 쑰건μ λ§μ‘±νλ μ΄μ λΈλ¦¬ λͺ©λ‘μ μμ§ν©λλ€.
```csharp
var predicte = new Func(t => t.Name == nameof(ApiException));var assemblies = ReflectionHelper.CollectAssembly(predicte);
```#### CollectTypes method
νμ¬ AppDomain μ νμ μ€ μ‘°κ±΄μ λ§μ‘±νλ νμ λͺ©λ‘μ μμ§ν©λλ€.
```csharp
var predicte = new Func(t => t.Name == nameof(ApiException));var types = ReflectionHelper.CollectTypes(predicte);
```