Skip to content

Commit a1df264

Browse files
committed
fix OrmLite links
1 parent 93b5362 commit a1df264

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

MyApp/_pages/ormlite/type-converters.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,52 +12,62 @@ This use of individual decoupled Type Converters makes it possible to enhance or
1212
## Encapsulated, reusable, customizable and debuggable
1313

1414
Converters allow for great re-use as common functionality to support each type is maintained in the common
15-
[ServiceStack.OrmLite/Converters](https://github.com/ServiceStack/ServiceStack.OrmLite/tree/master/src/ServiceStack.OrmLite/Converters)
15+
[ServiceStack.OrmLite/Converters](https://github.com/ServiceStack/ServiceStack/tree/main/ServiceStack.OrmLite/src/ServiceStack.OrmLite/Converters)
1616
whilst any RDBMS-specific functionality can inherit the common converters and provide any specialization
1717
required to support that type. E.g. SQL Server specific converters are maintained in
18-
[ServiceStack.OrmLite.SqlServer/Converters](https://github.com/ServiceStack/ServiceStack.OrmLite/tree/master/src/ServiceStack.OrmLite.SqlServer/Converters)
19-
with each converter inheriting shared functionality and only adding custom logic required to support that
18+
[ServiceStack.OrmLite.SqlServer/Converters](https://github.com/ServiceStack/ServiceStack/tree/main/ServiceStack.OrmLite/src/ServiceStack.OrmLite.SqlServer/Converters) with each converter inheriting shared functionality and only adding custom logic required to support that
2019
Type in Sql Server.
2120

2221
## Creating Converters
2322

2423
Converters also provide good encapsulation as everything relating to handling the field type is contained within
2524
a single class definition. A Converter is any class implementing
26-
[IOrmLiteConverter](https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/src/ServiceStack.OrmLite/IOrmLiteConverter.cs)
25+
[IOrmLiteConverter](https://github.com/ServiceStack/ServiceStack/blob/main/ServiceStack.OrmLite/src/ServiceStack.OrmLite/IOrmLiteConverter.cs)
2726
although, it's instead recommended inheriting from the `OrmLiteConverter` abstract class which allows
2827
only the minimum APIs needing to be overridden, namely the `ColumnDefinition`
2928
used when creating the Table definition and the ADO.NET `DbType` it should use in parameterized queries.
3029
An example of this is in
31-
[GuidConverter](https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/src/ServiceStack.OrmLite/Converters/GuidConverter.cs):
30+
[GuidConverter](https://github.com/ServiceStack/ServiceStack/blob/main/ServiceStack.OrmLite/src/ServiceStack.OrmLite/Converters/GuidConverter.cs):
3231

3332
```csharp
3433
public class GuidConverter : OrmLiteConverter
3534
{
3635
public override string ColumnDefinition => "GUID";
37-
3836
public override DbType DbType => DbType.Guid;
37+
38+
public override object FromDbValue(Type fieldType, object value)
39+
{
40+
if (value is string s)
41+
return Guid.Parse(s);
42+
43+
return base.FromDbValue(fieldType, value);
44+
}
3945
}
4046
```
4147

4248
For this to work in SQL Server the `ColumnDefinition` should instead be **UniqueIdentifier** which is also
4349
what it needs to be cast to, to be able to query Guid's within an SQL Statement.
4450
Therefore, Guids require a custom
45-
[SqlServerGuidConverter](https://github.com/ServiceStack/ServiceStack.OrmLite/blob/master/src/ServiceStack.OrmLite.SqlServer/Converters/SqlServerGuidConverter.cs)
51+
[SqlServerGuidConverter](https://github.com/ServiceStack/ServiceStack/blob/main/ServiceStack.OrmLite/src/ServiceStack.OrmLite.SqlServer/Converters/SqlServerGuidConverter.cs)
4652
to support Guids in SQL Server which looks like:
4753

4854
```csharp
4955
public class SqlServerGuidConverter : GuidConverter
5056
{
5157
public override string ColumnDefinition => "UniqueIdentifier";
5258

53-
public override string ToQuotedString(Type fieldType, object value) => $"CAST('{(Guid)value}' AS UNIQUEIDENTIFIER)";
59+
public override string ToQuotedString(Type fieldType, object value)
60+
{
61+
var guidValue = (Guid)value;
62+
return $"CAST('{guidValue}' AS UNIQUEIDENTIFIER)";
63+
}
5464
}
5565
```
5666

5767
## Registering Converters
5868

5969
To get OrmLite to use this new Custom Converter for SQL Server, the `SqlServerOrmLiteDialectProvider` just
60-
[registers it in its constructor](https://github.com/ServiceStack/ServiceStack.OrmLite/blob/41226795fc12f1b65d6af70c177a5ff57fa70334/src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs#L41):
70+
[registers it in its constructor](https://github.com/ServiceStack/ServiceStack/blob/e764d49a1a0c760bec2c5cb5714a03ea8c39af99/ServiceStack.OrmLite/src/ServiceStack.OrmLite.SqlServer/SqlServerOrmLiteDialectProvider.cs#L50):
6171

6272
```csharp
6373
base.RegisterConverter<Guid>(new SqlServerGuidConverter());

0 commit comments

Comments
 (0)