欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

orleans 2.0 进阶之自定义持久化储存

程序员文章站 2023-11-10 21:41:16
一丶简单介绍下目录结构和项目依赖,如图 二丶主要核心自定义代码 1. 添加自定义实现类 CustomProvider 2. 在silo中注入代替默认实现 3. 在grain类上启用 三丶运行结果如下 示例代码下载地址:SimpleStorage ......

一丶简单介绍下目录结构和项目依赖,如图

orleans 2.0 进阶之自定义持久化储存

二丶主要核心自定义代码

1. 添加自定义实现类 customprovider

public class customprovider : igrainstorage
    {
        public task clearstateasync(string graintype, grainreference grainreference, igrainstate grainstate)
        {
            throw new notimplementedexception();
        }

        public task readstateasync(string graintype, grainreference grainreference, igrainstate grainstate)
        {
            if(graintype== "grain.usergrain")
            {
                var id = grainreference.getprimarykeylong();

                //模拟从数据库读出user
                var user = new userstate()
                {
                    mobilephone = "124214214",
                    name = "李四"
                };

                grainstate.state = user;

            }
            return task.completedtask;
        }

        public task writestateasync(string graintype, grainreference grainreference, igrainstate grainstate)
        {

            if (graintype== "grain.usergrain")
            {
                var id = grainreference.getprimarykeylong();

                //模拟保存数据库


            }
            return task.completedtask;
        }
    }

2. 在silo中注入代替默认实现

 .configureservices(services =>
                   {
                       var providername = "customprovider";
                       services.tryaddsingleton(sp =>
                           sp.getservicebyname<igrainstorage>(providerconstants.default_storage_provider_name));

                       services.addsingletonnamedservice<igrainstorage, customprovider>(providername);
                   })

3. 在grain类上启用

 [storageprovider(providername = "customprovider")]

 

三丶运行结果如下

orleans 2.0 进阶之自定义持久化储存

 

 

示例代码下载地址:simplestorage