"Deep" Assignment operator

S

sklett

I know the assignment operator is shallow and I know it can't be overloaded.

What I can't find (on the web) is a best practice or guideline for assigning
the values of one instance to the value of another instance of objects.
In C++ I would overload the assignment operator to achieve what I want. But
in C# I'm not seeing how to do this other than creating a member function
like "T.DeepAssign(T source)"

The problem is that when you have controls bound to the properties of an
object that support INotifyPropertyChanged the Binding instances are
referencing the original instance supplied in the Binding constructor. So
if I change the reference that the Binding is pointing to no events are
fired to let the Binding know. I hope that makes sense. I'm typing this
like I have it all figured out but I'm really just getting my head around
still.

I'd rather not re-bind everything when my reference changes. All I'm really
looking for is best practice (interface to support, method name,
anything....) for batch property assignment.
IE:
<code>
Type.DeepAssign(Type source)
{
this.Prop1 = source.Prop1;
this.Prop2 = source.Prop2;
}
</code>

A method like the above would notify my Bindings that the properties have
changed and all will be well in the world.

Am I missing some basic concept here? I'm surprised that I'm even needing
to think about this so much, seems there would be something in place to
assign the properties of one object to another.

-Steve
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

Instead of doing this just to facilitate the updating of the bindings
that are attached to the control, you could just set a new reference to the
binding, then cycle through the Bindings exposed by the PropertyManager (the
Binding has a reference to the BindingManagerBase, which is a
PropertyManager instance, if you are not binding to a list, but an object
instance) and call the ReadValue method on each of the bindings. This
should force all the controls to refresh themselves, and you should be able
to write a generic method which will take any BindingManagerBase (or
Binding), replace the instance it is attached to, and then refresh all the
controls attached to the instance.
 
S

Steve K.

Hi Nicholas,

Thanks for the reply. With my current design I don't have access to the
binding at the point the business entity change is caught. It's hard to
explain, but basically it's something like this:

(using MVP (or trying!))

1) Main View has several UserControls
2) Presenter gives MainView the model to bind to (Patient)
3) MainView passes properties of the Model onto the inner UserControls
("level 1") for them to bind to (Patient.Insurance, Patient.Physician, etc)
4) Some "level 1" UserControls have their own inner UserControls ("level
2") and they pass the Model on for them to bind to
(Patient.Insurance.Address, Patient.Physician.Address, etc)
5) "level 1" UserControl fires an event that MainView catches and requests
some data from Presenter
6) Presenter gives MainView the new data but this data is bound to "level
2" UserControls which MainView doesn't have access to.

I'm not saying it's a good design, but it's what I've got at this point and
I can't change it right now.... but I plan to because I'm sure it's
terrible.

Because MainView has a reference to the top level business entity that
everything else is bound to, it can dig down and set the new data and the
"level 2" UserControl will receive the property change notifications and the
UI will update.

I hope that all makes sense.... just wanted to explain why I didn't do the
obvious thing and update the binding reference.

I'm going to re-read your suggestion again, there might be some gems in
there that I can work with.

Thanks again for the help,
Steve


Nicholas Paldino said:
Steve,

Instead of doing this just to facilitate the updating of the bindings
that are attached to the control, you could just set a new reference to
the binding, then cycle through the Bindings exposed by the
PropertyManager (the Binding has a reference to the BindingManagerBase,
which is a PropertyManager instance, if you are not binding to a list, but
an object instance) and call the ReadValue method on each of the bindings.
This should force all the controls to refresh themselves, and you should
be able to write a generic method which will take any BindingManagerBase
(or Binding), replace the instance it is attached to, and then refresh all
the controls attached to the instance.


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

sklett said:
I know the assignment operator is shallow and I know it can't be
overloaded.

What I can't find (on the web) is a best practice or guideline for
assigning the values of one instance to the value of another instance of
objects.
In C++ I would overload the assignment operator to achieve what I want.
But in C# I'm not seeing how to do this other than creating a member
function like "T.DeepAssign(T source)"

The problem is that when you have controls bound to the properties of an
object that support INotifyPropertyChanged the Binding instances are
referencing the original instance supplied in the Binding constructor.
So if I change the reference that the Binding is pointing to no events
are fired to let the Binding know. I hope that makes sense. I'm typing
this like I have it all figured out but I'm really just getting my head
around still.

I'd rather not re-bind everything when my reference changes. All I'm
really looking for is best practice (interface to support, method name,
anything....) for batch property assignment.
IE:
<code>
Type.DeepAssign(Type source)
{
this.Prop1 = source.Prop1;
this.Prop2 = source.Prop2;
}
</code>

A method like the above would notify my Bindings that the properties have
changed and all will be well in the world.

Am I missing some basic concept here? I'm surprised that I'm even
needing to think about this so much, seems there would be something in
place to assign the properties of one object to another.

-Steve
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

If you don't have access to the binding (which I don't fully understand
really, because the controls that have the instance pushed on them have
access to the binding, don't they?), then you will have to perform a copy,
like you stated in your OP.

In that case, my guidance would be to have a Copy method which takes a
parameter of the type that you are copying, and then work from there.


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

Steve K. said:
Hi Nicholas,

Thanks for the reply. With my current design I don't have access to the
binding at the point the business entity change is caught. It's hard to
explain, but basically it's something like this:

(using MVP (or trying!))

1) Main View has several UserControls
2) Presenter gives MainView the model to bind to (Patient)
3) MainView passes properties of the Model onto the inner UserControls
("level 1") for them to bind to (Patient.Insurance, Patient.Physician,
etc)
4) Some "level 1" UserControls have their own inner UserControls ("level
2") and they pass the Model on for them to bind to
(Patient.Insurance.Address, Patient.Physician.Address, etc)
5) "level 1" UserControl fires an event that MainView catches and
requests some data from Presenter
6) Presenter gives MainView the new data but this data is bound to "level
2" UserControls which MainView doesn't have access to.

I'm not saying it's a good design, but it's what I've got at this point
and I can't change it right now.... but I plan to because I'm sure it's
terrible.

Because MainView has a reference to the top level business entity that
everything else is bound to, it can dig down and set the new data and the
"level 2" UserControl will receive the property change notifications and
the UI will update.

I hope that all makes sense.... just wanted to explain why I didn't do
the obvious thing and update the binding reference.

I'm going to re-read your suggestion again, there might be some gems in
there that I can work with.

Thanks again for the help,
Steve


Nicholas Paldino said:
Steve,

Instead of doing this just to facilitate the updating of the bindings
that are attached to the control, you could just set a new reference to
the binding, then cycle through the Bindings exposed by the
PropertyManager (the Binding has a reference to the BindingManagerBase,
which is a PropertyManager instance, if you are not binding to a list,
but an object instance) and call the ReadValue method on each of the
bindings. This should force all the controls to refresh themselves, and
you should be able to write a generic method which will take any
BindingManagerBase (or Binding), replace the instance it is attached to,
and then refresh all the controls attached to the instance.


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

sklett said:
I know the assignment operator is shallow and I know it can't be
overloaded.

What I can't find (on the web) is a best practice or guideline for
assigning the values of one instance to the value of another instance of
objects.
In C++ I would overload the assignment operator to achieve what I want.
But in C# I'm not seeing how to do this other than creating a member
function like "T.DeepAssign(T source)"

The problem is that when you have controls bound to the properties of an
object that support INotifyPropertyChanged the Binding instances are
referencing the original instance supplied in the Binding constructor.
So if I change the reference that the Binding is pointing to no events
are fired to let the Binding know. I hope that makes sense. I'm typing
this like I have it all figured out but I'm really just getting my head
around still.

I'd rather not re-bind everything when my reference changes. All I'm
really looking for is best practice (interface to support, method name,
anything....) for batch property assignment.
IE:
<code>
Type.DeepAssign(Type source)
{
this.Prop1 = source.Prop1;
this.Prop2 = source.Prop2;
}
</code>

A method like the above would notify my Bindings that the properties
have changed and all will be well in the world.

Am I missing some basic concept here? I'm surprised that I'm even
needing to think about this so much, seems there would be something in
place to assign the properties of one object to another.

-Steve
 

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