简体   繁体   English

服务结构参与者的静态类配置

[英]static class configuration for service fabric actors

Service fabric actors are used to access database and all the methods suppose to use Dapper as ORM tool. Service Fabric Actor 用于访问数据库,所有方法都假设使用 Dapper 作为 ORM 工具。

One thing I found out as the best way to solve current problem is using something called SqlMapper in Dapper.我发现解决当前问题的最佳方法的一件事是在 Dapper 中使用称为 SqlMapper 的东西。 With that, you can define general behavior in handling certain data types, for example:有了它,您可以定义处理某些数据类型的一般行为,例如:

SqlMapper.AddTypeHandler(new DateTimeHandler());

public class DateTimeHandler : SqlMapper.TypeHandler<DateTime>
{
    public override void SetValue(IDbDataParameter parameter, DateTime dateTime)
    {
        parameter.Value = dateTime.ValidateDateTime();
    }

    public override DateTime Parse(object value)
    {
        return ((DateTime)value).ValidateDateTime();
    }
}

As you declare static method above (AddTypeHandler) with custom handler such as DateTimeHandler, the mapping done using Dapper framework will make sure any dateTime type goes through above handler correctly.当您使用 DateTimeHandler 等自定义处理程序声明上面的静态方法 (AddTypeHandler) 时,使用 Dapper 框架完成的映射将确保任何 dateTime 类型正确通过上述处理程序。

I would like to see this happening as each Actor communicates to database via Dapper.当每个 Actor 通过 Dapper 与数据库通信时,我希望看到这种情况发生。 I haven't seen it happening yet declaring above static method in attempts with several different places such as Actor constructor() or the main method such as below:我还没有看到它发生在尝试在几个不同的地方(例如 Actor 构造函数()或如下所示的主要方法)中声明上述静态方法:

private static void Main()
{
        try
        {
            // This line registers an Actor Service to host your actor class with the Service Fabric runtime.
            // The contents of your ServiceManifest.xml and ApplicationManifest.xml files
            // are automatically populated when you build this project.
            // For more information, see https://aka.ms/servicefabricactorsplatform

            ActorRuntime.RegisterActorAsync<SqlRepositoryActor>(
               (context, actorType) => new ActorService(context, actorType)).GetAwaiter().GetResult();

            // This doesn't seem like a right place as I don't see the handler being called when Actor uses dapper mapping methods.
            SqlMapper.AddTypeHandler(new DateTimeHandler());

            Thread.Sleep(Timeout.Infinite);
        }
        catch (Exception e)
        {
            ActorEventSource.Current.ActorHostInitializationFailed(e.ToString());
            throw;
        }
} 

I usually don't want to answer my own question, but here is what I found out so far.我通常不想回答我自己的问题,但这是我目前所发现的。

First of all, maybe I need to change the title of this question if I want the actual solution solving the issue to be more relevant to the title itself.首先,如果我希望解决问题的实际解决方案与标题本身更相关,也许我需要更改此问题的标题。 However since the titled question is answered with this post, I will keep it as is.但是,由于这篇文章已回答了标题问题,因此我将保持原样。

To give the short answer, static class configuration can be done with all the places I mentioned above such as Actor constructor, OnActivateAsync(), etc, as static class is still shared across different threads (Actors are all single independent threads) within the AppDomain (And actors are running within the AppDomain. Please correct me if I am wrong.)简而言之,静态类配置可以在我上面提到的所有地方完成,例如 Actor 构造函数、OnActivateAsync() 等,因为静态类仍然在 AppDomain 内的不同线程之间共享(Actor 都是单个独立线程) (并且演员在 AppDomain 中运行。如果我错了,请纠正我。)

What actually caused the above issue was because the mapping definition using Dapper was declared as dynamic;导致上述问题的真正原因是因为使用 Dapper 的映射定义被声明为动态的; shown below:如下图:

return sqlConnection.Query<dynamic>(some_sql_script, new { objInstance }).FirstOrDefault();

Once you change the expected return type T to strong type which contains the typeHandler type, then Dapper invoked the typeHandler correctly:一旦将预期的返回类型 T 更改为包含 typeHandler 类型的强类型,Dapper 就会正确调用 typeHandler:

return sqlConnection.Query<StrongType>(some_sql_script, new { objInstance }).FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM