From 5ee173ea25e254ae8222fd9cd8e152e21adb943f Mon Sep 17 00:00:00 2001
From: 2881099 <2881099@users.noreply.github.com>
Date: Mon, 19 Aug 2024 23:43:53 +0800
Subject: [PATCH] =?UTF-8?q?Updated=20=E7=B1=BB=E5=9E=8B=E6=98=A0=E5=B0=84?=
=?UTF-8?q?=20(markdown)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
类型映射.md | 45 ++++++++++++++++++---------------------------
1 file changed, 18 insertions(+), 27 deletions(-)
diff --git a/类型映射.md b/类型映射.md
index 7672a74..77ded5f 100644
--- a/类型映射.md
+++ b/类型映射.md
@@ -83,47 +83,38 @@ fsql.Select
().Where(a => a.Options.Value1 == 100 && a.Options.Value2 == "
## TypeHandlers(自定义)
```csharp
-FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(JsonClass), new String_JsonClass());
+FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(JsonPoco), new JsonPocoTypeHandler());
+FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(DateOnly), new DateOnlyTypeHandler());
+FreeSql.Internal.Utils.TypeHandlers.TryAdd(typeof(DateTimeOffset), new DateTimeOffsetTypeHandler());
class Product
{
- public Guid id { get; set; }
- [Column(MapType = typeof(string), StringLength = -1)]
- public JsonClass json { get; set; }
+ public JsonPoco json { get; set; }
+ public DateOnly date { get; set; }
+ public DateTimeOffset dateTimeOffset { get; set; }
}
-class JsonClass
+class JsonPoco
{
public int a { get; set; }
public int b { get; set; }
}
-class String_JsonClass : TypeHandler
+class JsonPocoTypeHandler : TypeHandler
{
- public override object Serialize(JsonClass value)
- {
- return JsonConvert.SerializeObject(value);
- }
- public override JsonClass Deserialize(object value)
- {
- return JsonConvert.DeserializeObject((string)value);
- }
+ public override object Serialize(JsonPoco value) => JsonConvert.SerializeObject(value);
+ public override JsonPoco Deserialize(object value) => JsonConvert.DeserializeObject((string)value);
+ public override void FluentApi(FluentColumn col) => col.MapType(typeof(string)).StringLength(-1);
}
-
-class Class1
+class DateOnlyTypeHandler : TypeHandler
{
- //自定义 DateTimeOffset
- [Column(MapType = typeof(string), DbType = "datetime")]
- public DateTimeOffset Join { get; set; }
+ public override object Serialize(DateOnly value) => value.ToString("yyyy-MM-dd");
+ public override DateOnly Deserialize(object value) => DateOnly.TryParse(string.Concat(value), out var trydo) ? trydo : DateOnly.MinValue;
+ public override void FluentApi(FluentColumn col) => col.MapType(typeof(string)).StringLength(12);
}
class DateTimeOffsetTypeHandler : TypeHandler
{
- public override object Serialize(DateTimeOffset value)
- {
- return value.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss");
- }
- public override DateTimeOffset Deserialize(object value)
- {
- return DateTimeOffset.TryParse((string)value, out var dts) ? dts : DateTimeOffset.MinValue;
- }
+ public override object Serialize(DateTimeOffset value) => value.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss");
+ public override DateTimeOffset Deserialize(object value) => DateTimeOffset.TryParse((string)value, out var dts) ? dts : DateTimeOffset.MinValue;
+ public override void FluentApi(FluentColumn col) => col.MapType(typeof(string)).DbType("datetime");
}
```