Only implementations of method are used?

Viewed 4172

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);
        }
    }
}
2 Answers

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.

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.

Related