Skip to content

Commit

Permalink
Filter generic base types when seaching for the resource proxy type
Browse files Browse the repository at this point in the history
When inheriting from a generic base type without adding additional interfaces, a resource can still have a generated proxy. However, in that case the proxy builder would assume the generic base type to be the best possible fit, which cannot be proxified.
  • Loading branch information
1nf0rmagician committed Dec 12, 2024
1 parent aaebfb7 commit 0e00a08
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,19 @@ private ResourceProxy InstantiateProxy(string typeName, Resource instance)
/// </summary>
private void ProvideProxyType(Type resourceType)
{
// Step 1: Find the least specific base type that offers the same amount of interfaces
// Step 1: Find the least specific base type that offers the same amount of interfaces and is not a generic itself
// ReSharper disable once AssignNullToNotNullAttribute -> FullName should be not null
var targetType = _typeCache[resourceType.ResourceType()];
var linker = targetType;

var interfaces = RelevantInterfaces(linker);
// Move up the type tree until the parent offers less interfaces than the current linker
while (linker.BaseType != null && interfaces.Count == RelevantInterfaces(linker.BaseType).Count)
// Move up the type tree until the parent offers less interfaces than the current linker, is abstract or a generic
while (linker.BaseType != null && !linker.BaseType.ResourceType.IsGenericType
&& interfaces.Count == RelevantInterfaces(linker.BaseType).Count)
{
linker = linker.BaseType;
}


// Step 2: Check if we already created a proxy for this type. If we already
// did use this one for the requested type as well.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,10 @@ public void RaiseEvent()
throw new NotImplementedException();
}
}

public interface IGenericBaseResourceInterface : IResource { }

public class GenericBaseResource<T> : Resource, IGenericBaseResourceInterface { }

public class InheritingFromGenericResource : GenericBaseResource<object> { }
}
14 changes: 14 additions & 0 deletions src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ public void ProxyBuilderFiltersGenericInterfaces()
Assert.IsFalse(typeof(IGenericMethodCall).IsAssignableFrom(proxy.GetType()));
}

[Test]
public void ProxyBuilderSkipsGenericBaseTypes()
{
// Arrange
var driver = new InheritingFromGenericResource { Id = 42, Name = "A non generic resource inheriting from a generic base type" };

// Act
var proxy = _typeController.GetProxy(driver);

// Assert
Assert.IsNotNull(proxy);
Assert.IsFalse(typeof(GenericBaseResource<object>).IsAssignableFrom(proxy.GetType()));
}

[Test]
public void FacadeExceptionForGenericProxy()
{
Expand Down

0 comments on commit 0e00a08

Please sign in to comment.