Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registering actor from Type in runtime #1091

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading