From 62ef8d8f4c70ae017e6e92aed3782bc1ad1b02bb Mon Sep 17 00:00:00 2001 From: Marcel Vielhaus Date: Mon, 12 Feb 2024 15:18:46 +0100 Subject: [PATCH] Adds the AllowMultiple flag to the ResourceAvailableAs attribute When searching for the relevant interfaces to build the resource proxies in the ResourceTypeController, the method creates a list of distinct instanzes of the attribute. However, without the AllowMultiple flag on the attribute this call will never return more than one element. Therefore, the missing flag is added. --- .../ResourceAvailableAsAttribute.cs | 2 +- .../Mocks/DerivedResourceWithNewProxy.cs | 20 +++++++++++++++++++ .../TypeControllerTests.cs | 15 ++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/Tests/Moryx.Resources.Management.Tests/Mocks/DerivedResourceWithNewProxy.cs diff --git a/src/Moryx.AbstractionLayer/Resources/Attributes/ResourceAvailableAsAttribute.cs b/src/Moryx.AbstractionLayer/Resources/Attributes/ResourceAvailableAsAttribute.cs index 026ef4310..db7b6a9f5 100644 --- a/src/Moryx.AbstractionLayer/Resources/Attributes/ResourceAvailableAsAttribute.cs +++ b/src/Moryx.AbstractionLayer/Resources/Attributes/ResourceAvailableAsAttribute.cs @@ -8,7 +8,7 @@ namespace Moryx.AbstractionLayer.Resources /// /// Members of the given interfaces are available outside of the resource management /// - [AttributeUsage(AttributeTargets.Class)] + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class ResourceAvailableAsAttribute : Attribute { /// diff --git a/src/Tests/Moryx.Resources.Management.Tests/Mocks/DerivedResourceWithNewProxy.cs b/src/Tests/Moryx.Resources.Management.Tests/Mocks/DerivedResourceWithNewProxy.cs new file mode 100644 index 000000000..0c3197ba0 --- /dev/null +++ b/src/Tests/Moryx.Resources.Management.Tests/Mocks/DerivedResourceWithNewProxy.cs @@ -0,0 +1,20 @@ +// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG +// Licensed under the Apache License, Version 2.0 + +using Moryx.AbstractionLayer.Resources; + +namespace Moryx.Resources.Management.Tests +{ + public interface ISecondNonResourceInterface + { + } + + [ResourceAvailableAs(typeof(ISecondNonResourceInterface))] + public class DerivedResourceWithNewProxy : SimpleResource, ISecondNonResourceInterface + { + public override int MultiplyFoo(int factor) + { + return Foo *= factor + 2; + } + } +} diff --git a/src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs b/src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs index 13513e1a1..9c32d9f15 100644 --- a/src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs +++ b/src/Tests/Moryx.Resources.Management.Tests/TypeControllerTests.cs @@ -79,6 +79,21 @@ public void UseBaseProxyForDerivedType() Assert.AreEqual(baseProxy.GetType(), proxy.GetType()); } + [Test] + public void UseNewProxyForDerivedTypeWithNewInterface() + { + // Arrange: Create instance + var baseInstance = new SimpleResource { Id = 2 }; + var instance = new DerivedResourceWithNewProxy { Id = 3 }; + + // Act: Build Proxy + var baseProxy = _typeController.GetProxy(baseInstance); + var proxy = _typeController.GetProxy(instance); + + // Assert: Make sure proxy is still the base type + Assert.That(baseProxy.GetType(), Is.Not.EqualTo(proxy.GetType())); + } + [Test] public void CallMethodOnProxy() {