Skip to content

Commit

Permalink
Add registering actor from Type in runtime
Browse files Browse the repository at this point in the history
Signed-off-by: nero.wang <nero.wang@re4.top>
  • Loading branch information
nero-philip-wang committed May 18, 2023
1 parent 8152c74 commit 7b43b30
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/Dapr.Actors/Runtime/ActorRegistrationCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,26 @@ public void RegisterActor<TActor>(string actorTypeName, Action<ActorRegistration
public void RegisterActor<TActor>(string actorTypeName, ActorRuntimeOptions typeOptions, Action<ActorRegistration> configure = null)
where TActor : Actor
{
var actorTypeInfo = ActorTypeInformation.Get(typeof(TActor), actorTypeName);
RegisterActor(typeof(TActor), actorTypeName, typeOptions, configure);
}

/// <summary>
/// Registers an actor type in the collection.
/// </summary>
/// <param name="actionType">Type of actor.</param>
/// <param name="actorTypeName">The name of the actor type represented by the actor.</param>
/// <param name="typeOptions">An optional <see cref="ActorRuntimeOptions"/> that defines values for this type alone.</param>
/// <param name="configure">An optional delegate used to configure the actor registration.</param>
/// <remarks>The value of <paramref name="actorTypeName"/> will have precedence over the default actor type name derived from the actor implementation type or any type name set via <see cref="ActorAttribute"/>.</remarks>
/// <exception cref="ArgumentException">The specified actionType is not a subclass of Actor.</exception>
public void RegisterActor(Type actionType, string actorTypeName, ActorRuntimeOptions typeOptions, Action<ActorRegistration> configure = null)
{
if (!actionType.IsSubclassOf(typeof(Actor)))
{
throw new ArgumentException("ActionType must be a subclass of Actor.", nameof(actionType));
}

var actorTypeInfo = ActorTypeInformation.Get(actionType, actorTypeName);
var registration = new ActorRegistration(actorTypeInfo, typeOptions);
configure?.Invoke(registration);
this.Add(registration);
Expand Down
17 changes: 17 additions & 0 deletions test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ private interface ITestActor : IActor
{
}

[Fact]
public void TestRegisterActorFromTypeInRuntime()
{
var actorType = typeof(TestActor);

var options = new ActorRuntimeOptions();
options.Actors.RegisterActor(actorType, null, null);
var runtime = new ActorRuntime(options, loggerFactory, activatorFactory, proxyFactory);

Assert.Contains(actorType.Name, runtime.RegisteredActors.Select(a => a.Type.ActorTypeName), StringComparer.InvariantCulture);
Assert.Throws<ArgumentException>(() => options.Actors.RegisterActor(typeof(FakeActor), null, null));
}

[Fact]
public void TestInferredActorType()
{
Expand Down Expand Up @@ -316,6 +329,10 @@ public async Task TestActorSettingsWithPerActorConfigurations()
Assert.Equal(32, element.GetInt32());
}

private sealed class FakeActor
{
}

private sealed class TestActor : Actor, ITestActor
{
public TestActor(ActorHost host)
Expand Down

0 comments on commit 7b43b30

Please sign in to comment.