Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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);
```