views:

56

answers:

2

What are some lesser known tips for implementing a loosely-coupled MVC structure in a non-trivial desktop application (e.g. having at least two levels of views/controllers and more than one model)?

+1  A: 

Declare event handlers in your interfaces (important for the views). This way you can loosely couple event handling which is managed by the controller. You may need to use the InvokeRequired when working with the view if your application is multi-threaded.

orvado
+1  A: 

Use interfaces.

A lot. I like using the "IDoThisForYou" style (even in a language where this isn't idiomatic) because an interface represents a role that another class can use.

Make the controllers responsible for controlling interaction

The controllers control interaction between domain objects, services, etc.

Use events to pass information between controllers

Let every controller who needs information subscribe to the event. Use an interface.

Don't put presentation information on your domain objects

Instead, allow the controller to create a presenter or view model which has the information you need. This includes no "ToString()". If you're in a language without multiple inheritance you might end up with a bit of duplication between presenters. That's OK - duplication is better than coupling, and the UI changes a lot anyway.

Don't put logic in your gui

Instead, allow the controller to create a presenter or view mdoel which has the information you need. This includes train wrecks like "MyAnimal.Species.Name" - make it present "SpeciesName" instead.

Test it

Manually. There is no substitute. Unit and acceptance testing goes a long way, but there's nothing like bringing the app up and actually using the mess you wrote for finding out how messy it is. Don't pass it to the QAs without having a go yourself.

Oh, and don't mock out domain objects in unit tests. It's not worth it. Use a builder.

Lunivore