Skip to content

Commit

Permalink
Added the documentation for TypeHandlers class (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva authored Oct 11, 2024
2 parents a299c83 + 05002c6 commit 10eb708
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/TinyHelpers.Dapper/StringEnumerableTypeHandler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# StringEnumerableTypeHandler

`StringEnumerableTypeHandler` is a custom type handler used with Dapper to map a string field in a database to an `IEnumerable<string>` in the application, and vice versa.

### Usage

This handler allows you to split a delimited string (e.g., `"apple;banana;cherry"`) from the database into a list or array of strings, and join a list or array of strings into a delimited string when saving to the database.

```csharp
StringEnumerableTypeHandler.Configure(";");
```


### `Parse(object value)`

The `Parse` method converts a database value into an `IEnumerable<string>`, splitting a string using the specified separator.

#### Parameters
- **`value`** (`object`):
The database value to be parsed. It is expected to be a string containing multiple values separated by the specified separator.

#### Returns
- **`IEnumerable<string>`**:
A collection of strings obtained by splitting the input value based on the separator. Empty entries are removed from the result.

#### Example

```csharp
var result = handler.Parse("apple;banana;cherry");
// result: ["apple", "banana", "cherry"]
```

### `SetValue(IDbDataParameter parameter, IEnumerable<string>? value)`

The `SetValue` method converts an `IEnumerable<string>` into a single delimited string, suitable for saving to the database, and assigns it to the given database parameter.

#### Parameters
- **`parameter`** (`IDbDataParameter `):
The database parameter.

- **`value`** (`IEnumerable<string> `):
The enumerable collection of values

#### Returns
- **`void`**:


#### Example

```csharp
var values = new List<string> { "apple", "banana", "cherry" };
handler.SetValue(myParameter, values);
// myParameter.Value: "apple;banana;cherry"
```

0 comments on commit 10eb708

Please sign in to comment.