MVC delegates problem...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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?
 
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 said:
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]
- (e-mail address removed)

Dominique said:
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?
 
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]
- (e-mail address removed)

Dominique said:
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 said:
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]
- (e-mail address removed)

Dominique said:
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?
 
Dominique said:
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
 
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.
 
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
 
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.
 
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
 

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

Back
Top