Skip to main content
added 4 characters in body
Source Link
Sam
  • 123
  • 8

Here is WPF application consisting from 3 UserControls:

enter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism.

I need to invoke servicecustom class method (which is model in terms of MVVM) in UserControl3 from view-model of UserControl1. The restriction that service in UserControl3 can't be singleton. I suppose to do it one of the following way:

  1. Using event aggregator from Prism. UserControl1 view-model is publisher and UserControl3 model is subscriber. For this I'll need to create unique Id in Window and pass it to UserControl1 and UserControl3.

  2. Creating service instance in WindowWindow and pass it to UserControl1 and UserControl3. Then UserControl1 just invoke method on this instance.

  3. Window justWindow pass UserControl2 instance to UserControl1. View-model in UserControl1 will just invoke method of UserControl2, which will invoke method of UserControl3 and so on.

It seems like 2 and 3 approaches violates MVVM. What will you prefer?

Here is WPF application consisting from 3 UserControls:

enter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism.

I need to invoke service method (which is model in terms of MVVM) in UserControl3 from view-model of UserControl1. The restriction that service in UserControl3 can't be singleton. I suppose to do it one of the following way:

  1. Using event aggregator from Prism. UserControl1 view-model is publisher and UserControl3 model is subscriber. For this I'll need to create unique Id in Window and pass it to UserControl1 and UserControl3.

  2. Creating service instance in Window and pass it to UserControl1 and UserControl3. Then UserControl1 just invoke method on this instance.

  3. Window just pass UserControl2 instance to UserControl1. View-model in UserControl1 will just invoke method of UserControl2, which will invoke method of UserControl3 and so on.

It seems like 2 and 3 approaches violates MVVM. What will you prefer?

Here is WPF application consisting from 3 UserControls:

enter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism.

I need to invoke custom class method (which is model in terms of MVVM) in UserControl3 from view-model of UserControl1. The restriction that service in UserControl3 can't be singleton. I suppose to do it one of the following way:

  1. Using event aggregator from Prism. UserControl1 view-model is publisher and UserControl3 model is subscriber. For this I'll need to create unique Id in Window and pass it to UserControl1 and UserControl3.

  2. Creating service instance in Window and pass it to UserControl1 and UserControl3. Then UserControl1 just invoke method on this instance.

  3. Window pass UserControl2 instance to UserControl1. View-model in UserControl1 will just invoke method of UserControl2, which will invoke method of UserControl3 and so on.

It seems like 2 and 3 approaches violates MVVM. What will you prefer?

deleted 1250 characters in body
Source Link
Sam
  • 123
  • 8

Here is WPF application consisting from 3 UserControls:

enter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism, so all UI events processing via commands in view-model (VM). All data logic encapsulated in services (classes) which are used by VM.

In one side, I need to invoke service method UserControl3 has a(which is DataModulemodel class and VM of UserControl3 needs to be notified when this module finished some work. In another side, UserControl1 has a button, which should start asynchronous execution of logic in DataModule. Due to restrictions, DataModule can't be singleton.

What is the best way to connect this two sides in terms of MVVM?

TL;DR:

From now I see two completely different approaches:

First approach:

Both view-models from UserControl3 and UserControl1 should have instance of this module (variables with type of module interface). So UserControl1 VM simply invoke method of this module in command's body, while UserControl3 VM subscribes to module's event and receive notification.

To achieve this, all three UserControls should have custom initialize methods (in their code-behind) to pass module instance into target view-models. Module instance should be created in mainfrom Window, which pass instance toview-model of UserControl1 and UserControl2 during initialization (UserControl2 in turn will pass module to UserControl3).

  The problem isrestriction that I'm not sure if it correct in MVVM for window to create data moduleservice in it's code-behind and pass it directly to instances of UserControls.

Second approach:

Let UserConrol2UserControl3 inherit interfacecan't be singleton. Then window pass UserConrol2 instanceI suppose to UserConrol1, which in turn passdo it to VM to store in interface variable. When button clicked, VM invoke method of UserConrol2 interface variable. That means triggering method in UserConrol2 code-behind, which will invoke methodone of UserConrol3, which will invoke its view-model method, which will start module executing and await its result in async/await manner.the following way:

  1. Using event aggregator from Prism. UserControl1 view-model is publisher and UserControl3 model is subscriber. For this I'll need to create unique Id in Window and pass it to UserControl1 and UserControl3.

  2. Creating service instance in Window and pass it to UserControl1 and UserControl3. Then UserControl1 just invoke method on this instance.

  3. Window just pass UserControl2 instance to UserControl1. View-model in UserControl1 will just invoke method of UserControl2, which will invoke method of UserControl3 and so on.

I believe that not only this approach extremelyIt seems like 2 and 3 approaches violates MVVM by heavily using model logic in code-behind, but this approach also makes code hard to understand.

  What approach canwill you suggest according to your experienceprefer?

Here is WPF application consisting from 3 UserControls:

enter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism, so all UI events processing via commands in view-model (VM). All data logic encapsulated in services (classes) which are used by VM.

In one side, UserControl3 has a DataModule class and VM of UserControl3 needs to be notified when this module finished some work. In another side, UserControl1 has a button, which should start asynchronous execution of logic in DataModule. Due to restrictions, DataModule can't be singleton.

What is the best way to connect this two sides in terms of MVVM?

TL;DR:

From now I see two completely different approaches:

First approach:

Both view-models from UserControl3 and UserControl1 should have instance of this module (variables with type of module interface). So UserControl1 VM simply invoke method of this module in command's body, while UserControl3 VM subscribes to module's event and receive notification.

To achieve this, all three UserControls should have custom initialize methods (in their code-behind) to pass module instance into target view-models. Module instance should be created in main Window, which pass instance to UserControl1 and UserControl2 during initialization (UserControl2 in turn will pass module to UserControl3).

  The problem is that I'm not sure if it correct in MVVM for window to create data module in it's code-behind and pass it directly to instances of UserControls.

Second approach:

Let UserConrol2 inherit interface. Then window pass UserConrol2 instance to UserConrol1, which in turn pass it to VM to store in interface variable. When button clicked, VM invoke method of UserConrol2 interface variable. That means triggering method in UserConrol2 code-behind, which will invoke method of UserConrol3, which will invoke its view-model method, which will start module executing and await its result in async/await manner.

I believe that not only this approach extremely violates MVVM by heavily using model logic in code-behind, but this approach also makes code hard to understand.

  What approach can you suggest according to your experience?

Here is WPF application consisting from 3 UserControls:

enter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism.

I need to invoke service method (which is model in terms of MVVM) in UserControl3 from view-model of UserControl1. The restriction that service in UserControl3 can't be singleton. I suppose to do it one of the following way:

  1. Using event aggregator from Prism. UserControl1 view-model is publisher and UserControl3 model is subscriber. For this I'll need to create unique Id in Window and pass it to UserControl1 and UserControl3.

  2. Creating service instance in Window and pass it to UserControl1 and UserControl3. Then UserControl1 just invoke method on this instance.

  3. Window just pass UserControl2 instance to UserControl1. View-model in UserControl1 will just invoke method of UserControl2, which will invoke method of UserControl3 and so on.

It seems like 2 and 3 approaches violates MVVM. What will you prefer?

added 19 characters in body
Source Link
Sam
  • 123
  • 8

Here is WPF application consisting from 3 UserControls:

enter image description hereenter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism, so all UI events processing via commands in view-model (VM). All data logic encapsulated in services (classes) which are used by VM.

In one side, UserControl3 has a DataModule class and VM needof UserControl3 needs to be notified when this module finished some work. In another side, UserControl1 has a button, which should start asynchronous execution of logic in DataModule. Due to restrictions, DataModule can't be singleton.

What is the best way to connect this two sides in terms of MVVM?

TL;DR:

From now I see two completely different approaches:

First approach:

Both view-models from UserControl3 and UserControl1 should have instance of this module (variables with type of module interface). So UserControl1 VM simply invoke method of this module in command's body, while UserControl3 VM subscribes to module's event and receive notification.

To achieve this, all three UserControls should have custom initialize methods (in their code-behind) to pass module instance into target view-models. Module instance should be created in main Window, which pass instance to UserControl1 and UserControl2 during initialization (UserControl2 in turn will pass module to UserControl3).

The problem is that I'm not sure if it correct in MVVM for window to create data module in it's code-behind and pass it directly to instances of UserControls.

Second approach:

Let UserConrol2 inherit interface. Then window pass UserConrol2 instance to UserConrol1, which in turn pass it to VM to store in interface variable. When button clicked, VM invoke method of UserConrol2 interface variable. That means triggering method in UserConrol2 code-behind, which will invoke method of UserConrol3, which will invoke its view-model method, which will start module executing and await its result in async/await manner.

I believe that not only this approach extremely violates MVVM by heavily using model logic in code-behind, but this approach also makes code hard to understand.

What approach can you suggest according to your experience?

Here is WPF application consisting from 3 UserControls:

enter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism, so all UI events processing via commands in view-model (VM). All data logic encapsulated in services (classes) which are used by VM.

In one side, UserControl3 has a DataModule class and VM need to be notified when this module finished some work. In another side, UserControl1 has a button, which should start asynchronous execution of logic in DataModule. Due to restrictions, DataModule can't be singleton.

What is the best way to connect this two sides in terms of MVVM?

TL;DR:

From now I see two completely different approaches:

First approach:

Both view-models from UserControl3 and UserControl1 should have instance of this module (variables with type of module interface). So UserControl1 VM simply invoke method of this module in command's body, while UserControl3 VM subscribes to module's event and receive notification.

To achieve this, all three UserControls should have custom initialize methods (in their code-behind) to pass module instance into target view-models. Module instance should be created in main Window, which pass instance to UserControl1 and UserControl2 during initialization (UserControl2 in turn will pass module to UserControl3).

The problem is that I'm not sure if it correct in MVVM for window to create data module in it's code-behind and pass it directly to instances of UserControls.

Second approach:

Let UserConrol2 inherit interface. Then window pass UserConrol2 instance to UserConrol1, which in turn pass it to VM to store in interface variable. When button clicked, VM invoke method of UserConrol2 interface variable. That means triggering method in UserConrol2 code-behind, which will invoke method of UserConrol3, which will invoke its view-model method, which will start module executing and await its result in async/await manner.

I believe that not only this approach extremely violates MVVM by heavily using model logic in code-behind, but this approach also makes code hard to understand.

What approach can you suggest according to your experience?

Here is WPF application consisting from 3 UserControls:

enter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism, so all UI events processing via commands in view-model (VM). All data logic encapsulated in services (classes) which are used by VM.

In one side, UserControl3 has a DataModule class and VM of UserControl3 needs to be notified when this module finished some work. In another side, UserControl1 has a button, which should start asynchronous execution of logic in DataModule. Due to restrictions, DataModule can't be singleton.

What is the best way to connect this two sides in terms of MVVM?

TL;DR:

From now I see two completely different approaches:

First approach:

Both view-models from UserControl3 and UserControl1 should have instance of this module (variables with type of module interface). So UserControl1 VM simply invoke method of this module in command's body, while UserControl3 VM subscribes to module's event and receive notification.

To achieve this, all three UserControls should have custom initialize methods (in their code-behind) to pass module instance into target view-models. Module instance should be created in main Window, which pass instance to UserControl1 and UserControl2 during initialization (UserControl2 in turn will pass module to UserControl3).

The problem is that I'm not sure if it correct in MVVM for window to create data module in it's code-behind and pass it directly to instances of UserControls.

Second approach:

Let UserConrol2 inherit interface. Then window pass UserConrol2 instance to UserConrol1, which in turn pass it to VM to store in interface variable. When button clicked, VM invoke method of UserConrol2 interface variable. That means triggering method in UserConrol2 code-behind, which will invoke method of UserConrol3, which will invoke its view-model method, which will start module executing and await its result in async/await manner.

I believe that not only this approach extremely violates MVVM by heavily using model logic in code-behind, but this approach also makes code hard to understand.

What approach can you suggest according to your experience?

Source Link
Sam
  • 123
  • 8
Loading