Only implementations of method are used?
Asked Answered
R

3

7

In ISerialized, Resharper is complaining that "Only implementations of 'SerializeShape" are used. Is there something more I should be doing, or is my use of an interface simply over-kill in this instance? My 'requirements' are that any use of class Shape implement SerializeShape. I am attempting to use Interface in a plausible, conventional way, but maybe I am not?

I have an interface of such:

namespace Shapes
{
    internal interface ISerialized<in T>
    {
        string SerializeShape();

    }
}

I have a class of such:

using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace Shapes
{

    [DataContract]
    public class Shape : ISerialized<Shape>
    {
        [DataMember] public double Perimeter { get; set; }
        [DataMember] public double Area { get; set; }
        [DataMember] public string ShapeName { get; set; }
        [DataMember] public string ShapeException { get; set; }

        public string SerializeShape(Shape shape)
        {
            return JsonConvert.SerializeObject(shape, Formatting.Indented);
        }
    }
}
Radioactivity answered 15/9, 2018 at 12:52 Comment(0)
P
13

In essence if all you do is have a class implement an interface, then there is no use for the interface. It must be referenced inlieu of the class to be of any real benefit. A brief contrived example to explain in code:

public interface IFoo
{
    string Bar();
}

public class Foo : IFoo
{
    public string Bar()
    {
        return "Foo";
    }
}

public class FooTwo : IFoo
{
    public string Bar()
    {
        Return "FooTwo";
    }
}

public class FooBar
{
    public void UseFoo()
    {
        IFoo foo = new Foo();
        string result = foo.Bar();
    }
    public void UseFooTwo()
    {
        IFoo fooTwo = new FooTwo()
        string result = fooTwo.Bar();
    }
}

As you can see both methods in FooBar use IFoo instead of the actual implementation of Foo or FooTwo. This allows you (or someone who is implementing a portion of code you wrote) to honor the contract that is IFoo. If they had done FooTwo fooTwo = new FooTwo() then they aren't really getting any benefit of FooTwo implementing IFoo.

Postrider answered 15/9, 2018 at 13:10 Comment(6)
So I can do this: ISerialized<Shape> rectangle = new Rectangle("Steve's Perfect Rectangle", 144, Rectangle.PerfectSquareReturns.Perimeter); But cannot seem to return the string output of SerializeShape? Console.WriteLine(rectangle.SerializeShape(???)); SerializeShape is expecting a parameter of type Shape (understandably).Radioactivity
I know this answer is fairly old but how does the answer differ if you're using the interface as a contract to enforce methods that must be present in classes that implement said interface?Merkle
@Merkle not sure I understand your question.Postrider
@Postrider Sorry what I mean is right now I've got this same Resharper suggestion popping up in my interface as well but I only have these methods in my interface because I want to ensure that all classes that implement the interface include these methods at a minimum on top of whatever other methods are desired.Merkle
@Merkle it sounds like you're having the same issue where you defined an interface but don't actually use it (beyond saying a class must implement it). To gain proper benefit you must have it be IFoo foo = new Foo() not Foo foo = new Foo(). For more details, I'm afraid you'll need to ask another question, this is too much for a comment to really delve into.Postrider
@Postrider No no that's perfect, thank you for the insight. I may just make my own question on it then if it comes to that. Appreciate it nonetheless!Merkle
O
1

I know this is an old question, but I just ran into a similar situation.

It seems as if this warning pops up because you aren't using it from an outside class. Maybe you just haven't created the call to it or, in my case, it's being used only internal to the class implementing the interface.

If this warning pops up as soon as you write the interface or when implementing the interface, then you simply haven't used the method yet. Once you call the method from other code using the interface, then the warning will go away.

In my case, the method was possibly used outside the class at one point, but through iterations of the code, it's now only being used internally. If I wanted to, I could change the method from public to private and remove the method declaration from the interface.

Aside: Because I only have one class implementing the interface, I could delete the interface entirely and just have the implementing class on it's own, which would entirely avoid this warning altogether. However, this interface has several references, including automated tests. Removing the interface is beyond the scope of the changes I need/want to make, so I'm not going to do it.

Olszewski answered 1/11, 2021 at 17:26 Comment(0)
B
0

In my case, I had a library project with classes and interfaces used only by code that references the library. That said, I had unit tests that referenced the methods in some classes directly instead of through the interface. I fixed this by adding the [PublicAPI] attribute to the interfaces. This let the ReSharper analyzer know that the code was intended for external use and the messages went away.

Betterment answered 10/7, 2024 at 16:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.