C #: using type "i" as a general parameter?

This may seem a little strange, but I really need to create a workaround for the very complex handling of duplex communications in C #, especially in order to get other developers to adhere to the DRY principle.

So what I am doing is to have a multi-standard based on a type that looks like this:

internal sealed class SessionManager<T> where T : DuplexServiceBase 

which is not a problem at all - for now.

However, as soon as I want the services (I gather with one instance per session) to register with the SessionManager, problems begin:

 internal abstract class DuplexServiceBase : MessageDispatcherBase<Action> 

(MessageDispatcherBase is my class that creates a stream and sends messages asynchronously).

I want to have a method that looks like this:

  protected void ProcessInboundMessage() { // Connect SessionManager<self>.Current.Connect(this); } 

... but the problem is, how can I get into the "me"?

I really need separate session managers for each class of service, because everyone has their own notifications (basically these are very annoying "NotifyAllClients" - a method that makes us want to pull my own hair in the last hours), and they need to be processed separately.

Do you have ANY ideas?

I do not want to use "AsyncPattern = true", btw ... this would require me to give up the type of security, to comply with the requirements of the contract (this will lead to very bad abuse of the communication system that I configure here), and this will require abandonment DRY principle, there’s going to be a lot of repetitive code in all of this, and that’s what I seriously frowned at.

Edit:

I found the best possible solution, thanks to the answers here - this is the EXTENSION METHOD, hehe ...

  public static SessionManager<T> GetSessionManager<T>(this T sessionObject) where T : DuplexServiceBase { return SessionManager<T>.Current; } 

I can use this as follows:

 GetSessionManager().Connect(this); 

Mission accomplished.: - D

This method (owned by DuplexServiceBase) gives me the session manager I want to work with. Fine!: -)

+7
c # design-patterns type-systems singleton wcf
source share
2 answers

I would write a helper method:

 static class SessionManager { // non-generic! static void Connect<T>(T item) where T : DuplexServiceBase { SessionManager<T>.Current.Connect(item); } } 

and use SessionManager.Connect(this) , which will automatically detect it using type type inference.

+15
source share

You can wrap the call in a general method, thereby using the output of the compiler type:

 private static void ConnectSessionManager<T>(T service) { SessionManager<T>.Current.Connect(service) } protected void ProcessInboundMessage() { // Connect ConnectSessionManager(this); } 
+3
source share

All Articles