|
Hello everybody.
I have a class A that publishes an event, and is using a delegate to allow other objects to subscribe to it. After raising the event, class A is sending some information wrapped up in the custom class deriving from EventArgs. When the event fires, the class is then going through the list of subscribers using the GetInvocationList() and is getting the response from the subscribers.
So far so good.
The problem is when trying to subscribe to the event from a class B that has no reference to class A.
It would be easy enough to add a reference to it and to just subscribe to the event, however we do not want to make the server code to reference the client classes.
Is there a way to do this differently? If not, that what about adding an intermediary object that can receive and pass on the events raised in class A on the client side, so that class B on the server needs only to reference one object* But if there's a better way then I would really appreciate some directions.
Thanks a lot.
Sarajevo, Bosnia
-- modified at 15:21 Wednesday 23rd November, 2005
|
|
|
|
|
mirano wrote: The problem is when trying to subscribe to the event from a class B that has no reference to class A.
It would be easy enough to add a reference to it and to just subscribe to the event, however we do not want to make the server code to reference the client classes.
You could make the event handler in Class B public and have a mediator class attach it to the event in Class A.
Or have an interface represent only the event that's in Class A that Class B needs to subscribe to. Hopefully, the event represents something easily identifiable so that it's easy to name.
Have Class A implement this interface. Pass an instance of Class A to Class B as a reference to the interface, not Class A. This way, Class B only knows about the interface. This decouples Class B from Class A.
mirano wrote: If not, that what about adding an intermediary object that can receive and pass on the events raised in class A on the client side, so that class B on the server needs only to reference one object*
I would go the interface route first. The interface is in essence doing the job that your intermediary object is doing, only basically for free.
-- modified at 17:47 Wednesday 23rd November, 2005
|
|
|
|
|
Thank you for your help, Leslie. I will give it a try today. The solution seems reasonable and I was thinking about the interface when I meant intermediary object, just everything is kinda upside down - the server is to subscribe to the event that the clients would raise.
The solution with the interface is a common one, but still I wanted to see is there some other pattern that we should use when going with a delegates, event though I could not remember seeing any such implementation.
The easiest way is to add a reference to the server, but doing that and in a short time the server implementation gets bloated with too many assemblies.
Thanks again.
Sarajevo, Bosnia
|
|
|
|
|
mirano wrote: The easiest way is to add a reference to the server, but doing that and in a short time the server implementation gets bloated with too many assemblies.
I can understand that.
If you do try the interface approach, you may want to put it in the server library/assembly. I'm assuming you have a server assembly and various client assemblies that use the server. In this situation, it's understandable that you wouldn't want the server referencing the clients to avoid assembly bloat, but also to avoid a circular dependency as well.
You could put a IClient interface (or whatever name seems appropriate to you) in the server library. It would represent the basic functionality the server needs to interact with its clients regardless of where or who they are. In this interface would be the event you described earlier. Client libraries would implement this interface and could register themselves with the server; something like a server.Connect(IClient client) method in which the server subscribes to the event in the IClient interface.
Alot of what I've described above is based on some assumptions that may not be true for your situation. At any rate, if it's helpful at all, cool. Just my 2 cents.
|
|
|
|
|
Could not get it to work.
Here's the problem: Class A implements the interface IClient that is on the server, everything's fine. When the method Foo of the class A is executed, the event is raised, passing some data class as a reference. Now, in order to subscribe to the event from the server, I need to do something like this:
Foo f = new Foo();
IClient icl = f as IClient();
and then:
icl.SomeEvent += new ... bla, bla...
but I DO NOT HAVE a reference to class A, method Foo, which is the whole point from the very beginning. Is there another way to subscribe to the event that is wrapped in the interface without having to know the class that's implementing it on the client side?
Thanks.
Sarajevo, Bosnia
|
|
|
|
|
You can have a reference to Class A on the server side without knowing its type as long as it implements an interface the server knows about and the reference is passed as a reference to that interface type.
Let's look at a bit of code so that what I wrote above is more concrete.
In the server library, you have the IClient interface:
public interface IClient
{
event EventHandler ClientChanged;
}
Also, we have the Server class:
public class Server
{
public void Connect(IClient client)
{
client.ClientChanged += new EventHandler(HandleClientChangedEvent);
}
public void Disconnect(IClient client)
{
client.ClientChanged -= new EventHandler(HandleClientChangedEvent);
}
private void HandleClientChangedEvent(object sender, EventArgs e)
{
}
}
Now, the Server can attach and detach itself to the client's event without having to know its type. All it knows is that the client implements the IClient interface.
Make sense?
On the client side, we do have to know about the server, but the dependency is one-side, i.e. not circular, so we're ok:
public class SomeClient
{
public SomeClient(Server s)
{
s.Connect(this);
}
}
-- modified at 13:12 Saturday 26th November, 2005
|
|
|
|
|
Yes, you are right. It works. It was just not that obvious how to get the client throught the interface in order to subscribe to the event.
Thanks.
Sarajevo, Bosnia
|
|
|
|
|
Seems the only reasonable way of playing a sounds - such as a .wav is to go back down to Win32?
Am I right or wrong.
"Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table.
Shameless Plug - Distributed Database Transactions in .NET using COM+
|
|
|
|
|
Giles wrote: Seems the only reasonable way of playing a sounds - such as a .wav is to go back down to Win32?
System.Media[^]
|
|
|
|
|
I guessed there would be something like that. Problem is .NET 1.1.
I've currently got a third party lib, and it does not like 2.0, as its a wrapper for a C++ class, linked to the 7.1 C++ runtime.
Sorry, should have been more specific.
"Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table.
Shameless Plug - Distributed Database Transactions in .NET using COM+
|
|
|
|
|
Giles wrote: I guessed there would be something like that. Problem is .NET 1.1.
I've currently got a third party lib, and it does not like 2.0, as its a wrapper for a C++ class, linked to the 7.1 C++ runtime.
Ah, well in that case, yeah, looks like platform invoke is the way to go. If you just need simple .wav playback, the sndPlaySound looks like it could do the job for you.
If you need some help in this area, let us know. I've had some experience wrapping the MIDI API, so I may be able to give you some pointers on wrapping the sndPlaySound function, or probably someone here at Code Project has already done it.
|
|
|
|
|
Leslie Sanford wrote: If you need some help in this area, let us know. I've had some experience wrapping the MIDI API, so I may be able to give you some pointers on wrapping the sndPlaySound function, or probably someone here at Code Project has already done it.
Thanks. I saw the artiles on CP. Seemed pretty straight formward, and I've used the MM library before from CPP.
Was just hoping there was a cleaner way. Nice to know the've fixed it of sorts for the future.
"Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table.
Shameless Plug - Distributed Database Transactions in .NET using COM+
|
|
|
|
|
Is there any one that can point me in the right direction on how to create a generic printer driver, or is there a walktrough on who to create a printer driver.
I would like a generic printer driver that a can handle the flow of chars and do wath ever a mant with them.
|
|
|
|
|
Not in C# you don't.
For the time being you need to use very low level C++, using the Driver Development Kit or DDK.
There are some good articles on CP about an introduction the black art of driver development.
Saying that in future, becuase so many printer drivers are badly written in Windows Vista, MS is going to make all printer drivers user mode.
http://www.microsoft.com/whdc/devtools/ddk/default.mspx[^]
Saying that you could probably fudge it using a serial cable, or even a netowork based printer, but not by using USB port.
"Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table.
Shameless Plug - Distributed Database Transactions in .NET using COM+
|
|
|
|
|
is there a way i can move the mouse to position (x,y) when a function is called?
|
|
|
|
|
Use Cursor.Position from the Winforms namespace.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
|
I am having a lot of problems with source safe going very slow pulling files. I'm using NAnt to do a GET and build my application. The application consists of about 1000 files, and it takes nearly 10 minutes just to get the files to the server to build. Does anyone else experience this?
Also, how can I make it just pull the changed files? I have another project which contains about 5000 xml files and it takes over an hour to pull all of these, when only 30 have changed. There has to be a way to pull only these 30 in a matter of less than a minute.
Thanks!
Curtis
|
|
|
|
|
glocklt4 wrote: Does anyone else experience this?
Everyone. And that's why anyone with at least a mid-sized project or more than 3 or 5 person working on the same projects migrate to other VCS, like CVS, SVN, Perforce, Bitkeeper, etc.
I see dead pixels
Yes, even I am blogging now!
|
|
|
|
|
So there is now way to make it really only pull the lastest files?
GREAT!!
|
|
|
|
|
Hi there.
I am having a bit of a problem with Paint. I have a form and then when a user selects a certain choice from a menu the program puts several label controls with a text on the form. The program is also supposed to draw some lines when the same selection is made but it does not. This is what I did:
private void dowhatIwant(object sender, EventArgs e)
{
this.Paint += new PaintEventHandler(p2_Paint);
...
... //here is the code that puts the labels on the form
}
...
...
private void p2_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black,1);
Point p1 = new Point(88, 32);
Point p2 = new Point(299, 32);
g.DrawLine(p, p1, p2);
Point p3 = new Point(68, 106);
Point p4 = new Point(115, 106);
g.DrawLine(p, p3, p4);
...
...
}
I have several more lines I want to draw but only the first one shows? Can someone please show me the error of my ways?
Thank you,
FJ
|
|
|
|
|
|
Ok, I have a listview and a treeview. Gif's are being dragged from Listview and borught over to the treeview. I have managed to create a Ghost icon of the GIF when the user clicks on the picture, but the problem I have is that when drag starts, the ICON changes from the ghost icon to the default NO cursor. When it gets in the treeview, its the drag icon.
Can someone suggest as to where I may be going wrong. I can obviosuly post codes snippets, butI am not sure it will be of any help. Its something more basic as to the dragdrop effects that I am not setting.
I mean the drag drop works, the Ghost ICON, would be the icing on the cake.
Thanks for any input.
Sameer
|
|
|
|
|
Could it be because your ListView does not have the
AllowDrop<code> property set to true? In this case, Windows Forms would specify the NO_CURSOR because you can't drop there. <br />
<br />
"we must lose precision to make significant statements about complex systems."<br />
-deKorvin on uncertainty
|
|
|
|
|
I am having a problem with trying to get a series of wav files to play, I just want a simple button that when you press it, it will play a list of wav files, so far all that happen when I try and do this is it will play the last audio file stated in the code several times (the amount of other files I have tried to play.)
Any help that can be given will be much appreciated
Thank you
RoB
|
|
|
|
|