Interfaces & Limitations

Given the following setting, how can I hold back IConnectionjust keep IPortthe same ConnectionTypein Ports? Maybe I will miss something obvious here:

Enum ConnectionType
{
    Undefined,
    Type1,
    Type2
}


IConnection
{
    ConnectionType ConnectionType {get;set}
    IEnumerable<IPort> Ports {get;set;}
}

IPort
{
    ConnectionType ConnectionType {get;set;}
}
+3
source share
4 answers

You cannot enforce such restrictions at compile time.

You will need to do some validation at runtime.

+3
source

To control the type IPorts, you will need to change the class so as not to display a writable list.

, ReadOnlyCollection, IPort . AddPort, IPort .

+2

How to wrap enumeration and use generics?

    public interface IConnectionType
{
    ConnectionTypeEnum Connection{ get; set;}
}

public enum ConnectionTypeEnum
{
   Undefined,
   Type1,
   Type2
}

public interface IPort<T> where T : IConnectionType
{
}

public interface IConnection<T> where T : IConnectionType
{
    IEnumerable<IPort<T>> Ports {get;set;}
}

I'm not sure if the listing makes sense, though

+2
source

Instead of having a connection type in the IPort interface, you have an IConnection property, for example

IConnection
{
  ConnectionType ConnectionType { get; }
  IEnumerable<IPort> Ports { get; }
}

IPort
{
  IConnection Connection { get; }
}

Management of these properties should be left to implementation.

0
source

Source: https://habr.com/ru/post/1769504/


All Articles