액터 모델에서 액터는 메시지 정렬과 같은 메시지 루프를 가지고 있습니다. 패턴 일치 (ofc 언어에 따라 다름)C# 5/Async CTP에서 액터 모델 구현
의사 F #
let message_loop() =
let! message = receive_message() //sync receive message
match message with
| Foo(a,b) -> DoStuff(a,b)
| Bar(c) -> DoOtherStuff(c)
| _ -> error ("unknown message")
message_loop()
메시지 서명이 일치하고 메시지 콘텐츠에서 수행 할 작업과 관련이 있습니다.
실제 호출 메소드와 실제 호출 메소드 사이에 개념적 차이점이 있습니까? 예 : 나는 C# 5에서 다음을 수행 할 경우 :
class MyActor
{
//message signature Foo(a,b)
public void Foo(int a,string b)
{
Act(() => DoStuff(a,b));
}
//message signature Bar(c)
public void Bar(int c)
{
Act(() => DoOtherStuff(c));
}
// the rest is infrasturcture code, can be refactored into an Actor Base class
//this emulates the sync behavior of the above actor
//each action is pushed onto a queue
//and then processed synchronously by the message handler
private void Act(Action action)
{
actions.Post(action);
}
private BufferBlock<Action> actions = new BufferBlock<Action>();
//this needs max degreee of parallellism = 1
private ActionBlock<Action> messageHandler = new ....
}
이 방법 만 메시지의 하나의 종류를 처리하는 메시지 큐에 게시 된 비동기 메시지가 표시됩니다 MyActor의 메소드를 호출; 행동. 그러나 메시지와 관련된 동작은 메시지 자체에 포함되어 있습니다 (public 메서드에서 게시 됨)
C# 5/Async CTP에서 배우를 수행하는 데는 깨끗한 방법으로 간주됩니까?
메시지가 단순히 클래스처럼 어색한 메시지 DTO를 만드는 대신 일반 메시지로 정의된다는 이점이 있습니다.
이렇게하면 충분합니까?
이 스타일은 코드 검토에서 가정에서 더 느낀다. 그것은 나의 취향을 위해 약간 개방적이다. http://codereview.stackexchange.com/ – GregC
아마도 새로운 [async/await를 사용하는 C#의 Agent/MailboxProcessor] (http://stackoverflow.com/questions/4075189/agent-mailboxprocessor-in-c-sharp-using- new-async-await) – Benjol
C#을 사용하는 경우 Rx를 대신 학습하는 것이 좋습니다. 그것은 매우 강력하고 효율적이며 표현력이 뛰어납니다. Rx로 문제를 모델링 할 수 있어야합니다. 에이전트 지향 접근 방식보다 F #에 Rx를 추천하기도합니다. 또한 C#으로 구현할 때 버그가 될 수있는 많은 코너 케이스가 Rx 팀에서 다룰 예정입니다. –