Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ internal static class ResourceExtensions
public static TResource Proxify<TResource>(this TResource source, IResourceTypeController typeController)
where TResource : class, IPublicResource
{
if (ResourceTypeController.IsGenericResourceInterface(typeof(TResource)))
throw new NotSupportedException("Generic resource interfaces are not supported through the facade!");

return (TResource)typeController.GetProxy(source as Resource);
}

Expand Down
14 changes: 14 additions & 0 deletions src/Moryx.Resources.Management/Resources/ResourceTypeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,21 @@ where resourceInterface.IsPublic
&& relevantInterfaces.Any(generalInterface.IsAssignableFrom) // It is a base type of a relevant interface
select generalInterface);

// Filter all interfaces that are generic OR contain generic methods
relevantInterfaces = relevantInterfaces.Where(candidate => !IsGenericResourceInterface(candidate)).ToList();

return relevantInterfaces;
}

internal static bool IsGenericResourceInterface(Type resourceInterface)
{
if (resourceInterface.IsGenericType || resourceInterface.IsGenericTypeDefinition)
return true;

if (resourceInterface.GetMethods().Any(method => method.IsGenericMethod || method.ContainsGenericParameters))
return true;

return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Moryx.AbstractionLayer.Capabilities;
using Moryx.AbstractionLayer.Drivers;
using Moryx.AbstractionLayer.Drivers.InOut;
using Moryx.AbstractionLayer.Drivers.Message;
using Moryx.AbstractionLayer.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Moryx.Resources.Management.Tests
{
public interface IGenericMethodCall : IPublicResource
{
/// <summary>
/// Get channel using specialized API
/// </summary>
IList<TChannel> GenericMethod<TChannel>(string identifier);
}

public class ResourceWithGenericMethod : Resource, IGenericMethodCall, ISimpleResource
{
public int Foo { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public ICapabilities Capabilities => throw new NotImplementedException();

public event EventHandler<int> FooChanged;
public event EventHandler<bool> FooEven;
public event EventHandler SomeEvent;
public event EventHandler<ICapabilities> CapabilitiesChanged;

public IList<TChannel> GenericMethod<TChannel>(string identifier)
{
throw new NotImplementedException();
}

public int MultiplyFoo(int factor)
{
throw new NotImplementedException();
}

public int MultiplyFoo(int factor, ushort offset)
{
throw new NotImplementedException();
}

public void RaiseEvent()
{
throw new NotImplementedException();
}
}
}
24 changes: 24 additions & 0 deletions src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,30 @@ public void AfterDisposeTheProxyIsDetached()
Assert.Throws<ProxyDetachedException>(() => proxy.MultiplyFoo(2));
}

[Test]
public void ProxyBuilderFiltersGenericInterfaces()
{
// Arrange
var driver = new ResourceWithGenericMethod { Id = 2, Name = "Some other Resource" };

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

// Assert
Assert.IsNotNull(proxy);
Assert.IsFalse(typeof(IGenericMethodCall).IsAssignableFrom(proxy.GetType()));
}

[Test]
public void FacadeExceptionForGenericProxy()
{
// Arrange
var driver = new ResourceWithGenericMethod { Id = 2, Name = "Some other Resource" };

// Assert
Assert.Throws<NotSupportedException>(() => ResourceExtensions.Proxify<IGenericMethodCall>(driver, _typeController));
}

[Test]
public void ReplaceWithProxy()
{
Expand Down