I am having serious design issues due to generic issues. Maybe someone has some suggestions.
EDIT: So, I know that this is usually not done, but I completely changed my sample code because I realized that the original pseudocode did not really explain my problem. The following code is much more like the real example I'm dealing with. Hope my problem will be clearer. I apologize for being a little long, but from my experience, generic problems usually come up when you try to build a more complex structure. So:
class Program
{
static void Main(string[] args)
{
IConnector<IService> connector = ConnectorBuilderFactory.NewBuilder<IService>("someEndpoint").MakeReliable().GetConnector();
connector.Connect();
}
}
public interface IService : IConnectionMaintainable
{
void DoSomething();
}
public interface IConnectionMaintainable
{
DateTime GetServerTime();
}
public interface IConnector<T>
{
T Channel { get; }
void Connect();
void Disconnect();
}
public interface IConnectorBuilder<T>
{
IConnector<T> GetConnector();
IConnectorBuilder<T> MakeReliable();
}
public class ChannelWatchDog<T> where T : IConnectionMaintainable
{
private IConnector<T> connector;
public ChannelWatchDog(IConnector<T> connector )
{
this.connector = connector;
}
}
public class Connector<T> : IConnector<T>
{
private T channel;
public Connector(string endpoint)
{
}
public T Channel
{
get { return channel; }
}
public void Connect()
{
}
public void Disconnect()
{
}
}
public class ConnectorBuilder<T> : IConnectorBuilder<T>
{
private string endpoint;
public ConnectorBuilder(string endpoint)
{
this.endpoint = endpoint;
}
public IConnector<T> GetConnector()
{
Connector<T> connector = new Connector<T>(endpoint);
return connector;
}
public IConnectorBuilder<T> MakeReliable()
{
return this;
}
}
public static class ConnectorBuilderFactory
{
public static IConnectorBuilder<T> NewBuilder<T>(string endpoint)
{
return new ConnectorBuilder<T>(endpoint);
}
}
, -, GetConnector ConnectorBuilder, , , . . , , :
(ChannelWatchDog), IConnector. IConnector, IConnector, IConnector, GetServerTime IConnectionMaintainable.
, , Expression Builder ( IConnectionBuilder). , IConnector, IConnector <IConnectionMaintainable> . T IConnectorBuilder , ChannelWatchDog. , , GetConnector. MakeReliable .
, , , , , -, , -, . , ChannelWatchDog ConnectorBuilder :
public class ChannelWatchDog
{
private IConnector<IConnectionMaintainable> connector;
public ChannelWatchDog(IConnector<IConnectionMaintainable> connector )
{
this.connector = connector;
}
}
public class ConnectorBuilder<T> : IConnectorBuilder<T>
{
private string endpoint;
public ConnectorBuilder(string endpoint)
{
this.endpoint = endpoint;
}
public IConnector<T> GetConnector()
{
Connector<T> connector = new Connector<T>(endpoint);
ChannelWatchDog watchDog = new ChannelWatchDog((IConnector<IConnectionMaintainable>)connector);
return connector;
}
public IConnectorBuilder<TReliable> MakeReliable<TReliable>() where TReliable : T, IConnectionMaintainable
{
return (IConnectorBuilder<TReliable>)this;
}
}
IConnector .
, . , :)
, .
, , ConnectorBuilders ( ReliableConnectorBuilder) factory factory. .
EDIT: : IConnector ConnectionBuilder, , IConnectionMaintainable .