C # interface design, expose library classes?

I want to define an "IFile" interface that includes an array of key / value metadata pairs. When receiving or installing these key / value pairs, the IFile developer must be able to take action. What would be the best way to do this? I see three methods:

Method 1) Get / Set the dictionary object:

public interface IFile
{
    ...

    Dictionary<String, String> GetMetadata();

    void SetMetadata(Dictionary<String, String> metadata);
}

Method 2) Use the dictionary directly:

public interface IFile
{
    ...

    Dictionary Metadata();
}

and in the implementation of IFile, a legacy version of the Dictionary that acts on get / set can be provided.

Method 3) Avoid a complete dictionary and provide your own interface, for example:

public interface IMetadata
{
    String GetValue(String key);        
    void SetValue(String key, String value);        
    Boolean Contains(String key);        
    void Delete(String key);  

    ...
}

public interface IFile
{
    ...

    IMetadata Metadata();
}

3, , . . 3, , .

, , , , . , , " " !

,

+3
4

, , , :

public interface IFile
{
    ...

    IDictionary<string, string> Metadata { get; }
}

- IDictionary - - , .

( , , , IMetadata, , , )

ps - , enum - , .

+1

3 ( ), :

  • .
  • /.
+1

4:

public class JFile
{
    ...

    protected Dictionary Metadata() { ... }

    ...

    // --> all this use "Dictionary" internally
    // --> but, do somthing to other "IFile" fields

    public String GetValue(String key) { ... }
    public void SetValue(String key, String value)  { ... }
    public Boolean Contains(String key)  { ... }
    public void Delete(String key)  { ... }
}

, , .

2, ...

... , / "IFile", , , "", "", , - .

- , 2 .

, , . :

  • MyClass
  • , MyClass: MySuperClass, .
  • ,

There is a “better way”, but a “right, perfect, way” does not exist in Software Development because it is too complex to have one way to do something. Even if you choose another method, if it works, well, then it works.

Luck; -)

+1
source

Why not use IDictionary if your problem suppresses inheritance using a dictionary in a value key dictionary?

then your first approach works.

0
source

All Articles