Skip to content

Commit 009318c

Browse files
PR 123
1 parent 3d1fa01 commit 009318c

File tree

3 files changed

+111
-9
lines changed

3 files changed

+111
-9
lines changed

src/Dapr.Actors/Resources/SR.Designer.cs

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Dapr.Actors/Runtime/ActorRegistrationCollection.cs

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ public void RegisterActor<TActor>(Action<ActorRegistration> configure = null)
5151
public void RegisterActor<TActor>(ActorRuntimeOptions typeOptions, Action<ActorRegistration> configure = null)
5252
where TActor : Actor
5353
{
54-
RegisterActor<TActor>(null, typeOptions, configure);
54+
RegisterActor(typeof(TActor), default, typeOptions, configure);
5555
}
5656

5757
/// <summary>
@@ -64,21 +64,78 @@ public void RegisterActor<TActor>(ActorRuntimeOptions typeOptions, Action<ActorR
6464
public void RegisterActor<TActor>(string actorTypeName, Action<ActorRegistration> configure = null)
6565
where TActor : Actor
6666
{
67-
RegisterActor<TActor>(actorTypeName, null, configure);
67+
RegisterActor(typeof(TActor), actorTypeName, null, configure);
6868
}
6969

7070
/// <summary>
7171
/// Registers an actor type in the collection.
7272
/// </summary>
73+
/// <typeparam name="TActorInterface">Type of actor interface.</typeparam>
74+
/// <typeparam name="TActor">Type of actor.</typeparam>
75+
/// <param name="configure">An optional delegate used to configure the actor registration.</param>
76+
public void RegisterActor<TActorInterface, TActor>(Action<ActorRegistration> configure = null)
77+
where TActor : Actor
78+
{
79+
RegisterActor<TActorInterface, TActor>(actorTypeName: null, configure);
80+
}
81+
82+
/// <summary>
83+
/// Registers an actor type in the collection.
84+
/// </summary>
85+
/// <typeparam name="TActorInterface">Type of actor interface.</typeparam>
7386
/// <typeparam name="TActor">Type of actor.</typeparam>
74-
/// <param name="actorTypeName">The name of the actor type represented by the actor.</param>
7587
/// <param name="typeOptions">An optional <see cref="ActorRuntimeOptions"/> that defines values for this type alone.</param>
7688
/// <param name="configure">An optional delegate used to configure the actor registration.</param>
89+
public void RegisterActor<TActorInterface, TActor>(ActorRuntimeOptions typeOptions, Action<ActorRegistration> configure = null)
90+
where TActor : Actor
91+
{
92+
RegisterActor(typeof(TActorInterface), typeof(TActor), null, typeOptions, configure);
93+
}
94+
95+
/// <summary>
96+
/// Registers an actor type in the collection.
97+
/// </summary>
98+
/// <typeparam name="TActorInterface">Type of actor interface.</typeparam>
99+
/// <typeparam name="TActor">Type of actor.</typeparam>
100+
/// <param name="actorTypeName">The name of the actor type represented by the actor.</param>
101+
/// <param name="configure">An optional delegate used to configure the actor registration.</param>
77102
/// <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>
78-
public void RegisterActor<TActor>(string actorTypeName, ActorRuntimeOptions typeOptions, Action<ActorRegistration> configure = null)
103+
public void RegisterActor<TActorInterface, TActor>(string actorTypeName, Action<ActorRegistration> configure = null)
79104
where TActor : Actor
80105
{
81-
var actorTypeInfo = ActorTypeInformation.Get(typeof(TActor), actorTypeName);
106+
RegisterActor(typeof(TActorInterface), typeof(TActor), actorTypeName, null, configure);
107+
}
108+
109+
/// <summary>
110+
/// Registers an actor type in the collection.
111+
/// </summary>
112+
/// <param name="actorType">Type of actor.</param>
113+
/// <param name="actorTypeName">The name of the actor type represented by the actor.</param>
114+
/// <param name="typeOptions">An optional <see cref="ActorRuntimeOptions"/> that defines values for this type alone.</param>
115+
/// <param name="configure">An optional delegate used to configure the actor registration.</param>
116+
/// <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>
117+
public void RegisterActor(Type actorType, string actorTypeName, ActorRuntimeOptions typeOptions, Action<ActorRegistration> configure = null)
118+
{
119+
RegisterActorInternal(default, actorType, actorTypeName, typeOptions, configure);
120+
}
121+
122+
/// <summary>
123+
/// Registers an actor type in the collection.
124+
/// </summary>
125+
/// <param name="actorInterfaceType">Type of actor interface.</param>
126+
/// <param name="actorType">Type of actor.</param>
127+
/// <param name="actorTypeName">The name of the actor type represented by the actor.</param>
128+
/// <param name="typeOptions">An optional <see cref="ActorRuntimeOptions"/> that defines values for this type alone.</param>
129+
/// <param name="configure">An optional delegate used to configure the actor registration.</param>
130+
/// <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>
131+
public void RegisterActor(Type actorInterfaceType, Type actorType, string actorTypeName, ActorRuntimeOptions typeOptions, Action<ActorRegistration> configure = null)
132+
{
133+
RegisterActorInternal(actorInterfaceType, actorType, actorTypeName, typeOptions, configure);
134+
}
135+
136+
private void RegisterActorInternal(Type actorInterfaceType, Type actorType, string actorTypeName, ActorRuntimeOptions typeOptions, Action<ActorRegistration> configure = null)
137+
{
138+
var actorTypeInfo = ActorTypeInformation.Get(actorInterfaceType, actorType, actorTypeName);
82139
var registration = new ActorRegistration(actorTypeInfo, typeOptions);
83140
configure?.Invoke(registration);
84141
this.Add(registration);

src/Dapr.Actors/Runtime/ActorTypeInformation.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -120,6 +120,40 @@ public static ActorTypeInformation Get(Type actorType)
120120
/// <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>
121121
public static ActorTypeInformation Get(Type actorType, string actorTypeName)
122122
{
123+
return GetInternal(default, actorType, actorTypeName);
124+
}
125+
126+
/// <summary>
127+
/// Creates an <see cref="ActorTypeInformation"/> from actorType.
128+
/// </summary>
129+
/// <param name="actorInterfaceType">The type of interface implementing the actor to create ActorTypeInformation for.</param>
130+
/// <param name="actorType">The type of class implementing the actor to create ActorTypeInformation for.</param>
131+
/// <param name="actorTypeName">The name of the actor type represented by the actor.</param>
132+
/// <returns><see cref="ActorTypeInformation"/> created from actorType.</returns>
133+
/// <exception cref="System.ArgumentException">
134+
/// <para>When <see cref="System.Type.BaseType"/> for actorType is not of type <see cref="Actor"/>.</para>
135+
/// <para>When actorType does not implement an interface deriving from <see cref="IActor"/>
136+
/// and is not marked as abstract.</para>
137+
/// </exception>
138+
/// <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>
139+
public static ActorTypeInformation Get(Type actorInterfaceType, Type actorType, string actorTypeName)
140+
{
141+
return GetInternal(actorInterfaceType, actorType, actorTypeName);
142+
}
143+
144+
private static ActorTypeInformation GetInternal(Type actorInterfaceType, Type actorType, string actorTypeName)
145+
{
146+
if (actorInterfaceType != default && !actorInterfaceType.IsActor())
147+
{
148+
throw new ArgumentException(
149+
string.Format(
150+
CultureInfo.CurrentCulture,
151+
SR.ErrorNotAnActorInterface,
152+
actorInterfaceType.FullName,
153+
typeof(Actor).FullName),
154+
"actorInterfaceType");
155+
}
156+
123157
if (!actorType.IsActor())
124158
{
125159
throw new ArgumentException(
@@ -132,7 +166,7 @@ public static ActorTypeInformation Get(Type actorType, string actorTypeName)
132166
}
133167

134168
// get all actor interfaces
135-
var actorInterfaces = actorType.GetActorInterfaces();
169+
var actorInterfaces = actorInterfaceType != default ? new Type[] { actorInterfaceType } : actorType.GetActorInterfaces();
136170

137171
// ensure that the if the actor type is not abstract it implements at least one actor interface
138172
if ((actorInterfaces.Length == 0) && (!actorType.GetTypeInfo().IsAbstract))

0 commit comments

Comments
 (0)