Тёмный

Blazor Code Separation - Easily Moving Code to a Code Behind File 

IAmTimCorey
Подписаться 415 тыс.
Просмотров 18 тыс.
50% 1

When you see demos in Blazor, you often see the code on the same page as the HTML and Razor syntax. But for a production app, you more often want the code to be in a separate file. In this video, I will show you how to easily move the code from the component to a separate file.
Full Training Courses: IAmTimCorey.com
Mailing List: signup.iamtimcorey.com/

Опубликовано:

 

9 апр 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 68   
@rbee4551
@rbee4551 Год назад
Another added benefit of using code behind is that at the moment razor Intellisense is pretty wonky, using code behind restores your access to the much needed Intellisense.
@IAmTimCorey
@IAmTimCorey Год назад
That is a good point.
@satyabratamohapatra3397
@satyabratamohapatra3397 Год назад
Oh my goodness!!. I was doing it manually. Thanks a lot Tim.
@IAmTimCorey
@IAmTimCorey Год назад
I am glad it was helpful.
@LlamaNL
@LlamaNL Год назад
You CANNOT use contstructor injection with Blazor: It will throw this error: Cannot dynamically create an instance of type 'CodeBehindInjectionTest.Pages.FetchData'. Reason: No parameterless constructor defined. If you want to inject in the code behind you do it like this: [Inject] public WeatherForecastService ForecastService { get; set; } = default!;
@IAmTimCorey
@IAmTimCorey Год назад
You are correct. I thought they had fixed that. Thanks for sharing.
@legittaco4440
@legittaco4440 Год назад
Was going to make the same comment
@StateHasChanged
@StateHasChanged Год назад
According to the docs, you can use DI in the code behind using the following syntax... [Inject] NavigationManager NavMgr { get; set; } = default!; Just tried and seems to work.🥳
@Sander-Brilman
@Sander-Brilman Год назад
Great video. This concept of quikly explaining a task/subject in 10 minutes is awesome, i like your 50+ minutes explainer video's aswell but this is a much more accessible time format to quikly learn something useful. keep them comming!
@IAmTimCorey
@IAmTimCorey Год назад
Glad you enjoyed it!
@Tymonello
@Tymonello 6 месяцев назад
Hello Tim, another Tim here and I would like to report a mistake in this video. At 6:20 you say that we can create a constructor in code-behind file and inject the service there, but that's a mistake. We can't use Constructor DI in Blazor pages, because it produces an error: MissingMethodException: Cannot dynamically create an instance of type 'FetchData'. Reason: No parameterless constructor defined. So DI in Blazor can only be done via @inject in the view or with [Inject] attribute over a property in code-behind.
@MiningForPies
@MiningForPies Год назад
Woo just saved me about 7 hours work refactoring. Cheers Tim
@IAmTimCorey
@IAmTimCorey Год назад
Awesome! I'm glad it was helpful.
@nahdinet
@nahdinet Год назад
Hi Tim! Thanks so match for your great tutorial 💯💥
@IAmTimCorey
@IAmTimCorey Год назад
You are welcome.
@imadabab
@imadabab Год назад
Brilliant. Thanks a lot. You remember me with webforms.
@IAmTimCorey
@IAmTimCorey Год назад
You are welcome!
@handypda
@handypda Месяц назад
Always great!
@IAmTimCorey
@IAmTimCorey Месяц назад
Thanks!
@manuelgamezz
@manuelgamezz Год назад
Thanks Tim, I didn't know this feature in blazor, I think is very usefull when you have a big razor component html code + c# code.
@IAmTimCorey
@IAmTimCorey Год назад
You are welcome.
@ANILKHANDEI
@ANILKHANDEI 7 месяцев назад
Thanks needed this!!
@IAmTimCorey
@IAmTimCorey 7 месяцев назад
You are welcome.
@Tielc
@Tielc 10 месяцев назад
Another big advantage is code analyzers like StyleCop don't work on a .razor page, but do in the .cs files. It took [Inject] for me to able to finally separate my code completely, which is awesome.
@IAmTimCorey
@IAmTimCorey 9 месяцев назад
Good point.
@sreenucnu4641
@sreenucnu4641 Год назад
We can create component class for each razor file and inherit from ComponentBase class and use it in razor file right. Is there any advantages in without using ComponentBase class for separating code?
@bootsector13
@bootsector13 Год назад
Hi Tim! Thanks for the great video again. I've been using this feature for almost a year now. We can also create a css file for each razor file (e.g Counter.razor.css).
@zigzagas1000
@zigzagas1000 Год назад
does the css sit in the razor file as well?
@IAmTimCorey
@IAmTimCorey Год назад
Yep, although I don't find myself using that feature very much. Do you?
@bootsector13
@bootsector13 Год назад
@@zigzagas1000 yes just like the cs file. It will only work for the specific razor.
@bootsector13
@bootsector13 Год назад
@@IAmTimCorey I used it for my templated component, so that I can easily copy it to other projects without referencing it :D
@Andrew-ch3bl
@Andrew-ch3bl День назад
This is awesome! Is there any easy way to undo it?
@TYNEPUNK
@TYNEPUNK 4 месяца назад
very useful indeed. is there any html equivalent, I think you just make a razor component and call it like and that way you can call on bits of html/markup without them having to be in the same file. Sorry I am new to all this trying to make it neater :)
@SKIDDOW
@SKIDDOW Год назад
Oh! Thanks! I was creating it manually. Additionally, here we don't need to use ViewModel architecture. Don't we?
@IAmTimCorey
@IAmTimCorey Год назад
What do you mean?
@80amnesia
@80amnesia 6 месяцев назад
thanks, i was doing it manually took me ages
@IAmTimCorey
@IAmTimCorey 6 месяцев назад
You are welcome.
@karliskalviss1381
@karliskalviss1381 Год назад
Awesome vids, Video Idea: Can you talk about the issue with Blazor client DLLs beeing block by Antivirus, and If your customers are Companies with internal security layers most likely default Blazor WASM will not work. This would be nice to know before I started to build My App with Blazor and maybe other beginners would Like to know this aswell.
@IAmTimCorey
@IAmTimCorey Год назад
Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/
@minimalstory
@minimalstory Год назад
How many partial classes can l create in component??
@IAmTimCorey
@IAmTimCorey Год назад
I've never tried. Not sure.
@MumBorne
@MumBorne 4 месяца назад
Good video.. but, injection of the weatherforecastservice from the code behind also should have been covered..
@IAmTimCorey
@IAmTimCorey 4 месяца назад
I ran out of time to cover that in detail as well.
@sodr4c
@sodr4c Год назад
how about the css? of the component?
@IAmTimCorey
@IAmTimCorey Год назад
You can do that, although I don't see it as terribly useful in most cases.
@trevordennis
@trevordennis Год назад
Been wishing for this feature. Turns out my ReSharper settings was set to hide the VS quick actions... :( Had an issue though. I tried this on a couple of my razor files and started getting errors in the code behind saying there is no suitable method to override for OnInitalized, and it doesn't see anything else from the parent classes I was inheriting from. I have also seen this when manually moving code to code behind so it's probably a VS bug (17.5.3). After checking my Git changes, I found out that VS was adding an ItemGroup with Compile statements for the code behind files to my other project in VS that I use for the web server side and not the Blazor WebAssembly project that I was actually editing in. Removing the ItemGroup seemed to fix things up. Just in case anyone else runs into this :^)
@IAmTimCorey
@IAmTimCorey Год назад
Thanks for sharing.
@louiseeggleton7420
@louiseeggleton7420 5 месяцев назад
I like this idea, but what about code that needs to be on more than one page? Is there a way to have a common "code behind" page that can be shred by multiple pages?
@IAmTimCorey
@IAmTimCorey 5 месяцев назад
You are describing a class.
@louiseeggleton7420
@louiseeggleton7420 5 месяцев назад
@@IAmTimCorey I know what a class is, but I guess what you are saying is that with Blazor I am free to use any of the separation of code techniques and software patterns have been using with other types of projects. I'm very experienced with c#, but new to Blazor and still considering it.
@IAmTimCorey
@IAmTimCorey 5 месяцев назад
Yep. You can use your full range of C# code tools.
@TahmidRadit
@TahmidRadit Год назад
Is it wrong to call web API in Blazor Server or is it wrong using controllers to use web API in Blazor Server?
@IAmTimCorey
@IAmTimCorey Год назад
Blazor can definitely call an API. That’s a good thing. I don’t recommend mixing an API in with your Blazor project, though. Keep them separate.
@TahmidRadit
@TahmidRadit Год назад
@@IAmTimCorey Web API in separate project and Blazor Server in separate project I think.
@marna_li
@marna_li Год назад
I just love how C# has gotten so much less verbose with implicit usings and file-scoped namespaces. File-scoped namespaces should really be the default! I hope that in the coming years we will just refer to them as simply namespace declarations. Though I'm a bit disappointed that som tooling, like VS for Mac, still defaults to block-scoped namespaces for new class files.
@IAmTimCorey
@IAmTimCorey Год назад
Yeah, the templates need to be updated to modern standards.
@jankool01
@jankool01 Год назад
Can add DI to code behind [Inject] NavigationManager NavigationManager { get; set; } = null!;
@jankool01
@jankool01 Год назад
just seen previous comments on same issue
@XXnickles
@XXnickles Год назад
Honestly, I have never liked this approach of dividing the markup and the code. Having your markup and behavior code in the same file give you that sense of component (unit) which at least for me is a more natural path for the web. Also, there is a hidden advantage: you can easily see when your component is getting out of control (in terms of complexity) I guess it narrows to habits, traditionally c# dev separates this thing (even in different folders, which for me is even more painful) and then rely on tooling to navigate around
@IAmTimCorey
@IAmTimCorey Год назад
It definitely depends on the situation, but having the code and the markup in the same file does make it easier to see in most cases. However, having two files does allow you to open them separately and have them side-by-side.
@jakealexandermurray9977
@jakealexandermurray9977 Год назад
But the razor editor is so buggy with c# code in the mix. Custom code snippets isn't even working inside the @code block. So as of now, the "code-behind" approach is still a must until they make the razor editor great.
@petertoshliltoshmakafui9886
Please how to create admin panel and link my website to my admin panel. ? There’s no answer to it on internet…
@IAmTimCorey
@IAmTimCorey Год назад
Do you mean like a cPanel or something similar? Those come from your web host.
@lgmsampaio
@lgmsampaio 3 месяца назад
Worth nothing that the "Creating constructor in the code behind file" actually doesn't work. Would be better to test before making a video about it.
@IAmTimCorey
@IAmTimCorey 3 месяца назад
That’s coming. What we have right now is property injection. If I said constructor injection, I misspoke.
@MarioStaats
@MarioStaats Год назад
Hi, just a little question. Should i use the dependencyinjection over the constructor, or using the [Inject] Attribute in the Codebehind? whats your thoughts? @iamtimcorey
@IAmTimCorey
@IAmTimCorey Год назад
Constructor injection isn't working yet (my mistake). So, you either use the Inject attribute on a property or you inject it onto the Blazor page.
@StateHasChanged
@StateHasChanged Год назад
According to the docs, you can use DI in the code behind using the following syntax... [Inject] NavigationManager NavMgr { get; set; } = default!;
Далее
Correct String Initialization in C#
8:40
Просмотров 31 тыс.
Blazor RenderFragment
23:11
Просмотров 1,2 тыс.
Liskov: The Liskov Substitution Principle
4:23
Просмотров 19 тыс.
The Blazor Competitor is Here!
15:08
Просмотров 63 тыс.
Blazor Localization in .NET 8 & Blazor Server
13:58
Просмотров 6 тыс.
8 Free and Open Source Blazor UI Libraries
9:42
Просмотров 35 тыс.
3 Ways Every Developer Fails And How To Avoid Them
26:16