Inherited grains design pattern
I'm finding myself with the following pattern in a few places:
public interface IBaseInterface
{
Task SharedMethod();
}
public interface IInheritedA : IBaseInterface
{
Task MethodA();
}
public interface IInheritedB : IBaseInterface
{
Task MethodB();
}
public interface IInheritedC : IBaseInterface
...
The problem I run into is when I have some functionality that needs to just call the SharedMethod(), I currently have a switch statement that conditionally creates a reference to the correct inherited grain interface:
public async Task CallSharedMethod()
switch (type)
{
case TypeLookup.A:
await GrainFactory.GetGrain<IInheritedA>(id).SharedMethod();
break;
case TypeLookup.B:
await GrainFactory.GetGrain<IInheritedB>(id).SharedMethod();
break;
...
}
}
This seems like an awkward way to call a base method. I recognize why it isn't possible to get a grain reference to the base interface directly, is there a better pattern for accessing shared functionality between inherited grains? Maybe I'm missing something rather obvious...
My first question is how many grain classes implement IInheritedA and IInheritedB in this example. If the answer is one class for each of the two interfaces, then you can use GrainFactory.GetGrain<IBaseInterface>(id, classNamePrefix) to explicitly tell which grain class you are targeting. classNamePrefix is full or partial (from left) name of the target grain class starting with its namespace.
The same approach works for more complex inheritance hierarchies. Just need a way to identify the target grain class.
Correct, currently there is one class that implements each inherited interface.
Using the classNamePrefix I could write code with something to this effect:
public async Task CallSharedMethod()
switch (type)
{
case TypeLookup.A:
await GrainFactory.GetGrain<IBaseInterface>(id, "ClassA").SharedMethod();
break;
case TypeLookup.B:
await GrainFactory.GetGrain<IBaseInterface>(id, "ClassB").SharedMethod();
break;
...
}
}
But I'm afraid I still need to add a new entry for every new inheritor, again to call that same shared base method. Optimally, I would prefer to not distinguish at all between the different inherited interfaces.
I think what I'm asking for currently isn't possible, if there are multiple classes that implement the same base interface, Orleans needs to know what implementing class to instantiate.
I'm just cooking up a thought experiment now:
Let's say at some point prior during execution, my code makes a method call to a grain instance for the IInheritedA interface. What if Orleans were to remember that a particular id was associated with that IInheritedA implementer, rather than any of the other inherited interfaces. Later on in the execution, some code makes a call to IBaseInterface, with the same id as before. Orleans recognizes the id from the previous call, and instantiates the IInheritedA implementer even though the call is ambiguous.
At first glance, this seems to me to in some way break the concept of virtual actors (an actor's exact identity isn't determined until some point in time?). Yet, it also seems like from a coding perspective this kind of behavior might make inheritance easier? As well as brings on a new host of problems...
Anyway, thanks for your time Sergey. Orleans really is an impressive piece of technology.
Thank you, @TrexinanF14!
It works find of similar to what you described. A grain reference returned by var grain = GrainFactory.GetGrain<IInheritedA>(id) does internally include the type code of the target grain class. If you keep/store that reference, you can later cast it to IBaseInterface by doing var x = grain.AsReference<IBaseInterface>.
Now you have a grain reference that still points to the same grain class, but implements only IBaseInterface. And that reference can be passed as an argument or persisted in a store. It will maintain the concrete grain class as its target.
Ah, so this reference could be persisted in the grain state and passed to other grains? That would definitely solve the problem.
Actually, instead of bothering you, I'll just try it. Thanks.