Lucas Meijer
Lucas Meijer

Reputation: 4484

How to deal with not being allowed to define types in an interface in c#

I often find the need to do something along these lines:

public class OperationResult
{
  bool succes;
  SomeOtherObject someobject;
}


public interface ICanDoSomethingWeird
{
  OperationResult DoMyThing();
}

Where the OperationResult is really a class that belongs to the ICanDoSomethingWeird interface. I find it very annoying I cannot place it in the namespace of the interface. I'm wondering how other people deal with this. Do you just stick it in the global namespace? or just one namespace up from the namespace where the interface sits?

My current approach is to do rename the OperationResult class to ICanDoSomethingWeird_OperationResult, but am not very impressed by how pretty that is :)

Anybody have a better solution?

Upvotes: 2

Views: 228

Answers (3)

Kent Boogaart
Kent Boogaart

Reputation: 178650

If OperationResult is a type used solely by ICanDoSomethingWeird and clients of ICanDoSomethingWeird, then it belongs in the same "context" as ICanDoSomethingWeird. That is, it belongs in the same namespace as ICanDoSomethingWeird:

namespace MyNamespace
{
    public class OperationResult {}

    public interface ICanDoSomethingWeird
    {
        OperationResult DoMyThing();
    }
}

Think about the client code. Would you prefer this:

using MyNamespace;

ICanDoSomethingWeird myWeirdThing = ...;
ICanDoSomethingWeird.OperationResult result = myWeirdThing.DoMyThing();

or this:

using MyNamespace;

ICanDoSomethingWeird myWeirdThing = ...;
OperationResult result = myWeirdThing.DoMyThing();

The latter makes more sense to me, and is the only option where interfaces are concerned. You cannot declare inner types in interfaces. And do note the general advice regarding nested types:

DO NOT use nested types if the type is likely to be referenced outside of the containing type.

Upvotes: 10

AnthonyWJones
AnthonyWJones

Reputation: 189457

An interface does not define any implementation. By including a class your interface is effectively including some implementation. Nested classes have the purpose of assisting the class implementation that defines it.

Nested classes should not be used as if the class has merit outside of its use with the containing class. A class should not be used as a surrogate for a namespace to define other classes.

If an interface defines some behaviour that requires the use of a class that as yet does not exist it would make sense to create that class in the same namespace to which the interface belongs.

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532435

You could define the interface as an abstract class instead and then define a public inner class.

 public interface ICanDoSomething

 public abstract class AbstractCanDoSomething
 {
     public class OperationResult
     {
          public bool SomeValue { get; set; }
          public int SomeOherVAlue { get; set; }
     }

     public abstract OperationResult DoMyThing();
 }

Upvotes: 0

Related Questions