GithubHelp home page GithubHelp logo

Comments (4)

cpx86 avatar cpx86 commented on May 24, 2024 1

Cool, I'll get to work on the receive pipeline first, then continue with persistence.

from protoactor-dotnet.

cpx86 avatar cpx86 commented on May 24, 2024

Given a receive pipeline like that which exists in Go right now, adding persistence without inheritance shouldn't be too difficult.

The main problem that I see is to find a suitable replacement for the mixin/embedded field. One option is to replace it with an interface that you need to implement, containing the ProviderState, EventIndex, etc. The downside of it is that the implementation of those properties will be forced upon each actor implementation. One way of making it a bit easier on the actor implementer is to wrap all of the embedded fields in a composed type.

So you would have something like the below (based on existing persistence example). It is basically equivalent to the Go implementation except you need to "dot in" to access the persistence stuff, and the Actor needs to implement an auto-property from the interface. Thoughts?

// user code
class MyPersistentActor : IActor, IPersistentActor
{
    public PersistenceState PersistenceState { get; set; }
    public Task ReceiveAsync(IContext context)
    {
        switch (context.Message)
        {
            case RenameCommand rename:
                PersistenceState.PersistReceive(rename);
                break;
            case AddItemCommand addItem:
                PersistenceState.PersistReceive(addItem);
                break;
        }
    }
}

// protoactor implementation
public interface IPersistentActor
{
    PersistenceState PersistenceState { get; set; }
}

public class PersistenceState
{
    public ProviderState ProviderState { get; set; }
    public int EventIndex { get; set; }
    public IContext Context { get; set; }
    public bool Recovering { get; set; }

    public void Init(PersistenceProvider provider, IContext context)
    {
        ProviderState = provider.GetState();
        Context = context;
        Recovering = true;
        //... more init code
    }
}

Receive Using(PersistenceProvider provider)
{
    return context =>
    {
        switch (context.Message)
        {
            case Started started:
                break;
            case Replay replay:
                var p = context.Actor as IPersistentActor;
                if (p != null)
                {
                    p.PersistenceState = new PersistenceState();
                    p.PersistenceState.Init(provider, context);
                }
                break;
        }
    };
}

from protoactor-dotnet.

rogeralsing avatar rogeralsing commented on May 24, 2024

Love it, your interface suggestion is as good as it gets in C# IMO.
I cant see any other non magic way of doing this with less code.

from protoactor-dotnet.

rogeralsing avatar rogeralsing commented on May 24, 2024

This has been merged, closing

from protoactor-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.