• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#初识ActorModel

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最近学了点 c# dataflow的一些东西,然后国外有个人,用dataflow来实现了,一个Actor模型;

这里做个比较,算是初识我们的actor模型,然后我们再进一步的深入了解一哈;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;

namespace Actor_Model
{
    //actor model 的基本使用和示例;
    //https://blog.jayway.com/2013/11/15/an-actor-model-implementation-in-c-using-tpl-dataflow/#comment-463337

    public abstract class Message { }

    //把所有的action 都通过meg的 形式表达出来;

    //吧我们的每一动作都看成了信息;让后将信息传递出去,真正的执行,交给我们的具体的actor去执行滴呀;
    public class Deposit : Message
    {

        public decimal Amount { get; set; }
    }
    //To check the balance we send a QueryBalance message
    public class QueryBalance : Message
    {
        //The result of the query should be sent as a message to another actor
        //也就是说查询出来的信息又交给了另外一个actor 处理了滴呀;信息之间的传递;
        public Actor Receiver { get; set; }
    }

    //The final message is the result of the balance chec
    public class Balance : Message
    {
        public decimal Amount { get; set; }
    }

    public abstract class Actor
    {

        private readonly ActionBlock<Message> _action;

        public Actor()
        {
            _action = new ActionBlock<Message>((msg =>
              {
                  dynamic self = this;
                  dynamic mess = msg;
                  self.Handle(mess);

              }));

        }

        public void Send(Message msg)
        {
            _action.Post(msg);
        }

        public Task Completion
        {
            get
            {
                _action.Complete();
                return _action.Completion;
            }
        }

    }


    //Each account has a balance. We want to be able to deposit money and check the balance
    //实际动作的执行者;
    public class AccountActor : Actor
    {
        private decimal _balance;

        //存钱
        public void Handle(Deposit msg)
        {
            _balance += msg.Amount; //deposite money 
        }
        //查询余额
        public void Handle(QueryBalance msg)
        {
            //查询出来的余额,叫给我们的额新actor去处理;
            //相当于创建一个新的acto
            msg.Receiver.Send(new Balance { Amount = _balance });
        }



    }
    //We also need an actor that outputs the result of the balance query
    public class OutputActor : Actor
    {
        public void Handle(Balance msg)
        {
            Console.WriteLine("Balance is {0}", msg.Amount);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {

            var account = new AccountActor();
            var output = new OutputActor();

            account.Send(new Deposit { Amount = 50 });
            account.Send(new QueryBalance { Receiver = output });

            account.Completion.Wait();
            output.Completion.Wait();

            Console.WriteLine("Done!");
            Console.ReadLine();


        }
    }
}

看完基本了解actor,但是还是很不透彻~


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap