ObservableCollection(of T): Only for UI-tier or for all tiers?

H

Henrik Dahl

Hello!

In some situations I have a collection of objects which it in different
situations could be relevant to deal with at the UI-tier and sometimes at
other tiers. Typically my concept is a composite so MyCollectionElementClass
has a collection of MyCollectionElementClass.

Should I make one implementation for the UI-tier using
ObservableCollection(MyCollectionElementClass) and one for the non-UI tier
using List(MyCollectionElementClass) or should I use the implementation
using ObservableCollection(MyCollectionElementClass) for all tiers?

A more general question: Should ObservableCollection(Of T) basically only be
used at UI-tiers due to some performance or infrastructural reasons for
instance?


Best regards,

Henrik Dahl
 
N

Nicholas Paldino [.NET/C# MVP]

Henrik,

I don't see why it shouldn't be used at all tiers. ObservableCollection
doesn't have any UI specific functionality. It is basically a Collection<T>
instance which implements INotifyCollectionChanged and
INotifyPropertyChanged. These interfaces, while they can be used for UI,
don't HAVE to be strictly for UI. You might want some code to run when
certain properties are changed (in a chain reaction sort of way). The most
prominent example is UI changes, but not by any means the only example.

Hope this helps.
 
H

Henrik Dahl

Hello Nicholas,

Yes, but if you invoke some methods from a non-UI thread an exception is
actually raised from the ObservableCollection(Of T) instance. This could
indicate there's something more into this ObservableCollection because what
you've described probably should not lead to such treading related
behaviour. What do you think?


Best regards,

Henrik Dahl

Nicholas Paldino said:
Henrik,

I don't see why it shouldn't be used at all tiers.
ObservableCollection doesn't have any UI specific functionality. It is
basically a Collection<T> instance which implements
INotifyCollectionChanged and INotifyPropertyChanged. These interfaces,
while they can be used for UI, don't HAVE to be strictly for UI. You
might want some code to run when certain properties are changed (in a
chain reaction sort of way). The most prominent example is UI changes,
but not by any means the only example.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henrik Dahl said:
Hello!

In some situations I have a collection of objects which it in different
situations could be relevant to deal with at the UI-tier and sometimes at
other tiers. Typically my concept is a composite so
MyCollectionElementClass has a collection of MyCollectionElementClass.

Should I make one implementation for the UI-tier using
ObservableCollection(MyCollectionElementClass) and one for the non-UI
tier using List(MyCollectionElementClass) or should I use the
implementation using ObservableCollection(MyCollectionElementClass) for
all tiers?

A more general question: Should ObservableCollection(Of T) basically only
be used at UI-tiers due to some performance or infrastructural reasons
for instance?


Best regards,

Henrik Dahl
 
N

Nicholas Paldino [.NET/C# MVP]

Henrik,

That isn't the case. There is nothing in ObservableCollection which is
tied to the UI. The error is expected, as you are trying to update the UI
thread from a non UI thread (something not allowed in WPF as well as Win32).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henrik Dahl said:
Hello Nicholas,

Yes, but if you invoke some methods from a non-UI thread an exception is
actually raised from the ObservableCollection(Of T) instance. This could
indicate there's something more into this ObservableCollection because
what you've described probably should not lead to such treading related
behaviour. What do you think?


Best regards,

Henrik Dahl

Nicholas Paldino said:
Henrik,

I don't see why it shouldn't be used at all tiers.
ObservableCollection doesn't have any UI specific functionality. It is
basically a Collection<T> instance which implements
INotifyCollectionChanged and INotifyPropertyChanged. These interfaces,
while they can be used for UI, don't HAVE to be strictly for UI. You
might want some code to run when certain properties are changed (in a
chain reaction sort of way). The most prominent example is UI changes,
but not by any means the only example.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henrik Dahl said:
Hello!

In some situations I have a collection of objects which it in different
situations could be relevant to deal with at the UI-tier and sometimes
at other tiers. Typically my concept is a composite so
MyCollectionElementClass has a collection of MyCollectionElementClass.

Should I make one implementation for the UI-tier using
ObservableCollection(MyCollectionElementClass) and one for the non-UI
tier using List(MyCollectionElementClass) or should I use the
implementation using ObservableCollection(MyCollectionElementClass) for
all tiers?

A more general question: Should ObservableCollection(Of T) basically
only be used at UI-tiers due to some performance or infrastructural
reasons for instance?


Best regards,

Henrik Dahl
 
H

Henrik Dahl

Hello again!

OK, so your point is, that the exception is not thrown by
ObservableCollection(Of T) but by some action performed by an event handler
erroneously invoked by the non-UI thread in this case?


Best regards,

Henrik Dahl

Nicholas Paldino said:
Henrik,

That isn't the case. There is nothing in ObservableCollection which is
tied to the UI. The error is expected, as you are trying to update the UI
thread from a non UI thread (something not allowed in WPF as well as
Win32).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henrik Dahl said:
Hello Nicholas,

Yes, but if you invoke some methods from a non-UI thread an exception is
actually raised from the ObservableCollection(Of T) instance. This could
indicate there's something more into this ObservableCollection because
what you've described probably should not lead to such treading related
behaviour. What do you think?


Best regards,

Henrik Dahl

Nicholas Paldino said:
Henrik,

I don't see why it shouldn't be used at all tiers.
ObservableCollection doesn't have any UI specific functionality. It is
basically a Collection<T> instance which implements
INotifyCollectionChanged and INotifyPropertyChanged. These interfaces,
while they can be used for UI, don't HAVE to be strictly for UI. You
might want some code to run when certain properties are changed (in a
chain reaction sort of way). The most prominent example is UI changes,
but not by any means the only example.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello!

In some situations I have a collection of objects which it in different
situations could be relevant to deal with at the UI-tier and sometimes
at other tiers. Typically my concept is a composite so
MyCollectionElementClass has a collection of MyCollectionElementClass.

Should I make one implementation for the UI-tier using
ObservableCollection(MyCollectionElementClass) and one for the non-UI
tier using List(MyCollectionElementClass) or should I use the
implementation using ObservableCollection(MyCollectionElementClass) for
all tiers?

A more general question: Should ObservableCollection(Of T) basically
only be used at UI-tiers due to some performance or infrastructural
reasons for instance?


Best regards,

Henrik Dahl
 
N

Nicholas Paldino [.NET/C# MVP]

Henrik,

Yes.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henrik Dahl said:
Hello again!

OK, so your point is, that the exception is not thrown by
ObservableCollection(Of T) but by some action performed by an event
handler erroneously invoked by the non-UI thread in this case?


Best regards,

Henrik Dahl

Nicholas Paldino said:
Henrik,

That isn't the case. There is nothing in ObservableCollection which
is tied to the UI. The error is expected, as you are trying to update
the UI thread from a non UI thread (something not allowed in WPF as well
as Win32).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henrik Dahl said:
Hello Nicholas,

Yes, but if you invoke some methods from a non-UI thread an exception is
actually raised from the ObservableCollection(Of T) instance. This could
indicate there's something more into this ObservableCollection because
what you've described probably should not lead to such treading related
behaviour. What do you think?


Best regards,

Henrik Dahl

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> skrev
i en meddelelse Henrik,

I don't see why it shouldn't be used at all tiers.
ObservableCollection doesn't have any UI specific functionality. It is
basically a Collection<T> instance which implements
INotifyCollectionChanged and INotifyPropertyChanged. These interfaces,
while they can be used for UI, don't HAVE to be strictly for UI. You
might want some code to run when certain properties are changed (in a
chain reaction sort of way). The most prominent example is UI changes,
but not by any means the only example.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello!

In some situations I have a collection of objects which it in
different situations could be relevant to deal with at the UI-tier and
sometimes at other tiers. Typically my concept is a composite so
MyCollectionElementClass has a collection of MyCollectionElementClass.

Should I make one implementation for the UI-tier using
ObservableCollection(MyCollectionElementClass) and one for the non-UI
tier using List(MyCollectionElementClass) or should I use the
implementation using ObservableCollection(MyCollectionElementClass)
for all tiers?

A more general question: Should ObservableCollection(Of T) basically
only be used at UI-tiers due to some performance or infrastructural
reasons for instance?


Best regards,

Henrik Dahl
 
H

Henrik Dahl

OK.

Do you know if instances of ObservableCollection(Of T) may also be
marshalled via the serialization mechanishms of WCF or via XmlSerializer?


Best regards,

Henrik Dahl

Nicholas Paldino said:
Henrik,

Yes.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henrik Dahl said:
Hello again!

OK, so your point is, that the exception is not thrown by
ObservableCollection(Of T) but by some action performed by an event
handler erroneously invoked by the non-UI thread in this case?


Best regards,

Henrik Dahl

Nicholas Paldino said:
Henrik,

That isn't the case. There is nothing in ObservableCollection which
is tied to the UI. The error is expected, as you are trying to update
the UI thread from a non UI thread (something not allowed in WPF as well
as Win32).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello Nicholas,

Yes, but if you invoke some methods from a non-UI thread an exception
is actually raised from the ObservableCollection(Of T) instance. This
could indicate there's something more into this ObservableCollection
because what you've described probably should not lead to such treading
related behaviour. What do you think?


Best regards,

Henrik Dahl

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
skrev i en meddelelse Henrik,

I don't see why it shouldn't be used at all tiers.
ObservableCollection doesn't have any UI specific functionality. It
is basically a Collection<T> instance which implements
INotifyCollectionChanged and INotifyPropertyChanged. These
interfaces, while they can be used for UI, don't HAVE to be strictly
for UI. You might want some code to run when certain properties are
changed (in a chain reaction sort of way). The most prominent example
is UI changes, but not by any means the only example.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello!

In some situations I have a collection of objects which it in
different situations could be relevant to deal with at the UI-tier
and sometimes at other tiers. Typically my concept is a composite so
MyCollectionElementClass has a collection of
MyCollectionElementClass.

Should I make one implementation for the UI-tier using
ObservableCollection(MyCollectionElementClass) and one for the non-UI
tier using List(MyCollectionElementClass) or should I use the
implementation using ObservableCollection(MyCollectionElementClass)
for all tiers?

A more general question: Should ObservableCollection(Of T) basically
only be used at UI-tiers due to some performance or infrastructural
reasons for instance?


Best regards,

Henrik Dahl
 
N

Nicholas Paldino [.NET/C# MVP]

Henrik,

Well, did you check the documentation? *smiles*

It says that the Serializable attribute is applied to it, which means
that instances of it can be serialized, assuming that T is serializable.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henrik Dahl said:
OK.

Do you know if instances of ObservableCollection(Of T) may also be
marshalled via the serialization mechanishms of WCF or via XmlSerializer?


Best regards,

Henrik Dahl

Nicholas Paldino said:
Henrik,

Yes.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Henrik Dahl said:
Hello again!

OK, so your point is, that the exception is not thrown by
ObservableCollection(Of T) but by some action performed by an event
handler erroneously invoked by the non-UI thread in this case?


Best regards,

Henrik Dahl

"Nicholas Paldino [.NET/C# MVP]" <[email protected]> skrev
i en meddelelse Henrik,

That isn't the case. There is nothing in ObservableCollection which
is tied to the UI. The error is expected, as you are trying to update
the UI thread from a non UI thread (something not allowed in WPF as
well as Win32).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello Nicholas,

Yes, but if you invoke some methods from a non-UI thread an exception
is actually raised from the ObservableCollection(Of T) instance. This
could indicate there's something more into this ObservableCollection
because what you've described probably should not lead to such
treading related behaviour. What do you think?


Best regards,

Henrik Dahl

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
skrev i en meddelelse Henrik,

I don't see why it shouldn't be used at all tiers.
ObservableCollection doesn't have any UI specific functionality. It
is basically a Collection<T> instance which implements
INotifyCollectionChanged and INotifyPropertyChanged. These
interfaces, while they can be used for UI, don't HAVE to be strictly
for UI. You might want some code to run when certain properties are
changed (in a chain reaction sort of way). The most prominent
example is UI changes, but not by any means the only example.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello!

In some situations I have a collection of objects which it in
different situations could be relevant to deal with at the UI-tier
and sometimes at other tiers. Typically my concept is a composite so
MyCollectionElementClass has a collection of
MyCollectionElementClass.

Should I make one implementation for the UI-tier using
ObservableCollection(MyCollectionElementClass) and one for the
non-UI tier using List(MyCollectionElementClass) or should I use the
implementation using ObservableCollection(MyCollectionElementClass)
for all tiers?

A more general question: Should ObservableCollection(Of T) basically
only be used at UI-tiers due to some performance or infrastructural
reasons for instance?


Best regards,

Henrik Dahl
 
L

Linda Liu [MSFT]

Hi Henrik,

I agree that ObservableCollection(Of T) can be used for all tiers.
the exception is not thrown by ObservableCollection(Of T) but by some
action performed by an event handler erroneously invoked by the non-UI
thread in this case?

In my opinion, because the ObservableCollection(Of T) implements the
INotifyCollectionChanged and INotifyPropertyChanged interfaces, it will
cause UI update when the ObservableCollection(Of T) is changed.

Let's say a non-UI thread accesses and changes the ObservableCollection(Of
T), then it is the non-UI thread that performs the UI update in the next
step, which is not allowed in the thread models of both WinForms and WPF
applications.

In WinForms world, the solution is to define a delegate for the method to
access the control, and then call the Invoke or BeginInvoke method of the
WinForms control to invoke the delegate.

In WPF world, the solution is similar but is a little different. We still
need to define a delegate for the method to access the UI element, and then
call the Invoke or BeginInvoke method of the Dispather linked to the UI
thread.

The following is a sample.

delegate void SetTextBoxDelegate();
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new
SetTextBoxDelegate(SetTextBox1));
void SetTextBox1()
{
this.textBox1.Text = "some text";
}

For more information on the threading model in WPF, you may visit the
following link:

'Threading Model'
http://msdn2.microsoft.com/en-us/library/ms741870.aspx

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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