C#: api connection class design

P

puzzlecracker

What I currently have and request a suggestion to modularize it


public sealed class Connection: BaseConnection{

//I have to process about 30 events
public event Handler1=handler1;
public event Handler2=handler2;
public event Handler3=handler3;
public event Handler4=handler4;

//I have to process about 30 service calls

public void Service1(){}
public void Service2(){}
public void Service3(){}


private void OnHandler1(Request aRequest)
{
if (handler1 != null)
handler1(aRequest);
}

private void OnHandler2(Request aRequest)
{
if (handler2 != null)
handler2(aRequest);
}

private void onHandler3(Request aRequest)
{
if (handler3 != null)
handler3(aRequest);
}

//and so on
protected override Dispatch(Event e){ // based on event, I process
and call onHandlers}

// more functions to process the events.

}

In separate file, I define all those delegates. I give client the dll
containing this class and some enumerations, and declarations.


So, the client of my api would use it in this way

Connection conn=new Connection();
conn.handler1+=...
conn.handler2+=...
//etc
conn.Connect();
conn.Service1();
conn.Listen();
conn.Destroy();



The issue is that I implement services and event handling in just one
class Connection, taking some implementation from Base connection,
resulting in over 4K lines of code. Any suggestion to improve the
design?

Thanks
 
I

Ignacio Machin ( .NET/ C# MVP )

What I currently have and request a suggestion to modularize it

public sealed class Connection: BaseConnection{

//I have to process about 30 events
public event Handler1=handler1;
public event Handler2=handler2;
public event Handler3=handler3;
public event Handler4=handler4;

//I have to process about 30 service calls

public void Service1(){}
public void Service2(){}
public void Service3(){}

private void OnHandler1(Request aRequest)
{
if (handler1 != null)
handler1(aRequest);
}

private void OnHandler2(Request aRequest)
{
if (handler2 != null)
handler2(aRequest);
}

private void onHandler3(Request aRequest)
{
if (handler3 != null)
handler3(aRequest);
}

//and so on
protected override Dispatch(Event e){ // based on event, I process
and call onHandlers}

// more functions to process the events.

}

In separate file, I define all those delegates. I give client the dll
containing this class and some enumerations, and declarations.

So, the client of my api would use it in this way

Connection conn=new Connection();
conn.handler1+=...
conn.handler2+=...
//etc
conn.Connect();
conn.Service1();
conn.Listen();
conn.Destroy();

The issue is that I implement services and event handling in just one
class Connection, taking some implementation from Base connection,
resulting in over 4K lines of code. Any suggestion to improve the
design?

Thanks

Hi,


It all depends of the particualar events/handlers if all of then have
the same signature you can use a list or a dictionary to hold a list
of them.Then you will have only one dispatch method:

protected override Dispatch(Event e){
if ( eventDictionary.ContainKey( e.EventName))
if (
eventDictionary[e.EventName]!= null)
eventDictionary[e.EventName]( args);

You can declare your dictionary like:
Dictionary<string, EventHandler> eventDictionary = new
Dictionary<string, EventHandler>();


What is the use of the ServiceXXX() methods that you declare in your
code though?
 
P

puzzlecracker

What  I currently have and request a suggestion to modularize it
public sealed class Connection: BaseConnection{
//I have to process about 30 events
public event Handler1=handler1;
public event Handler2=handler2;
public event Handler3=handler3;
public event Handler4=handler4;
//I have to process about 30 service calls
         public void Service1(){}
         public void Service2(){}
         public void Service3(){}
        private void OnHandler1(Request aRequest)
        {
            if (handler1 != null)
                handler1(aRequest);
        }
        private void OnHandler2(Request aRequest)
        {
            if (handler2 != null)
                handler2(aRequest);
        }
        private void onHandler3(Request aRequest)
        {
            if (handler3 != null)
                handler3(aRequest);
        }
        //and so on
        protected override Dispatch(Event e){ // based on event, I process
and call onHandlers}
      // more functions to process the events.

In separate file, I define all those delegates. I give client the dll
containing this class and some enumerations, and declarations.
So, the client of my api would use it in this way
Connection conn=new Connection();
conn.handler1+=...
conn.handler2+=...
//etc
conn.Connect();
conn.Service1();
conn.Listen();
conn.Destroy();
The issue is that I implement services and event handling in just one
class Connection, taking some implementation from Base connection,
resulting in over 4K lines of code. Any suggestion to improve the
design?

Hi,

It all depends of the particualar events/handlers if all of then have
the same signature you can use a list or a dictionary to hold a list
of them.Then  you will have only one dispatch method:

         protected override Dispatch(Event e){
                if ( eventDictionary.ContainKey( e.EventName))
                  if (
eventDictionary[e.EventName]!= null)
       eventDictionary[e.EventName]( args);

You can declare your dictionary like:
Dictionary<string, EventHandler> eventDictionary = new
Dictionary<string, EventHandler>();

What is the use of the ServiceXXX() methods that you declare in your
code though?

It sends the requests the external app, then external app sends back
the results which are handled in the dispatch
 
P

puzzlecracker

It all depends of the particualar events/handlers if all of then have
the same signature you can use a list or a dictionary to hold a list
of them.Then you will have only one dispatch method:
protected override Dispatch(Event e){
if ( eventDictionary.ContainKey( e.EventName))
if (
eventDictionary[e.EventName]!= null)
eventDictionary[e.EventName]( args);
You can declare your dictionary like:
Dictionary<string, EventHandler> eventDictionary = new
Dictionary<string, EventHandler>();
What is the use of the ServiceXXX() methods that you declare in your
code though?

It sends the requests the external app, then external app sends back
the results which are handled in the dispatch

also signatures are different... There has to be a model that used to
design this sort of api
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top