Live Coding C# & WPF - Principal Software Engineering Manager at Microsoft
I'll be coding live in C# and WPF - focused on a helpful facade pattern!
View Transcript
all right folks I'm just checking to make sure my stream is active in the right spots before I start blabbing too much going to be doing some live coding so last night's stream was more just general software engineering so if you didn't check that out and you're interested in that kind of thing uh Monday nights usually at 9:30 p.m. PST I am live streaming across platforms to talk about General software engineering topics um last night I started about 30 minutes early and that actually worked out pretty well so going forward I might start those streams at 9:00 p.m. PST to talk about General software engineering um so I might make that adjustment that gave me about two hours hey rambling geek good to see you told you I'd be back on uh and then today and maybe this will be a pattern I can do
going forward but I I still want to make sure I can get some more time carved out regularly for coding live streams um I don't know if I can commit to it yet on a weekly basis but Tuesday Morning seems to be pretty good um the exception to that is if I have to do software engineering interviews so I might for for YouTube I should clarify so what I might do is if this time seems to work well I might just block that in my schedule and and just keep it free for live streaming so um I do realize that people want to see more coding live streams so that's why I want to mix them in uh I am going to be going through some WPF today and I figured a good exercise I I've leveraged something like what I want to build today
uh for many years and it was really helpful and I want to go rebuild it kind of from scratch um which is going to be interesting because I don't have a I don't have a copy of it somewhere to reflect on actually I do I don't have it pulled up in front of me but I wonder if I could dig that up um then so today I've discovered word interrupt doesn't work with net a Windows Form map I'm not sure what word interrupt is if you want to elaborate I'm not sure what that is um so the thing that I want to build today is uh it's a facade pattern and obviously there's uh many reasons why you might build a facade pattern um the one that I want to look at today is for oh word interop interesting I man I spent a long
time doing interop um and almost none of it successfully so I am not sure I will be in a good spot to help you on interop stuff but so the facade pattern that I want to build today is going to focus on what's an easy way to explain this okay let me I'm going to jump over to some code actually and that might just make it easier to show you the traditional way that we do this kind of thing and then uh from there I can show you why I'm trying to build the thing that I'm building so let me go flip over I really got to get this uh this Elgato thing I've been it's been like weeks now and I mentioned like I have one of these stream deck things and I should set it up to get my Transitions and stuff I
can just press a button on here and flip over I haven't done that yet and then the other kind of crappy thing is that as I'm streaming when I switch over to this full screen thing here I disappear and then I have to toggle my camera and bring it back on and here I am again so I'm kind of annoying but you have three stream decks I don't like the thing is I got it because it seemed fancy like it would help and I'm sure it will but then I actually never never set it up to use it so it feels kind of silly and pardon me sorry if you hear uh shaking and stuff I I haven't had any caffeine yet today and if you know anything about me then I have a caffeine problem uh that I've been working on so uh I'm
I've reduced my caffeine and take a great deal but I still need it to to kind of think clearly okay so in WPF the way that we kind of navigate this stuff is traditionally when you are building that's weird why is this the preview is borked let me see if I can reopen it not that it really matters but seems kind of weird okay cool there's a preview uh hello thoughts on 18-month orange badge contractors at Microsoft versus full-time employees um also a caffeine problem yes a lot of us do have caffeine problems uh before I answer the badge and contractor part on caffeine uh I seriously um I recommend people pay a little bit of attention to it because I I I'll explain this very briefly I don't drink coffee and many years ago especially from being into bodybuilding and pre-workout for the gym
I was like I'm not going to pay that kind of money for pre-workout it's insane like it it equates to like every time I want to go workout and have pre-workout it's like2 to3 doll just to go lift weights like in in supplements right and not including like protein powder and stuff that the math doesn't check out it's really silly and I discovered that you could buy pure caffeine powder for very very cheap and when I say very cheap I mean like 10 years worth of caffeine for like 50 bucks if you were to have a scoop of caffeine that was 200 milligrams every day so like a pre-workout scoop typical kind of thing and so I bought bulk caffeine powder and I just mix it with water and a bit of flavor and I've been doing that for well over a decade uh probably
closer to like 15 years now and the side effect of that is that I haven't really realized how much it's crept up on me for how much caffeine I have and I mentioned this in a couple of posts and uh stuff before but the scoop the 200 milligram scoop I have I guess over time I have swapped it with a different scoop accidentally just because I have lots of little Scoops and it's no longer a 200 milligram scoop I think it's a 300 milligram scoop does it not have a used by date kind of it's just a powder though it's dry so if you keep it dry it's not a big deal there's a lot of like a lot of supplements like that that they can go beyond their shelf life and it's just so that people will say typically they can't really guarantee it's
okay but I've had caffeine that's many many years old and it's totally fine so the problem is my scoop over time had changed to something that was closer to 300 milligrams and I wasn't really paying attention and so I was probably in the upper ranges having close to like 1.2 to 1.5 G of caffeine every day um every single day I could have caffeine I could have a 200 milligram scoop of caffeine and go to sleep um no issue so I required it but it also didn't have like it was basically just to keep me at Baseline of functioning and uh I was having sleep problems as you might imagine but the Sleep problems were not falling asleep I could fall asleep very easily so I went to the doctor to have you know to do a sleep study and then they did what's called
a nap test after and it turns out that I have narcolepsy now they didn't make the connection that it could be due to caffeine which in hindsight I strongly believe that it is but what I learned in my own sort of research I say research very Loosely my youtubing and reading articles is that um the the fact that you can fall asleep easy after ingesting caffeine is kind of irrelevant like sure that's fine whatever but the quality of your sleep is actually greatly diminished when you have caffeine in your system so you fall into this vicious cycle of like okay even if you're going to get your eight hours of sleep like all of that sleep is pretty crappy and then the next day you require caffeine just to get to Baseline again so if you extrapolate that over many years it was just an
increasingly worse problem um and I honestly think think that it led to me kind of being in a state where I have narcolepsy uh I'm not a doctor so I can't guarantee that but the test came back and from my nap test and it looks like I have narcolepsy so I said like I need to change my caffeine intake so I'm down to about 400 milligram a day before 300 p.m. which is still a lot but it's before 3 p.m. and I'm noticing like even to get up this morning I didn't need caffeine I used to literally set a first first alarm to take caffeine so that my second alarm I could wake up and get out of bed so not a good spot to be in um so anyway be careful with your caffeine and take seriously just like be conscious of how much
you're taking and um sleep is incredibly important I only sleep like you know 6 hours a night hopefully trying to get close to seven kind of thing eight makes me feel more tired but um the quality is important so just keep it in mind on the on the contractor part I've not heard of the orange bad labeling before um I've definitely worked with contractors at Microsoft um I don't have any contractors on my team the and and for what it's worth I should offer a bit of context um I work in the substrate side of of Microsoft so that's within Office 365 substrate is sort of the infrastructure we build for all of The Office 365 services including exchange and things like that so um contract contractors at least for me contractors don't report to me I've had one contractor on my previous team who
was not a direct report he was someone that worked on the team but he had his own manager um I'm assuming there's managers that have more like basically their focus is around organizing and managing the the contractors but uh that person worked on the team just like anyone else they were a site reliability engineer they were great um awesome to work with you know they'd come to the meetings and stuff the only difference was that like they and I shouldn't say the only difference the only thing that I observed was that from like a normal working perspective was that their day was like literally was from 9: to 5:00 like they work those hours that's it um and that's totally fine so I call that out not because I expect people to work long days and stuff but I call it out because we have
teams in China like we have people that work in India so your timing could be very different to support those types of uh geographies and it could vary from day to day so some days I might have to work a little late so I can be online for China and then I might go you know start a little later the next day or take a longer lunch but for the contractors it was very much like they have a working time frame and they work within those hours so uh it was really just more about having awareness of it but it was awesome to work with those contractors so um I I think you know if that's a for Luke on YouTube if that's something you're interested in definitely um I have not had any negative interactions with contractors or perceived it as a role or
a situation where like oh this doesn't feel good this is crappy it's been very much a positive experience so I think that's a a cool opportunity um I've never looked at the job postings for that type of thing but if they're there yeah I think that's a great opportunity okay so WPF here we go um traditionally in WPF what we're doing is you're able to drag and drop controls and you're basically building things with the designer so what we're looking at is the uh zaml for the code um yeah my own caffeine Vice monster white I agree sugarfree monster white is a great flavor we drag and drop controls in the designer and you kind of build top down so you have like even here I'm dealing with a parent window we might go like I've dropped in a grid so uh it's kind of
hard to see cuz the grid doesn't have a color but like if we do background uh great right Alice blue is the most light color ever let's just do red so you can see it so like that's a grid that's inside of the window right so you build from the top and then um I should shouldn't say top I want to say top level control but like the window is the the the bottom level and then you stack things on top of it so you have the window and then you have the r on top of it then we could drop in panels we could drop in buttons so you keep layering these things on but you do it when I'm saying top down I'm kind of talking about the organization of the zaml right so this top level and then we keep nesting stuff
further and further this is traditionally how you go build WPF apps windform applications a lot of user interfaces you have this hierarchy and we drop stuff in and then generally what happens is you get to a point where you're like okay I have this custom control so the custom control you might drop in Let's Pretend This label just for the sake of discussion this label let's say is a custom control now the label itself might be some other control like I said it's custom and then we are dropping in more things within that um so instead of seeing the hierarchy continue deeper and deeper like nesting more and more controls here for this again pretend custom control it kind of stops here and you have some other file that is showing that nesting okay so the point is that I'm just trying to like explain
the shape of how we build these user interfaces with respect to code and uh controls so where are we going from there well I think generally that a lot of my experience working WPF has led me to find that the more and I've said this on a couple of live streams before and some of the the more recent YouTube videos that have gone out but my experience has been the more things that we put directly into zaml the worse experience we have with things like dependency injection with testing and things like that so um I'm not telling you that you cannot go design stuff this way I think it's absolutely the way that the the team intended this is how all the tooling is set up how a lot of the patterns and stuff look so I'm not saying that it's wrong I'm just saying
that a lot of how I like to develop software with theend injection testing in mind more things going into zaml generally makes things tricky so what I want to build today is I don't have a good word for it I'm going to call it uh based on some Unity stuff I've done uh I'm going to call it like a Stitcher and what we're going to be able to do is take different controls and we're going to be able to combine them so I can Nest things directly inside of them and I've used this before to be able to combine WPF and forms in a way that feels pretty seamless so you don't have to think oh crap did I put um you know the right control there like you basically ask this Stitcher to go do it for you and we can build some things
that will automatically put in the Wind forms hosting you can do it the other way where it's Wind forms as a base and you have WPF hosting so there's a whole bunch of stuff we can do but it's really about building a facade which is going to give us a nice API that we want to work with that's the deal here it's like we want to work with it it's not just putting code together that works we're focused on the API interaction that we want to have um on the Contracting stuff my Dev experience in it is at a non- tech company where contractors work super hard and full-time employees don't ah um yeah I mean there I guess it's hard to it's hard to say this right like um are there companies where full-time employees don't work hard sure um sure there's lots uh
you know Microsoft is so huge can I say are there employees at Microsoft that don't work super hard sure because I think that's the case at any company right I'm not picking on Microsoft I'm trying to be extremely uh General in my answer there so uh yeah I'm sorry if that's been your experience it's kind of crappy if like as a contractor if you find like we're putting in so much effort and the full-time people aren't I can understand that being frustrating um but yeah be interesting depending on where you want to take a Contracting position within the uh within the company like I said I more familiar with the substrate side but Microsoft is huge so could be a could be a very cool opportunity okay so the reason that I like doing this is that this is actually code that I've got from
a previous video and it's funny why is this not building already that's funny visuals the text is probably way too small for you to see but bottom left it says build succeeded but you can see I have red squigglies it's like nope these things don't work um it's just the things out of date I guess I probably have to reload the project but anyway this is code from a previous YouTube video and uh actually this is code from the previous one of the previous live streams this YouTube video is not live yet but um the focus of this was taking the binding out of zaml right I'm literally building a binding in the code behind and I was trying to explain that uh you can set things up this way technically you can get some better testability because it's in code and not zaml so
some things you can do that way um pomax I can't find beginner friendly material to learnnet from zero um this is I promise folks this is not a plant in the audience uh I'm just going to drop in some some chor links if you're curious so uh one sec just getting it on my other screen here so I do have chuses on this stuff I try to put out as much free material as I possibly can just as a heads up but this is my getting started C course if you interested in that um and then there is a bundle where you get 20% off if you take that course and the next one that builds on top of it so the second one is the bundle that has both um again I don't I'm not the kind of person who's going to um tell
you like hey take my course you're going to be an expert in a week because it's just lying that's not fair um anyone who's promising you that you're going to be an expert after a very short amount of time is just it's false in any skill it just nothing there is nothing in the world that works that way unless you happen to be a prodigy and it's like accidental right um it's total fluke so um I don't promise people that you'll be an expert if you take my course because I don't want to lie to you um those two courses should get you to a a point where you're able to to build applications in C have a good level of comfort and then from there if you want to start doing things in asp.net or maybe you want to go with Maui or something
else that should put you in a direction where you feel more comfortable to jump over to those other things so hopefully that helps uh poax and for other folks that are interested in that um that seems a bit like it's spam so I'm not going to respond to that um okay so in the code behind here the direction that I start to head in and it like I've mentioned before in other videos other streams it starts to feel counter intuitive that we move things into code behind away from zaml and what I'm going to start to do is build up my controls in code behind and that means that I'm able to construct the user interface in code and we start to lose the preview features which is probably in my opinion the biggest drawback of this approach but if you're building modular um user
controls and stuff like that what you're able to do is preview what the control looks like again if I go back to main Windows ammo previews just oh there it is it's going to say it's just gone um you can preview things individually but you start having placeholders where you would go Nest your um your custom controls and if you're doing it the traditional way you get to kind of preview it the whole way down so if you have custom controls or then custom controls you can preview that whole thing it's very powerful so we start to lose that ability of having really good preview but your individual controls we can still preview so I just want to um I always like sharing pros and cons to things and I want to make that clear that we start to lose that capability um but the
thing that I want to do and I'm going to show you what the syntax looks like um and I think we might need a uh parent here so let me do this x name grid container it's really hard to type when you're streaming and you have a mic in your face so I have grid did it not save I think something's going on with my um my code file here because it keeps saying the stuff isn't available very weird um let me go ahead and do this one sec I'm going to unload this project I think there's something weird with uh the state of this thing so I'm going to unload and reload it this is a I'm not I can't claim that this is actually a you know it was busted I think it is and it's a pretty common thing that can happen
when you have a lot of uh generated code and all the WPF stuff is so still doesn't exist interesting that might make this very hard to do I'm just double-checking the name space and stuff but it feels very weird that you can't see this stuff like even initialized component it can't see usually this kind of thing happens if your name space is messed up but I have the name space here just double checking no still not okay uh I'm going to still explain what I'm trying to do here it's going to be a little bit difficult to see um without the intellisense and stuff working but uh I don't know why this class seems broken it's very odd but let's keep going so what I want to be able to do is have functionality that says well okay this is kind of thanks co-pilot um
I want to be able to do this kind of thing where in code I'm adding controls okay but the difference is that um I want to make it this is where it gets really weird I want to make it agnostic to WPF and Wind forms and potentially other user interface Technologies which you might be saying well that's impossible there's only like WPF only works with Wind forms or whatever else but that's kind of the beauty of the facade is that we can if someone built a control ever one type of control just to give you an example even um you could write uh something like grid container ad and then give it literally HTML as an I'm just giving you an example here and the facade would go oh someone's provided us HTML that means we must put a web component in there right the
facade itself can take care of doing some heav Lifting for us behind the scenes it kind of makes things feel a little bit more magical because you're thinking about the API you want to work with and all of the complexity is behind the scenes so to give you an example um and the syntax that we're going to be dealing with is not quite like this but you could have I don't know maybe you have a new HTML class or record type or something let me get this spaced out a little bit better you might do like sure there's some HTML partially um okay so you could have this kind of thing going on and you would say well Nick it's impossible because how are you going to add HTML into a grid container and this is where we start to change how the API looks
to give us this behavior and then we could go make an HTML type the facade that we go to build knows hey look I have a grid container that I'm dealing with and someone's given me HTML I understand behind the scenes how to go wire these things up this is also a really cool opportunity for plugins to make this extensible so I want to show you how to go build this um I have about an hour and a half to be able to make some progress on this but um this will be something that I was think about this I probably will release this as a nougat package which will be kind of fun um I have used this pattern this type of thing extensively I've used it professionally I've used it in my hobby video game stuff which is why I was saying I
actually do think I have code for this and I might on the side here try to pull it up but hopefully that concept makes sense it's just that this API that we have to deal with is not going to work like this literally this code here will never compile it's never going to work unless we built an HTML control but like I was saying I want to try and hide the fact that we are explicitly dealing with uh WPF and Wind forms or anything else so that's what we're going to build I'm just checking GitHub on the side here because I have a private repo that should have some of this stuff in it and it would actually help me a lot if I had that pulled up to glance over at and hilariously it's not in GitHub it's in bitbucket because this is before
we had free private repos on GitHub some people not might not remember that but I remember being like man like I just just want to put my code up somewhere and like I don't want it to be public cuz I don't want people going through and poking at my stuff I'm not comfortable with it [Music] so where is it this one let me see so to give you an example of why even in my video game development I use something like this um so a lot of stuff I was doing was building in unity but this facade that we're about to go build literally would allow me to to go build a user interface and change out the entire front end which sounds kind of weird you might be saying well why would you want to do that and the reason is that when I
started I built the same role playing game like over the past 21 years that I've been programming I've always been rebuilding it it's a hobby thing that I I don't have time for it currently but when I find time I like to go back to it and kind of reinvent it but I always found that I didn't like kind of sticking to a game engine I was like oh like and funny like a lot of people are crapping on Unity now like they don't like Unity for whatever reason and I'm like okay if I wouldn't built everything in unity I would be kind of screwed here if everyone moved away from it and that wasn't the right engine to use so I basically allowed myself to build tons of the business logic agnostic to any um game engine and then that way even the user
interface I could go building the user interface in code and then swap in the front end for the most part it's a little bit more complicated than that but that was part of the goal um so let me see if I can find it what not there okay one more spot to check I'm doing this on a different screen by the way just because they're private repositories but I think it should be this one there we go okay I had a different name for it I didn't call it stitching in this I had stitching in my Unity stuff meaning something else um let's see this one is I'm going to take some syntax as a starting point and give me well whatever I'll just paste it in so we're going to make this thing we'll call it Stitcher Factory okay so the syntax and the
reason changing it is I had this view welder thing so instead of weld it's going to be Stitch oh man I've talked about this before I'm streaming to Tik Tok right now and some of the keyboard shortcuts like I'm in Visual Studio the keyboard shortcut control h is being swallowed by the Tik Tok streaming application and that's super annoying um because number one it doesn't have Focus number two I can't use that shortcut at all so sorry that I'm doing this the long way but I can't do a search and replace okay so this is going to seem super weird but this is the syntax that I would start to use so you would have if we go back down here I'm going to show you this like I'm going to keep jumping between things just so you can see how it works uh and
I'll be a little bit more thoughtful about this we would pass in a Stitcher Factory if I could type it Stitcher Factory we'd pass this thing in using dependency injection and then the stitcher Factory we would say I need to create a Stitcher and you could give it the type that you want to use so you can have a simple Stitcher you might be able to have the Stitch the type that I want you to think about is that um it's going to be really hard to explain this but you might want to have something that's like I want a control and I want to drop it inside of another control and anchor it somewhere so maybe you want to center it like um horizontally and vertically so you want to give it some some type of parameters about how to lay it out or
maybe you want to have like a grid Stitcher so that's kind of what we have here is that we want to give it like a row and a column so you could do like I so give me a grid Stitcher and I want to use the parent which is grid container and then the first example we have is like this kind of thing so one sec uh what's a good way to do this just to make it readable so you do something like this Stitcher there we go and then you would say the word Stitch is losing all of its meaning because I'm typing it so much but you could do this kind of thing or you could basically do it all in one line like this one call so that's an example of the the facade or the API that I want to be
able to have so we asked the stitcher Factory I want to have a Stitcher that has these types of things and this actually should say like um and then column to make it a little bit more obvious create me a grid Stitcher here's the parent and here's the child and if we go back and look up here object and object right is the types so it'll take anything so I want you to make me a Stitcher that can combine these two things and Stitch them together and then we get get a Stitcher back and then we ask to stitch so this interface here is what's going to allow us to call how we want to stitch it you could use something like a I had a default one that was just like this and then instead of here you get this kind of thing so
like this was an alias for using like an nice simple Stitcher that took no parameters just as an example but a lot of the time I was you know I build interfaces mostly in WPF with grids that was pretty common for me um what's the other one like a flow panel I think it's been a while but that was the idea and this is what we're going to try to build so hopefully that makes more sense when you see it laid out this way um you could change out this text block for this HTML type that I was talking about um you could even make it so that you can pass in a string and maybe you have the um the facade so this it's called a Stitcher Factory is it is going to be a factory but it's also a facade at the same
time I realize that's confusing you could have this code behind here go parse you could say Hey look it's a string that's passed in is that HTML like you could build that logic in behind the scenes and if you're thinking hey this thing's going to do far too much work that's why I Outsource it to different plugins so hopefully you can see the direction of that maybe not though because it's pretty complicated um so let's keep going on here though so I don't think that my view Stitcher or I guess in my I'm just looking at more code I don't think yeah this interface is empty that I had this is kind of just placeholder if I needed to start enforcing anything I could uh so this is an empty interface right now so don't be too concerned by that it's just that when I
built this thing out before I was like I don't know if at any point I'm going to need to have something in force there and I would rather be able to go find all of my implementations of a Stitcher that I want to work with so that it would fail compilation I could go update all of them um so this is just a pattern that I wanted to do but literally the way things are currently working is like we don't need to do that so actually that's comment that it we don't need to enforce anything on this because there is actually no Properties or methods we have to have um like I said this was just so that if I started building this pattern out and went oh crap technically all stitchers need to do this then I could could go update them all easily
okay so how this is going to look though this is where it starts to get kind of weird um Aaron David I'm assuming that's a tag on Facebook I don't so I'm pulling I want to see if I can find a good example of one um it's funny the the bitbucket search is is not so great margin welder oh so interesting I had them based on like margins and stuff um sure let's just keep going on this I'm going to make a grid Stitcher so and technically I should we don't have to use an i grid Stitcher we don't have to um but I'm going to and the reason why I want to do that is that I might want to have a technology specific implementation so if you're going between Wind forms and WPF like I might need a whole new grid Stitcher to
be able to do that so this is thanks co-pilot this is the interface we're going to be dealing with so you can start to see that this code as I'm adding these pieces in this code is St starting to be closer and closer to being able to compile um of course this silly grid container thing that's so annoying there we go okay so I'm just cheating so I don't know why we can't see this stuff just to make this happy you can see already that is the API that I want to work with right if we had child controls passed in oh not what I wanted uh like this I'm just making stuff up by the way if it's not obvious you could do this kind of thing right and maybe find it a little bit readable like this so you say I want the
stitcher Factory to create me a grid Stitcher for these two controls the parent and the child and here is how I want you to combine them I'm looking at my other code that I have here uh on a different screen for other patterns one I have is a margin welder and when you go to weld and I guess I'm calling it Stitch here so when you go to Stitch things and you want to focus on the margins you would give it parameters about the margins you want to have so that's the I'm trying to explain these different concepts like of these patterns to give you some ideas about what you could do um it's just interesting I I don't know where the code is that implements these I only have um I literally only have the interfaces for them which is kind of funny so
I want to check one more spot because I think can I search across everything no I can't search across all my repositories for code I can only search for the repository names very annoying I just want to see if there's one more spot where I can get an implementation on one of these things and the answer is no so I don't know where my code that I wrote exists for this stuff but we're going to go build one of these uh stitchers and the other thing is so we have to talk about how we register these things because that's another very complicated part so the reason why facades are very nice is that facades let us build an API that we want to have okay so I shouldn't have done that so bar thank you very much so facades let us build the apis we
want to have but we still have to go handle all the complexity because there is nothing that does this literally there's no code that exists right now that will do any of this for us we just have an API but that means we can go figure out how to go build it so we are going to have to do some type of registration um I'm going to go make the actual grid Stitcher implementation and what this needs to do is have a public seal class and we'll call it kind of funny we'll go with what co-pilot's saying to start with you can see that um let me kind of go down here this this the factory needs to create a Stitcher and we're giving it the pair and the child so technically yes in the Constructor of the stitcher instance that we're doing we need
the parent and the child so this is what I would do actually it's parent and child um this is also giving the it's kind of interesting I didn't realize we don't need this we could have void on here we could do that um we could just allow it to return itself and get a bit more of a builder pattern but we don't really need that because once you Stitch you're done with it so let's just do that but this part's wrong this doesn't actually Stitch anything properly so what I need to do uh primary Constructor I don't I'm going to keep away from primary Constructors because I think most people get up in arms about them but we'll do this okay so we have this grid Stitcher we have the parent and the child mapped here and then we would just go do something like
this uh how does that work oh this is going to be fun um I can't remember these are they're dependency properties and basically you can extend things so sorry I have to jog my memory on this I'm just going to take a quick search w PF let's do it over here um this right here perfect so what we've done so far is this part we've added the control into the children but you can see this is a static class or I guess it's a static method on the grid class and we have to do this kind of thing so this is going to set dependency properties it's kind of gnarly um but see set row and set column and then we have not the we don't want the parent we want the child I believe thank you co-pilot so this is a Stitcher we've gone
ahead and we've made a grid Stitcher but it's a special one it's a WPF grid Stitcher the implementation of the things we're dealing with here are WPF related now how this would work is we need to be able to go register it up here onto our Factory so that when someone says hey look here's a parent and here's a child we can say look if the parent is a grid and the child is a UI element then you can use this particular Stitcher now what happens if the parent that's passed in all the way up here when we go to wire it up it's not a grid What do we do right and this is where we could build other stitchers and say look I don't care if you gave me a uh I'm trying to think of the right terminology in WPF I don't
care if you gave me any control that can take children and some other child I will drop a a grid in place for you I will go do that on your behalf so we can go build that and then that way when you go call this thing down here you can say sorry I don't need that um you can go say it doesn't have to be a grid right so um is it called a flow panel no layout panel what are the panel types ah stack padle that's the one stack and wrap so you might be able to do something like this and you say I want to use a grid Stitcher with this stack panel and this text block and you might read this and say well that's again that's impossible this thing is not a grid but we could go make the facade
say hey look you gave me something that has children and it's not a grid I will put a grid in place for you then we can go do the stitching to that grid so you can build the complexity behind the scenes in the facade this is my kind of my message that I want to get across in this stream is that the facade CL or the facade pattern lets us build complexity behind the scenes and I'm going to keep repeating it as we go through this so this is why for this WPF grid Stitcher we're going to have to go look at our Stitcher Factory and figure out how we want to go register these things so uh there's a handful of different ways so when I have something like this that is a facade I know it says Factory but it is behaving as
a facade they don't have to be mutually exclusive by the way I can have methods that allow us after this thing has been created so we've made an instance of a Stitcher Factory we can go then perform registrations that's one way we can do it um these also aren't mutually exclusive we could make it such that when you create this Factory through dependency injection we need to pass in all of the stitchers and how they handle things so that's going to be the approach I want to take for this but you can do it in different ways um we need to have I'm going to give you an example of what I want to be able to do I want to have a collection of stitchers kind of and I want to be able to say um Stitcher mapping let's say and then it would
be something like select almost like this but it's not quite okay sorry co-pilot not right uh I want to be able this would be a a dictionary and the key is not really a key it's really just I need uh tles so I could do a list let me go make this it's going to be easier to show you with an example so I can actually have this as a readon list and I want to have a tupple or Tuple depending on how you like to call it where this is the can match delegate and then this is the actual um well we need a Constructor call back so one sec okay so this is the mapping that we're going to want to deal with so what I need to do is say first or default and we basically have two items in here right
so I want to be able to ask and I'm going to make a type for this so that it's it will read better but it would be something along the lines of like so can match I'm kind of writing the code that I want to go Implement after just for the record because you're like Nick none of this compiles this is ugly this is terrible I get it um but that's part of it for now and that like and even this Tuple that I made like that's not even the right syntax I just trying to kind of give you some pseudo code so we want to see the first or default fault one that can match based on the parent and the child and then we're going to select x dot sorry one sec like this so the first one that we can do and
then we will select oops that's not right um sorry this is what happens when it's not compiling so we need to be able to do this uh create okay so then we do bar Stitcher and then we can say if Stitcher is null argument exception or we could do like a not supported exception will co-pilot get it for us let's come on co-pilot thank you I don't like the formatting of that but something like this right so if that doesn't work we tell them and here's the other thing we're going to do a cast after here so we might need to do another check this is kind of interesting so visual studio suggesting that we can do this thing it's kind of cool um we can combine the exception oh that's not what I thought it was going to do I thought that it was
going to do this oops oh man I lost my clipboard so that's a way that you can do it where you can actually throw an exception if you have a null which is kind of neat okay we have a bunch of stuff that we have to go Implement up here um so this isn't done yet okay public I'm going to make a sealed record it's going to be a Stitcher registration and then I'm going to basically have delegates for these things and then this is going to be a list of Stitcher registrations not a a tupple so the public delegate bull can match thank you co-pilot you know what I want all of the time so we will do that this is going to be the create Stitcher call back and then the can match delegate here and technically I think the the naming so
you don't put call back on these it's delegate delegate on the type name or the delegate name and then call back on the parameter name I think is traditionally doesn't really change anything it's just a naming convention so now we're getting somewhere we actually want that and it's not can matched out it's can match call back and it's not create Stitcher it's create Stitcher call back all of a sudden we're getting somewhere so this code compiles this part's kind of interesting um this will throw an invalid cast exception if we do this stuff wrong so so some things I want to call out about this kind of pattern um what a good way to say this we we can because we're building a lot of complexity behind the scenes we we need to make decisions about like how much we want to trust what's being
created versus not so you can see right here I have this casting right we're going going to cast whatever Stitcher got created to whatever type is being requested so that's something we have to be careful about because we can go break the rules not at compile time so at runtime this could totally explode and we should play around with this to see how how that might look so we can either trust that everyone doing this is doing the right thing and then we can go blindly cast or what I might recommend we do is something like um just to make it a little bit more of a bow why doesn't that work oh sure let's just do this um sorry this is kind of nasty we can be more explicit like does this do a whole lot not really use pattern matching oh cool yeah
is not uh like what is it trying to get me to do so we can do something like this does that work oh nice so I'm also giving it an alias very cool so all that this is doing is giving us a little bit more of a verbose um message so it's saying this Stitcher is not of this type instead of just saying invalid cast whatever but the other thing that we could go do is and I don't think it's really possible in an elegant way is the whole reason that we have to do this is because the result of create Stitcher call back like that needs to be generic so I don't know if we can actually do this very easily but let's go see I might have to undo this so we can go make this up here generic itself oh interesting invoke
thank you okay so yeah that was kind of weird the because these are delegates you can call them just this way or you put in voke on them and if you have generic type arguments you can't you need to use invoke basically to be able to get the generic type arguments so that's what's going on with that um but this is one thing we could do but you can see up here it's already upset with us right we want to go create create a Stitcher registration now we have to go make this generic and that's a problem because it's not consistent that way so I actually might say that we don't want to do that and basically push again push more of the complexity into our facade there are things we could do um you could write tests on this stuff to make sure that
all of your registrations you go exercise them and prove that if a Stitcher says it's going to return a certain type that it does but you can see or maybe it's not obvious so I should elaborate as soon as we start pushing the generic types onto these things I have to then go propagate it here and do this kind of thing which already is not happy about anyway but the point is that like you have to continue passing in the types all the way down and then we're going to get to a point where we just can't really leverage that so I'm going to undo that because this should work for us for us and I think between writing test that cover this kind of thing and having some more verose error messages and error handling here we should be okay but now at this
point we have something that will go work we need to pass this in so we need to actually instantiate this I realize every time I'm typing Stitcher I'm missing a t so I apologize like I said the word Stitcher is losing all of its meaning for me um I also I like doing this in different ways but this will be fine uh I like to copy on passing stuff in but I'm trying to move away from that too the re I like I'll explain this briefly I used to like doing this kind of thing from a safety perspective is you give me something and I will make a copy of it because I don't want to trust whoever was giving it to me but I'm trying to change that around because it's it's very heavy-handed to go make copies of everything that's passed to you
it's just it's just not wise per for performance so we'll move away from that so now we have a grid Stitch we have a way that we can register stuff but let's go see if we can go build one of these things I'm going to move away from man this is a pain in the butt because this class isn't building I want to be able to run this and I don't know if we're going to be able to with this stupid main window okay let me see if I can do something here I don't want to lose this code but I want to go put this code in a spot that we can go run it um and I don't know why this thing isn't building in the first place so just a pain in the butt okay let me I'm going to copy some
stuff out of here I say copy I just mean move we're going to get rid of all that it's on my clipboard no reason to panic I'm just going to drop it into here so this is in our main app I'm going to unload Lo our plugin and bye-bye um now what we're going to be able to do is this Stitch this is going to be kind of weird actually because I'm using screw door up here for assembly scanning okay this Stitcher Factory I don't think this is going to I don't know how to do this with screw door yet I could do it with autoa very easily but I'm going to skip the dependency injection stuff for now I'm going to kind of hijack what's going on here so I'm going to make the um Stitcher Factory and you can see that even co-pilot
saying oh you want to get that off your service provider we're not going to in this case um we're going to build it by hand I just needed a spot where I could go write some code basically because the rest of the app isn't building and I don't know why so the stitcher mapping itself is going to be a list of Stitcher registrations actually just just call it that that's a better name why did I call it mapping oh because it was going to be a dictionary originally that's okay some rename refactoring right there there we go so this is going to be a new list and what we're able to do is is create a new registration is co-pilot going to do it for us though look how smart co-pilot is this is literally what it needs to be here so again I'm going
to talk about you're going to see some complexity here you'll see a lot of stuff that I usually wouldn't recommend people do like why is there so much casting going on here why is there all this type checking and that's because removing all of this gross stuff that I would never recommend scattering across your application we are literally forcing it into one spot hello Nim from Tik Tok good to see you here thanks for joining we're going through some WPF stuff here I'm showing how we can make a facade that lets us combine different user interface elements so this registration when we look at code like this you might say this is the grossest code we have typed checking in two spots we're doing casting in another spot without the type checking on this you know code path here but this is nasty code hidden
behind the scenes that follows a particular pattern that enables this other functionality so this particular Stitcher registration will allow us to say hey look when someone asks this Stitcher Factory to go combine a grid and any other UI element right then we should be able to go make a Stitcher we know based on the the the code path parent is a grid so we can safely cast as it child is a UI element we can safely cast as it now inside of the grid Stitcher we know that we're dealing with a grid and some UI element we know that we can do this stuff so I just wanted to elaborate that sometimes we write nasty code like that and a very particular part of our application so that the rest of our application can flow and feel a lot nicer it's the same reason why
like if you're using um serialization code right or object relational mappers orms those things do some really nasty stuff behind the scenes but they do that so that you can have an API that's awesome to work with you enjoy working with it and you don't have to think about any of that nasty stuff that's what we're doing right here so now if we go back to for example and I unloaded I'm going to reload it so I can copy the code again wish this window was working for us sorry that it's not um I'll re unload that okay just copy this stuff in here we're not going to use the stack panel to start with let's just copy it or comment it out I spelled Stitcher Factory wrong there too my goodness whoever's watching the recorded version of this is like man this guy's an
idiot sorry okay Stitcher Factory we're going to create a Stitcher for our grid container and a text block and we should be able to stitch these things when this thing's done children uh let's see so I'm not writing xunit tests or anything right now I'm just kind of hacking this together so we can prove if it works but grid container do children. count equals 1 that should be true and then we can use these again this is a really weird way to do it but it's because these are like these are dependency Properties or and they call them attached properties is is how it works so text block and any other control does not have a row and a column because that's not a property that exists as the base part of that control but when you have one of these things inside of a
grid I believe it's called an attached property where the grid basically says any child that's within me must have a row in a column so you have this concept of attached properties which is why it's not text block. getet row right these feel like backwards not text block. getet row it's the grid getting the row of the text block so it feels backwards because it is literally in terms of How It's implemented but this should hopefully work for us but let's go see nothing ever works the first time right so doesn't even build oh that's my fault it is because the project reference I think look in this Row for text block yeah that's basically what's well it's it's almost like that it's basically behind the scenes it's going to keep a mapping of like where the text block this particular instance has a has
a row that it can go find why doesn't this work metadata file I got to go look at this it's funny um I removed or I unloaded the project and if I go to the dependencies in this menu it might be hard for you to see sorry but this list doesn't show the pro uh the plug-in project but it's like it's still here in the project file so I never would have been able to find that let's go run it though this should compile now at least okay we are going to make the stitcher Factory that should be very easy we make our controls and then we're going to go ask the stitcher Factory can you make this thing work for us if I hover over the stitcher registrations we have one of them so I'm going to keep stepping into this stuff it will
check the can match call back it says your it's funny your stepping request sure oh it it's still St into it so is parent a grid yes it's true is child a UI element true therefore are both of these true yes so it should pick this one and if we keep going it's going to go make a new instance of this it casted them properly so we have a grid and we have a text block excellent so far so good step over so now we check is it the right type of Stitcher yes it is is so we've come back out we've gotten a Stitcher based on the parent and the child that we want now I go into here this is where the magic happens and it's behind the scenes of the facade right this is an implementation very specific to WPF you can't
tell that at all from the outside so parent we just added a child set the row set the column so far this is looking great very surprising um and that wasn't sarcastic either it's very surprising that things are working the first time so the children count of the grid container one if I hover over and look inside of the children we have a tech sorry this is hard for you to see on the stream but we do have a text Block in there I could write another uh debug assert if we wanted just to prove that it's the same text block hopefully you believe me that if we made a brand new grid container and then we tried to add this text block that it's the same text block then we can check the row and the column and it passes so we have successfully
said in this syntax here we want to go add a text block to this grid container at these rows and columns pretty cool now what was the other example I gave what if we want to use a stack panel okay so this is going to blow up for us stack there we go we go if I go run this now I'm just going to press F5 you'll see no Stitcher found for parent of Stack panel and child text block and it's not wrong like we don't have anything that does that at all so what could we do well I think and I'm not sure and I need to check but I think if we check out a grid if we look at this see how it has this I add child interface on it here's something we could try doing so this is a grid
Stitcher what if as long as we had I add child I don't know what to call this one yet we're going to come back and get this a name when I don't know what to call stuff usually I put three x's and I come back to it just in case you're wondering so what I want to build is support for anything that's not a grid anything that's not a grid but has I add child I want to make sure that we can properly Stitch things what project is this setup uh vs code uh so this is in Visual Studio uh non vs code and this is the Enterprise Edition but you can get Visual Studio community and it will look the exact same as what you're seeing on my screen uh you just don't get the Enterprise features I have from working at Microsoft I
get access to the Enterprise license but personally I only use VSS code when I'm doing any type of web development so if I have to touch JavaScript or typescript which I Do by the way it's not common but I do I will use VSS code for those things but I personally cannot write C outside of visual studio um I feel like my hands are tied if I try to use C and any other um IDE let me know if that answers your question though okay so we have this Stitcher but this part's wrong now because we don't have we can't set a row we don't have a grid we just have something that is I add child and something that has this UI element so what I would like to do cuz this is kind of cool I'm G to ask for a grid Stitcher
so this is almost like a decorator lots of design patterns coming out here today so kind of like a decorator what I'm going to do is say hey look our parent needs a grid it is an I add child the way that we're going to register this thing is that the parent itself is not a grid so we'll check for that and you could even do um just to give you an example uh argument exception throw or we can I guess that's only for null checks if parent is grid parent should not be a um and then we can do actually can actually use the type and I'm going to put this in caps so the parent should not be a type of grid because this particular Stitcher is handling the other scenarios you could even make this combined in the other one and we
could check if it's a grid then don't do anything special if it's not a grid add the Grid in so you could do that too there's lots of different ways that you could do this when we go to stitch this thing together though we need to make a new grid okay and then parent we're going to add the Grid in and then we do this so we're reusing some other grid Stitcher we have to go make a grid to work with and actually this won't work uh so I apologize we can't use this exactly we need to be able to have a call back that can make a grid Stitcher and I'll explain that in a second okay so the reason why we need to do that is because this you'll notice if you look down here it might not be obvious um thanks for
checking in William and yes uh there's going to be other YouTube videos that go out this week so tomorrow and Friday uh in my time so PST I will have two more WPF videos a third WPF video on Monday and then of course this stream so um thanks for checking in and yep you can of course watch the stream later so create Stitcher call back here I spelled it wrong we need to use this because if we look down here why is that not working oh because I I typed it wrong from the very beginning chat you're supposed to be chiming in and saying stop being stupid Nick get your names right okay but let's look at this code because it might not be obvious we create a grid we add the grid to the parent then we ask the stitcher to go combine these
but this Stitcher has no idea about the Grid or the parent so we need to go make it and you can see that here thanks co-pilot we are using grid I'm going to put these on different lines we are using grid is the parent which we just made and child so our parent is getting a grid and then that grid becomes the new parent of this child so it's kind of sandwiching the grid between these two things and then we can say really what's going on here oh I got to cast it that's right so let's do this I spelled this wrong too my goodness this is maybe the hardest word I've ever had to spell okay this part's gross still right but if you recall earlier when we were talking about this call back we were having issues with making it generic and the
reality is the more pattern that we see with this we might be able to find a way to do it nicely so I'm not saying it's impossible but right now it starts to make it a lot trickier in other spots I would rather bury the complexity and doing gross things like this casting I would much rather bury it in these types of things than spread it out across my code base so that's the tradeoff that we're making with the facade right we want to work with a nice API everywhere else and we will take the hit on the grossness behind the scenes so this shouldn't Theory allow us to go do what we want here but something to think about is like here here is a bit of stitching already we're adding a grid into the parent we know that we're still operating WPF to
WPF so we can kind of cheat this way but what we don't get to do is like what kind of parent is this like do we need to talk about the sizing of the grid right so we don't have other parameters we're passing into this thing to afford US you know control as the caller we are saying hey look you gave us something that's not a grid we'll just slap a grid in there for you and make it work that might be Behavior you want so you can go combine anything however you want so you may want that but the tradeoff is that our API only talks about rows and columns for the grid itself or sorry I should say for the item within the grid itself as a result when we go to do this part on line 16161 we don't have any information
from the caller about how you would like to see that done right do you want the grid to be maximized within the parent can we even do that right like can I do grid do uh horizontal alignment like stretch vertical alignment stretch like maybe that's what we want but then we don't know if it's a stack panel and maybe we don't want to do that so my point here is that when you start getting very generic about how you want to Stitch things together in this case so anything that can add a child we're going to go do you're kind of trading the flexibility of literally anything that can take a child we will go slap a grid in there for you and this is this is what you get right you may want to have more specific things where you're like okay so this
is the maybe this is the fallback so we will always hit this Stitcher as a fallback and that's okay but maybe for uh like a layout panel or something else you want to do something or a flow panel you want to do something more specific that talks about the default behavior of how you combine these things so hopefully that part makes sense and maybe I'll leave a little comment here um note we are not giving control to the caller about how the grid gets put into the parent control so this is the tradeoff that you make in this situation I'm going to call this fallback PF GD Stitcher and I spelled this one wrong too oh okay I apologize for anyone watching this who's like man this guy cannot spell um Stitcher is a difficult word today okay so will this work right so where's
our code sorry for scrolling around here we don't have this one registered yet so let's see if co-pilot configured out so far so good very cool um let me go put these on new line so I can see what's up here child sorry that's the parent which is ADD child this part's not going to work though um you can see that it's like it's trying to add ask and this is usually what I would recommend though is like asking the uh dependency container for this thing but we we don't have that right so that kind of sucks but we can go do this kind of thing what's the signature for that so we can go make a local function so I you Stitcher great grid view Stitcher and this will be basically stop why did it put my arrow the wrong way that's ridiculous okay
I'm going to just copy and paste this part can't convert it because why my camera's on the way can't convert it because it's a delegate type what am I doing wrong here what am I doing wrong I can't make a local method here I'm missing something very obvious that's what it is okay um I was trying to make a local method but also declare a function pointer at the same time so that explains that don't do that um if I drop that in here and down here now I can reuse it really almost there that's kind of cool we can collapse that for readability I might just leave it like that we can see things kind of flowing the same way um but yeah now that we have this and notice the order is important so I'm going to let me put this in the
wrong order to start so wrong order if we go run this now grid itself is an I add child so when we go to find the right Stitcher it's going to pick the wrong one apparently not what these assertions don't even work or did it throw an exception oh it did it it threw an exception sorry debug assertion fail so it didn't work it didn't go register the things properly so it's because it picked the wrong one let's go back though because I want to step through this I'm very curious about what actually happened now so we are saying that we want to go use a stack panel and a text block but the first registration is anything that's I ad child so which Stitcher do we get parent is a stack panel and it is I add child oh um I'm showing you the
I'm demoing the wrong uh example I wanted to put it back to a grid container and show you why the order matters if we put the grid container in it will do the wrong thing here my apologies sorry if that was confusing so we've gone back to the grid container but we have both of these registrations in here on our facade so we're going to go ask for the right Stitcher and it says parent is a grid it doesn't have the interface I add child yes it does and child is I UI element yes it is so it's going to go pick this first registration that we had and that first registration was this one it's the fall back one it's not the one we want so the very first thing that happens in here is parent is grid we throw an exception parent grid
should not be a grid maybe in hindsight that was a bad error message but the order matters so we do want to make sure that the fallback one does operate as a fallback which means we're going to jam it at the end just checking my phone here sorry one sec we jam it at the end now this one should go work as expected I'm going to put a break point here because this is the base case that we had you'll see or maybe it's hard to see but in the output we had no exceptions thrown for um for debug asserts failing now if we go change this thing back to a where is it grid container to stack panel I want to step through this one cuz I think it'll be fun we'll put a break point right here this one should go to the
fallback so what Stitcher did we get we got the fallback WPF grid Stitcher as expected we step into it it will go make a new grid it will set the horizontal alignment it will add the grid into the PA which is a stack panel excellent and then it's going to call that call back to go make a grid Stitcher which goes to our local method create grid view Stitcher so we get a new one again if I hover over this grid Stitcher we can see that the child is the text block the parent is the grid that we just made might be hard to see on the stream and then I will go call Stitch with 1 2 3 and four five six for the row and column now this assertion is incorrect because it shouldn't be grid container it should be the stack panel
and the stack panel children if I look at that collection it has one thing inside of it and if I keep going what's a good way to do that oh it's a grid sorry this text is probably extremely hard to see and I realized it's probably being cut off by my uh overlays but if I go look at the grid and I check out its children it also has a count of one and it's our text block so I should say stack panel here what I should have done was this or IUI even just object and then if I put parent here I get to swap that stuff out EAS here this is where I should have done I add child no doesn't like it oh man okay there's no there's no children call on it but anyway the the point was that if we
go inspect the parent I wanted to be able to see if we go see what's up but um if we check the assertions of the row and the column of the text block they are set properly and I know that because inside of here in this output you're not seeing any debug asserts so no we're not visualizing it right now because my main window thing is not working so I apologize maybe what we could have done let's do this this should do it Moment of Truth does this actually work very cool this is a dynamically added text block so what we were able to do was call our stitching logic right sorry this line from 65 through 67 to go combine these things now you even saw what I did here where I took a window and I put the content in you could go
make a Stitcher that does this you can say I want to take a window as the parent and I want to take any child and go put it in there and our Stitcher Factor we could go look up the right registration and say who can handle this for us and we could go write a very basic one that says oh the parent as a window ow it has content and we can take that child and assign it in and if it was some other type say that it wasn't supported in WPF the example I gave earlier was just a string that was HTML we could go say oh someone is taking a window and they're trying to jam HTML onto the window maybe we should man manually add a browser control set the browser controls HTML to being the HTML that was passed in drop
the browser control onto the window and we do that all in the facade behind the scenes now I'm not saying that that is the right thing to do I'm just saying that if you want that type of capability you can literally go build it using this pattern which is pretty pretty cool in my opinion to make this maybe a little bit more obvious about what's going on with the controls what I'm going to do is is talk about this part here I'm going to say grid background oh brushes let's make this cyan this is going to look super ugly so I'm sorry but I want to show you that we have different things going on here so stack panel not stack outlock background chair will go with red the text blocks text block text block there we go yellow we're going to do green and
the window itself is white for the background so what does it look like when we go do this right you don't see the Grid at all interesting right so this is a way that you can debug this stuff just to show you if I go all the way down to our fallback one I made a new grid and I set the background color to cyan and if you look at this ugly mess and I just realized I pick red and green so if you are a red green color blind I am so sorry but um because it probably looks the same the top part where it says this is a dynamically added text block that is green and then there's only red below it there's no and so if you were like heyy that's not what I expect when I combine these things then you're
going to want to play around with your stitchers and make sure that you can get them working the way you want but I think that's mostly it there's still about 15 minutes left on this stream that I plan for at least so um chat's been relatively quiet I don't know if you folks have questions you want answered on WPF other C stuff anything you want to hear about but for me I will be going to build out this kind of stuff I think I'm going to make a nou package for this type of thing and then include as some Basics some like I'll probably make the a couple of packages one is the stitcher part on its own with no implementations and then you can go add them and then I'll add some uh some WPF ones and some interop ones between Wind forms and
WPF I've I've had to remake this type of thing many many times at this point now so I feel like it's probably time to make it a real thing and I can Rel leverage it myself but hopefully that helps this was an example of a few different design patterns so we had a factory that can go make stitchers for us it is also a facade because it's masking a lot of complexity across the registrations we have to go pick the right um Bas it's basically picking a factory to go run right because if we look at these registrations up here like this is basically a factory this is also basically a factory so our big Factory is a facade that picks between other factories and I talked about plugins like technically you could go Define these as little individual plugins so we could use something
like screw to go scan for the right things but would need to package these up nicely so I would maybe make a Stitcher registration scanner right so we could go look for all of these specifically um so someone would have to go Define the instances of these things it's kind of weird we might have to go make like a a Stitcher registration provider and we would scan for those and ask for all of the stitcher registrations off of that I think that's how we'd have to go do it which is a little weird um I'm not this is kind of changing gears a little bit I'm I mentioned I'm using screw door up here to do assembly scanning and it's going to go do the dependency registration stuff all in the same way across all of the types and I probably have to introduce a
couple of weird things to make that feel uh pretty smooth so instead of just registering this like I have here I probably have to go make a provider and then it has a property on it that returns a bunch of different or a single Stitcher registration then I will go ask for all of the providers ask for all of registrations and then just slap them into a big collection that goes onto here then that way this thing itself could be on the DI container then we don't have to go new it up but basically the the goal of what I was basically just saying that is try and pull this stuff in into dependency injection and that way we don't have to new it up then you can use plugins blah blah blah so hopefully that made sense but I think that's it folks um
I don't see any other questions coming in on the chat whether it's from Tik Tok or other places apologies I was not watching LinkedIn this time around but uh I think the chat was enabled for it I didn't see any chat from LinkedIn coming in though so maybe I should have gone back to check so I apologize but um hopefully found this helpful I will be trying to build out a little bit more WPF stuff but um yeah this is this is a pattern I've used extensively that we kind of trade off the ability to be able to preview effectively but we can build up very Dynamic user interfaces this way and this leads to some other testability things that are quite nice so thanks again for watching um still don't have a good schedule for coding live streams but I'm going to try and
see if maybe Tuesday Mornings so this time going forward might be a good fit but we'll see I'll try my best okay thanks folks I will see you next time take care and
Frequently Asked Questions
What is the purpose of the facade pattern in WPF applications?
The facade pattern allows us to create a simplified interface that hides the complexities of the underlying system. In WPF, this means we can combine different UI elements seamlessly without exposing the intricate details of how they interact. It helps in building a cleaner API for developers to work with.
How do I get started with C# and WPF if I'm a beginner?
I recommend checking out my getting started C# course, which provides a solid foundation. It's designed to help you build applications and gain confidence in your skills. Remember, becoming proficient takes time and practice, so be patient with yourself.
What are the benefits of using dependency injection in WPF applications?
Using dependency injection in WPF helps improve testability and maintainability of your code. It allows for easier management of dependencies, making it simpler to swap out implementations for testing or when you need to change functionality without affecting the rest of your application.
These FAQs were generated by AI from the video transcript.