16.08.2012 Views

dotnetrocks 0638 rob eisenberg

dotnetrocks 0638 rob eisenberg

dotnetrocks 0638 rob eisenberg

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

examples of apps that you'd used where these<br />

classes would come into play. If you start by thinking<br />

of a basic web browser and not a modern web<br />

browser with tabs but back a generation or two web<br />

browsers where you navigated from page to page and<br />

that was it. Think of the lifecycle that the web browser<br />

is enforcing upon that page. There are certain actions<br />

that occur at the load of that page. There are certain<br />

actions that need to occur at the unload of that page,<br />

and in fact there are certain JavaScript things that can<br />

happen that would prevent navigating away from that<br />

page. So there's sort of a lifecycle around that. So<br />

one of the things that conductors do is we extract that<br />

notion of a screen lifecycle into a super class and we<br />

basically say, hey, if you inherit your ViewModels from<br />

screen and you put them inside of this conductor<br />

class, then we'll sort of maintain this workflow of<br />

activation and deactivation and call back into various<br />

key points so that you can affect the logic in the flow<br />

of those things. This scenario comes up all the time.<br />

Just imagine if you're building a Silverlight navigation<br />

app, that's one scenario identical to what I described.<br />

If you're building a Wizard, you can think of that as a<br />

series of pages you're navigating to and you may<br />

need to stop the navigation back or forward at certain<br />

points. Or if you're thinking of . . .perhaps you're<br />

editing a customer record and somebody wants to<br />

close the record, and they have changed the data but<br />

you want to stop them from closing that record without<br />

notifying them, hey, you're going to lose what you<br />

save. . . these sort of scenarios start cropping up all<br />

over the place and you have to -- what we found<br />

ourselves doing, at least, was re-writing a lot of the<br />

logic to handle those over and over again. And this is<br />

also a place where you end up getting into. . . if there<br />

is any place I've seen or found myself really creating<br />

spaghetti code, it’s around these types of scenarios<br />

because you end up starting to, you know, if you start<br />

from scratch you start to wire up events and you want<br />

to see, okay, when is this unloading and when is this<br />

loading, and I've got to call back into this method, and<br />

it turns into a tangled mess. So we approach this<br />

from a ViewModel-first perspective where we sort of<br />

embed these lifecycle patterns into a couple of<br />

different types of ViewModels and if you inherit from<br />

these you just override a couple of methods and we'll<br />

just tell you, hey, activate yourself, or deactivate<br />

yourself, or hey, we're about to close. Do you want to<br />

allow the screen to close? And so on and so forth.<br />

So there are various different incarnations of these<br />

patterns and you can compose them in different ways<br />

to create all different kinds of shell behaviors. So if<br />

you think you like Visual Studio, it’s got a bunch of<br />

screens active at the same time but for the most part<br />

you've got that document area where you've got<br />

maybe a C# file open, maybe a XAML file open,<br />

maybe an XML file open. Depending on which one<br />

of those files is active, the toolbars change.<br />

Rob Eisenberg MVVMs Us with Caliburn.Micro!<br />

February 17, 2011<br />

Carl Franklin: Yeah.<br />

Rob Eisenberg: You might get an XML<br />

document specific toolbar or you might get toolbar<br />

icons related to C# when you’ve activated that. So all<br />

these kinds of scenarios where you need to sort of<br />

know what the user is looking at, what's activated,<br />

because you've got to change other parts of the<br />

application, those are the kinds of things that the<br />

screens and conductor pieces are literally just like<br />

Lego blocks; you can kind of assemble them together<br />

to make whatever type of shell you're making and<br />

then override the appropriate method that's necessary<br />

to control the workflow and things like that. So a little<br />

bit, you know, at first I think as people try to jump into<br />

this particular feature it can be a little bit confusing but<br />

when you're building a real application and you come<br />

across these scenarios and you try to solve them,<br />

that's when using these features just make it as<br />

simple as driving and overriding a method and<br />

everything just works. We've gone through several<br />

different implementations of these types of patterns<br />

across various different projects we’ve built and we<br />

arrived at the implementation we have now and<br />

they're all composable. So you can imagine -- let me<br />

give you a little more complex scenario so you can<br />

imagine how this might work. Back to the web<br />

browser, think about the modern web browser where<br />

you have multiple tabs, each one of those individual<br />

tabs goes through multiple pages. So the web<br />

browser knows which tab you're looking at right now<br />

and may do things depending on when you switch<br />

from tab to tab, but also things happen inside of that<br />

page when you go from page to page to page in front<br />

of the tab. So you've got all these kind of complex<br />

interactions that are happening and we can model this<br />

all very simply by composing our screens and<br />

conductors in a particular way to form that type of a<br />

shell pattern. So it just make those things really,<br />

really simple and helps you avoid spaghetti code.<br />

Like I said, if you don't come across that scenario,<br />

then it would overly complicate your solution to use<br />

them. But when you do find yourself needing to<br />

handle activation or shutdown of certain screens,<br />

cancellation, all these kinds of things, then it’s there<br />

for you to compose in and very simply to use – that’s<br />

it in a nutshell. There's a lot of thinking and a lot of<br />

evolution behind that and a lot of personal pain trying<br />

to solve those p<strong>rob</strong>lems in the past.<br />

Richard Campbell: I'm trying to imagine how this<br />

shows up in Visual Studio. Is it pretty much just<br />

transparent, just a set of libraries you've loaded?<br />

Rob Eisenberg: So you know all those things<br />

are just base classes for ViewModels.<br />

Richard Campbell: Okay.<br />

Transcription by PWOP Productions, http://www.pwop.com Page 10 of 13

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!