BrandGhost

Accessing Record Types on Earlier .NET Versions

In C# 9.0 we received access to a great quality of life type called the record. Record types allowed us as dotnet programmers to skip a lot of boiler plate code, thereby saving us time and making code more readable. Wins all around! In this video, I walk through a real application of how I am using the proposed solution to clean up my code to switch to record types. Oh, and did I mention I am stuck on older .NET framework versions before record types were available? Original Article: https://w...
View Transcript
Okay. So on my screen right now, we can see that I have a class, right? It's filling up the entire screen. Uh this is a what's called a data transfer object, a DTO. And the purpose of a data transfer object or a DTO is really just to kind of contain some state that we can pass around. And as the name suggests, we're going to be transferring data uh by using this object. So historically uh in C before we've had access to records when we're using a class uh in this form here um you kind of have a lot of duplicate information as a programmer to kind of put this together for something that should be super simple. So what I mean is that if we take this example here for this first um argument that's passed in, we can see that it's being assigned to a property and then we can see the property definition. So what I'm working in right now is one of the the prototyping projects that I have for a game that I'm working on. It's a bit of a dumping ground for different things. And I have a bunch of these uh DTO's I'm putting together to transform uh sort of uh across different domains of information. So a lot of the time these DTOs end up looking similar in the different domains but I might be switching between this say like I identifier type um and maybe actually translating that into a string in a different domain. But a lot of the, you know, the types of uh information I'm trying to transfer around are similar. So I end up having a bunch of these DTO's and uh maintaining them is kind of nasty because as I said, you have information really across three spots. So you can imagine if I wanted to even change the name of one of these, right? Um I kind of have three different lines of code I have to go touch to update. it's just it's messy to have to maintain. So the solution that we have access to in uh more recent .NET versions is the record type. And you can see if I type record here, I don't have access to that. And if you check it out in the top here, I'm on .NET 4.6. And the reason I'm on .NET 4.6 is I need compatibility for Unity. So my libraries are going to be um built and then leveraged in unity later and to the best of my knowledge currently we don't have some of the luxuries of uh lateret languages. So um we have a solution though and there's actually a a nougat package that we can leverage and it really helps us here. So I'm going to show you how that works and how um this goes from being super messy to something that's a little bit more uh easy on the eyes. So, what I'm going to do is jump over to um my project that I have here. Um font is going to be a little small, so I apologize, but I'm just going to go to manage nougat packages. And the nougat package that we're going to be looking at is called Polysharp. Cool. So, I'm just going to install that. It's doing its thing. says it's installed, but we'll get that going. Okay, so we're not quite done. We have the package installed. You can see by the little check mark here. I'm just going to go ahead and close that. And it still doesn't work. That's that's okay because we're not done. We have one more step. We're going to jump over to the project file. Right. So the same project file and we're going to change the language version. Uh so lang version. I actually have this on my clipboard I think. So I'll paste that in to 11.0 for example. And if I jump over here you'll see that all of the squiggly lines go away. So what Polysharp is able to do for us is um actually create some of these classes for us that the compiler is able to use because some of the features that we're looking at like a record type are things that uh can be determined at compile time. So if you check out the um the actual site for the package, I believe they have a really good rundown on how this works, what types of features are available. So definitely check out um the GitHub page for that. It'll be super useful. But now that we have record here, what do we do? Well, just to kind of make it obvious, I'm going to delete all that code. Um so you can see it's already significantly smaller in size. And what I'm just going to do is move some of this up. And I can't remember what I had on the clipboard there. Uh, too much. I need a parenthesy. There we go. Cool. Um, so that is infinitely smaller, more succinct. And just to make it actually happy, I'm going to uh actually change the casing here because the way records work is that these arguments being passed in now um like are actually going to be the properties that get defined on this. So all of a sudden that one uh DTO that was taking up my entire screen with information really duplicated three times is now reduced to just um having those pieces of information in one spot. So infinitely cleaner and we can do that because we have access to the record type which is all thanks to Polysharp. So a really simple change to be able to do. Um, I'm gonna try building this. I probably should have given this a whirl before I started recording. Um, but we check the output. Oh, it's already done. Cool. Um, yeah, one succeeded. Zero failed. So, it's able to build this no problem. It's still net 4.6 and we have access to the record type. So, I think the catch will be that you have to have the right tools installed for the compiler locally, but that shouldn't really be an issue. um the challenge is usually more about the target that you're trying to build for. So, uh with PolySharp, we are able to kind of overcome that. So, that's a that's really it. And it's a pretty quick solution for um this type of challenge that I keep running into. There's a handful of other stuff that you can use Polysharp for that. I mean, it's unlocking other features like this. uh primarily I'm just leaning on it for the record type right now and it's uh it's cleaning up a ton of code for me. So I'm super excited about that. So just a quick video for today. So if you enjoyed that uh please give the video a thumbs up, subscribe to the channel if you want to see more of this kind of stuff and uh definitely for the YouTube algorithm uh get some comments in there and I'm happy to get back to you. So, thanks again and we'll see you next

Frequently Asked Questions

What is a Data Transfer Object (DTO) and why is it used?

A Data Transfer Object, or DTO, is used to contain some state that we can pass around in our applications. It helps in transferring data between different parts of a system, especially when the data structure is similar across different domains.

How can I use record types in earlier versions of .NET like 4.6?

You can use record types in earlier versions of .NET by leveraging a NuGet package called Polysharp. After installing the package, you'll also need to change the language version in your project file to enable the use of record types.

What are the benefits of using Polysharp for record types?

Using Polysharp allows me to clean up my code significantly by reducing duplication in my DTOs. It enables me to use record types even in .NET 4.6, which makes my code more succinct and easier to maintain.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload