473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

MVC delegates problem...

In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddView(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHandler(view.DataModified);
}

and at the right time I invoke the event list via...
ViewChanged(this, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?
Dec 8 '05 #1
8 2153
Dominque,

From what you have shown, it doesn't seem like you should be having that
problem. Is there any point where you remove the delegates and it is
possible that you are removing it?

Is this all of the code?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dominique" <Do*******@discussions.microsoft.com> wrote in message
news:B0**********************************@microsof t.com...
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded
at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddView(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHandler(view.DataModified);
}

and at the right time I invoke the event list via...
ViewChanged(this, e);

This all works fine as long as I only have 1 instance of each type of
view.
So in this case I have a Pie Chart View and Bar Chart view of the data (
as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?

Dec 8 '05 #2
Hi Nicholas,
Thanks for your prompt reply.

RemoveView is only called once in the whole project group and that occurs in
the MVCView destructor as follows...

~MvcView()
{
if ( DataModel != null )
DataModel.RemoveView( this );
}

I planned to make this demo public once I ironed out this last bug and
commented the code for other newbies to hopefully learn by.
The other stuff it also shows...
0. How to create an MDI application
1. How to use GDI+ for Pie Charts and Graphs
2. Double Buffering to avoid flickering
3. Type Enumeration using Relection
4. How create an ArrayList descendent
5. How to use the ArrayList descendent in a DataGrid
6. It shows how you may want to implement a simple plugin system

Dominique

"Nicholas Paldino [.NET/C# MVP]" wrote:
Dominque,

From what you have shown, it doesn't seem like you should be having that
problem. Is there any point where you remove the delegates and it is
possible that you are removing it?

Is this all of the code?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dominique" <Do*******@discussions.microsoft.com> wrote in message
news:B0**********************************@microsof t.com...
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded
at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddView(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHandler(view.DataModified);
}

and at the right time I invoke the event list via...
ViewChanged(this, e);

This all works fine as long as I only have 1 instance of each type of
view.
So in this case I have a Pie Chart View and Bar Chart view of the data (
as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?


Dec 8 '05 #3
Dominque,

Don't do this. This is not a destructor. Rather, it is a finalizer,
and it is not called when the object goes out of scope. Rather, it is
called when the object is being reclaimed by a collection by the GC.

Also, I don't think the model you have is really a good one. Since you
are exposing the events, why not just have the views add the event handlers
themselves when they are constructed? You can pass the model to the views,
and they can decide how to hook up to it.

Your views should implement IDisposable (I imagine they do already), and
in that implementatio of Dispose, you can remove the event handler (so that
a reference to you is not kept through the delegate).

I don't know that this will solve your problem, but I think that there
is more to it. I haven't heard of any problems of delegates just being
dropped from event invocation lists...
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dominique" <Do*******@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
Hi Nicholas,
Thanks for your prompt reply.

RemoveView is only called once in the whole project group and that occurs
in
the MVCView destructor as follows...

~MvcView()
{
if ( DataModel != null )
DataModel.RemoveView( this );
}

I planned to make this demo public once I ironed out this last bug and
commented the code for other newbies to hopefully learn by.
The other stuff it also shows...
0. How to create an MDI application
1. How to use GDI+ for Pie Charts and Graphs
2. Double Buffering to avoid flickering
3. Type Enumeration using Relection
4. How create an ArrayList descendent
5. How to use the ArrayList descendent in a DataGrid
6. It shows how you may want to implement a simple plugin system

Dominique

"Nicholas Paldino [.NET/C# MVP]" wrote:
Dominque,

From what you have shown, it doesn't seem like you should be having
that
problem. Is there any point where you remove the delegates and it is
possible that you are removing it?

Is this all of the code?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dominique" <Do*******@discussions.microsoft.com> wrote in message
news:B0**********************************@microsof t.com...
> In order to become more familiar with the Model-View-Controller
> pattern, I
> have written a demo where each View is a plugin and the plugins are
> loaded
> at
> startup from the plugins directory. When the View's are loaded they add
> themselves to the DataModel View list...
>
> FDataModel.AddView(this);
>
> Where AddView looks like this...
> public void AddView( MvcView view )
> {
> ViewChanged += new ViewChangedHandler(view.DataModified);
> }
>
> and at the right time I invoke the event list via...
> ViewChanged(this, e);
>
> This all works fine as long as I only have 1 instance of each type of
> view.
> So in this case I have a Pie Chart View and Bar Chart view of the data
> (
> as
> well as the usual DataGrid view all as plugins ). Then the delegates
> correctly fire and everything works as advertised. The problem arises
> if I
> instantiate a second instance of say the Bar Chart. When that happens
> the
> first Bar Chart stops responding/refreshing while the second Bar Chart
> updates correctly.
>
> Can any C# pattern gurus shed some light on what I might have
> overlooked?


Dec 8 '05 #4
Dominique wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddView(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHandler(view.DataModified);
}

and at the right time I invoke the event list via...
ViewChanged(this, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?


I've seen some strange effects similar to this before. You could try
redesigning your chart and its data source to model the observer
pattern. This way you wouldnt need the event and the delegate chain.
Here is a good example of the observer pattern:
http://www.dofactory.com/Patterns/PatternObserver.aspx

Jason
Dec 9 '05 #5
Hi Dominique,

It may sound strange but can you check the id's of the views you've created ?
If the id's are the same than that's the problem. I've had a problem with
mutiple custom controls that raise events, they had the same Id. After
solving that problem things went ok again.

It's of course just a guess.

Good Luck and let us know when you've solved the problem Ok.
--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"Dominique" wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddView(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHandler(view.DataModified);
}

and at the right time I invoke the event list via...
ViewChanged(this, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?

Dec 9 '05 #6
Hi Rainer,
Can you clarify what you mean by ID's? And how do I get my views to have
separte IDs?

If you mean that I should be generating a GUID or similar for each instance,
I assumed ( possibly wrongly ) that a new instance of a class would make the
instance unique as it is created at a separate date and time to other classes.

Thanks,
Dominique.

Dominique

"Rainier [MCT]" wrote:
Hi Dominique,

It may sound strange but can you check the id's of the views you've created ?
If the id's are the same than that's the problem. I've had a problem with
mutiple custom controls that raise events, they had the same Id. After
solving that problem things went ok again.

It's of course just a guess.

Good Luck and let us know when you've solved the problem Ok.
--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"Dominique" wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddView(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHandler(view.DataModified);
}

and at the right time I invoke the event list via...
ViewChanged(this, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?

Dec 9 '05 #7
Hi John,
I'm not yet totally familiar with the observer pattern, but it sounds like
it only waits to be notified of changes, but not not actually produce
changes. Is this a correct interpretation of the pattern? If so, then it
won't do in this case as I allow the user to interact with the graph to
change sales values, creative accounting if you will :).

Regards,
Dominique.

"Jason Shortt" wrote:
Dominique wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddView(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHandler(view.DataModified);
}

and at the right time I invoke the event list via...
ViewChanged(this, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?


I've seen some strange effects similar to this before. You could try
redesigning your chart and its data source to model the observer
pattern. This way you wouldnt need the event and the delegate chain.
Here is a good example of the observer pattern:
http://www.dofactory.com/Patterns/PatternObserver.aspx

Jason

Dec 9 '05 #8
Hi Dominique,

The thing I had, I was programming Asp.Net Server controls creating
instances of these and adding them to a single page. This didn't work because
of the UniqueId that wasn't Unique.
I created the UniqueId's myself and afther that the code started working.
The real problem here is that the code I'm talking about is from a old
project for a old emplorer on an old laptop. So there really isn't a way for
me to show you the code.

Maybe you can set break points within your views to check there
UniqueId's/Guids just try checking everything that needs to be unique when
creating new instances.

Good luck,

--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"Dominique" wrote:
Hi Rainer,
Can you clarify what you mean by ID's? And how do I get my views to have
separte IDs?

If you mean that I should be generating a GUID or similar for each instance,
I assumed ( possibly wrongly ) that a new instance of a class would make the
instance unique as it is created at a separate date and time to other classes.

Thanks,
Dominique.

Dominique

"Rainier [MCT]" wrote:
Hi Dominique,

It may sound strange but can you check the id's of the views you've created ?
If the id's are the same than that's the problem. I've had a problem with
mutiple custom controls that raise events, they had the same Id. After
solving that problem things went ok again.

It's of course just a guess.

Good Luck and let us know when you've solved the problem Ok.
--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"Dominique" wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddView(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHandler(view.DataModified);
}

and at the right time I invoke the event list via...
ViewChanged(this, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?

Dec 12 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
0
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can...
4
by: Stephen | last post by:
I am new to C# and can't get my head round what delegates are and what they are for. can anyone enlighten me?
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
15
by: Marshal | last post by:
First... let's deal with Delegates. Comments welcome. 1) Invoking a NULL delegate is annoying. ** (It should just do nothing rather than crash.) 2) It's too easy to accidently attach multiple...
14
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB...
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.