0

I am making a program that does database navigation with relation tables.

For example you have a person class and a car class, and when adding a new person you need to also add which car they drive. So you have a browse button which opens the same window you used for people, and creates another instance of that view model. Keep in mind that my viewmodel is generic, so it works on all types.

What I need is some pattern that communicates between those two viewmodels so that it can, in a way, return a selected car if you know what i mean.

Any suggestions?

8
  • Try something and ask for help with it. Don't just sit there begging someone to think for you. Commented Aug 5, 2016 at 12:11
  • First of all I wouldn't have asked if I hadn't tried anything. Second of all, I am asking for a pattern that already exists, so if you consider that to be 'begging someone to think for you' then maybe we shouldn't even be using any existing pattern for anything in programming. Commented Aug 5, 2016 at 12:12
  • Lots of people ask without trying anything. Their questions look just like yours. Why should I think you tried anything? You didn't say what you tried, or what the problems were. Commented Aug 5, 2016 at 12:14
  • I said what my problems were, I didn't say what I had tried because what I tried failed Commented Aug 5, 2016 at 12:15
  • You mean something like a PropertyInjection? I agree with Ed, thats its hard to consult you, not knowing your Apllicationstructure. Codeexamples make it easier for us to imagine, what might help you Commented Aug 5, 2016 at 13:14

2 Answers 2

1

I cant comment, so here it goes.

So you have 2 instances of your ViewModel object and you need to know what type each represents to convert stored in ViewModel generic object to that type.

Try store Type in ViewModel so you can Convert.ChangeType(object, Type) in your ViewModel any object to specific type.

Sign up to request clarification or add additional context in comments.

1 Comment

OP mentioned, viewmodels are generic. Anyway, chageType is one of the worst built in functions of .net and indicate a bad architecture.
0

What you are looking for is referred to as the Mediator Pattern.

https://en.wikipedia.org/wiki/Mediator_pattern

In Prism and various MVVM libraries it is sometimes referred to as the "EventAggregator". There are various versions you can implement, but the basic idea is to inject in the mediator and the VMs subscribe or publish to a known event. The event can pass any data you need with it.

This ensures your VMs do not become coupled and allows them to communicate and is scalable as you can just keep adding new events to the event aggregator and VMs can pub/sub as they need to.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.