Trending on MSDN: Service Fabric Actors and Inheritance

Micah McKittrick 946 Reputation points Microsoft Employee
2019-10-29T19:49:54.367+00:00

I understand that projects in the cluster accessing Actors will only use their interfaces and not actually have a reference to the ActorClass it is accessing.

I can create a base class for actors so that all actors can share certain methods and members. But I understand that the interfaces can never have generic type parameters?

It would be great to have a base class that saves a state but it must derive from a unique actorStateBase for each actor type.

So even if the IActor can be derived from, the inheriting classes needs to implement an interface that cannot contain generics and so generics won't really be possible in Actor classes?

To elaborate further, let's say I have three actor types:

  • UserActor
  • PurchaseActor
  • Belonginsactor

Each actor implements its own interface that derives from Iactor and each actor derives from my own ActorBaseClass (which derives from Actor). Each have one state

  • UserActorState
  • PurchaseActorState
  • BelongingsActorState

Every state derives from ActorStateBase. To add, update and get these states let's say I need three methods

  • Add
  • Update
  • Get

What I would like to do is to have Add, Update and Get methods inside the ActorBaseClass with generics so that I could have

  • AddState
  • UpdateState
  • GetState

This way these CRUD methods would only have to be declared once instead of three times each (and then again for each new type).

Lastly, would it be possible to provide a type as a method argument and then have that method return an actor state base class that can be cast? Such as this in the interface

Task TryGetStateAsync(Type type, string name);

Sourced from MSDN

Azure Service Fabric
Azure Service Fabric
An Azure service that is used to develop microservices and orchestrate containers on Windows and Linux.
252 questions
0 comments No comments
{count} votes

Accepted answer
  1. olufemia-MSFT 2,861 Reputation points
    2019-10-29T23:22:33.953+00:00

    Welcome to the Microsoft Q&A (Preview) platform. Happy to answer your questions.

    Generics on interfaces are not supported for remoting calls. The states would be specific to Actor types and you would be interacting with them in that Actor type’s methods using the StateManager APIs.

    You could do something as in below conde snippet:

    public class ActorBaseClass : Actor  
        {  
            public ActorBaseClass(ActorService actorService, ActorId actorId)  
                : base(actorService, actorId)  
            {  
                 
            }  
            protected Task GetStateAsync(string stateName, CancellationToken cancellationToken)  
            {  
                return this.StateManager.GetStateAsync(stateName, cancellationToken);  
            }  
            protected Task AddStateAsync(string stateName, T value, CancellationToken cancellationToken)  
            {  
                return this.StateManager.AddStateAsync(stateName, value, cancellationToken);  
            }  
        }  
    internal class UserActor : ActorBaseClass, IUserActor  
        {  
            public UserActor(ActorService actorService, ActorId actorId)  
                : base(actorService, actorId)  
            {  
            }         
            async Task IUserActor.GetCountAsync(CancellationToken cancellationToken)  
            {  
                var state = await this.GetStateAsync("count", cancellationToken);  
                return state.Count;  
            }  
            Task IUserActor.SetCountAsync(int count, CancellationToken cancellationToken)  
            {  
                var userState = new UserActorState();  
                userState.Count = count;  
                return this.StateManager.AddStateAsync("count", userState, cancellationToken);  
            }  
        }  
    public class UserActorState : ActorBaseState  
        {  
            public int Count { get; set; }  
        }  
    public class ActorBaseState  
        {  
        }  
      
    

    Sourced from MSDN

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. AHMED ISMAIL GOMAA BAKIR 906 Reputation points
    2022-05-05T22:03:45.727+00:00

    Generics on interfaces are not supported for remoting calls. The states would be specific to Actor types and you would be interacting with them in that Actor type’s methods using the StateManager APIs.

    7 people found this answer helpful.
    0 comments No comments

  2. Hanan Ali Ibrahem Roffa 81 Reputation points
    2022-05-06T03:14:46.847+00:00

    Generics on interfaces are not supported for remoting calls. The states would be specific to Actor types and you would be interacting with them in that Actor type’s methods using the StateManager APIs

    0 comments No comments