By default Blazor component file consists of HTML code and C# code under the single file. For better coding standards we can split HTML code and C# code to separate files using concepts like:
- Partial Class
- ComponentBase Class
Create Sample Blazor Server Application:
Blazor Single Page Application comes with two types of templates like (1) Blazor Server,
(2) Blazor Web Assembly. Let's create a Blazor Server templated sample application to understand to split the *.razor(Blazor file extension) files.
Using Visual Studio 2019 using a rich user interface we can create Blazor Templated application very easily. But for this sample, I'm using .Net Core CLI(Command Line Interface) with Visual Studio Code IDE. Click to know how to use Visual Studio Code Editor for .Net Core applications.
.Net Core Blazor Server CLI Command:- dotnet new BlazorServer -n Your_Application_NameBlazor project preview:-
Open a command prompt whose path needs to be pointed to the Startup.cs file in our project(which application folder root path), then run the below .NET CLI command to start Blazor application.
CLI run Command:- dotnet watch runApplication preview:-
Let's examine the Counter Component Code default sample Blazor component.
Pages/Counter.razor:
@page "/counter" <h1>Counter</h1> <p>Current count: @currentCount</p> <button class="btn btn-primary" @onclick="IncrementCount">Click me</button> @code { private int currentCount = 0; private void IncrementCount() { currentCount++; } }
- #L3 - #L7: These lines contain HTML code with model binding.
- #L9 - #L16: These lines contain C# code.
- By default Blazor component single file is a combination of HTML and C# code.
Why Split *.razor File?:
- Code separation is like a fundamental rule in programming.
- Separating the Html Code and C# makes the development of Blazor Component easy and maintainable.
- Makes C# Code Testable.
Partial Class Implementation:
Partial Class is a feature of implementing a single class into multiple files. So now we will maintain the Html code in Counter.razor file and C# code in Counter.razor.cs file. Counter.razor.cs file acts as a code-behind file for Counter.razor file. While writing class in Counter.razor.cs file we explicitly mention a partial keyword, but Counter.razor Blazor file with Html Content, but .Net Compiler is good enough to read Counter.razor as a partial class, so on code compilation single Counter.cs file will be generated.
Let's create a Counter.razor.cs file and inject all c# code into it as below.
Pages/Counter.razor.cs:
namespace SplitFileSample.Pages { public partial class Counter { private int currentCount = 0; private void IncrementCount() { currentCount++; } } }#L3: class declared with a partial keyword.
#L5 - #L10: c# code copied from Counter.razor file.
By chance, if we miss declaring partial keyword in class .Net compiler intelligent enough to notify us by failing the project compilation as below.
Visual Studio Code editor good enough to track the reference of Counter.razor.cs file members on Counter.razor file.
Finally Counter.razor file contains only HTML or Razor Code.
Pages/Counter.razor:
@page "/counter" <h1>Counter</h1> <p>Current count: @currentCount</p> <button class="btn btn-primary" @onclick="IncrementCount">Click me</button>That's all we have successfully used partial class implementation for code separation.
ComponentBase Class Implementation:
ComponentBase is a class that hooked up with all component life cycles of a Blazor Component. ComponentBase class is derived from the library Microsoft.AspNetCore.Components. This approach works with the class inheritance technique.
In this approach Counter.razor.cs file declares class like CounterBase.cs which inherits ComponentBase class. Finally Counter.razor file inherits CounterBase.cs class.
Let's implement the inheritance of ComponentBase class to CounterBase.cs file as below.
Pages/Counter.razor.cs:
using Microsoft.AspNetCore.Components; namespace SplitFileSample.Pages { public class CounterBase : ComponentBase { protected int currentCount = 0; protected void IncrementCount() { currentCount++; } } }
- #L1: To inherit ComponentBase class added the reference of the library name Microsoft.AspNetCore.Components
- CounterBase class can override the life cycle methods that are inherited from ComponentBase class.
- #L7, #L9: At these lines, we have used protected access specifiers, because of Counter.razor(inheritance in the upcoming steps) file inherits CounterBase. So the methods or properties which need to expose in Counter.razor file then their minimum access specifier should be protected from this we can draw a conclusion like we can't use private access specifier.
Finally, inherit CounterBase class into Counter.razor file as below.
Pages/Counter.razor:
@inherits CounterBase <h1>Counter</h1> <p>Current count: @currentCount</p> <button class="btn btn-primary" @onclick="IncrementCount">Click me</button>At #L1 inherited CounterBase class.
That's it we completed ComponentBase class implementation as well.
Wrapping Up:
Hopefully, I think this article delivered some useful information about Partial Class and CounterBase class in Blazor Components. I love to have your feedback, suggestions, and better techniques in the comment section below.
When to use one or the other? Any advantages or disadvantages?
ReplyDeleteHi Moroni,
DeleteThey are like 2 techniques blazor providing both are at equal level, no advantage over other. My prefered approach was Partial Class because it more work like code behind file approach in .net technologies.
Hi Naveen,
ReplyDeleteOne advantage of the base class approach vs partial class is you can have multiple layers of inheritance (a sequence of base classes before you get to the last in the chain eg
ComponentBase
B1 : ComponentBase
B2 : B1
B3 : B2
Component : B3
if for example you have things common you want to share across multiple components in a library. For simple cases without the above need, I would also prefer the partial approach. Just worth bearing in mind as this is one scenario I can think of where it pays to go one way over the other.
Thanks for the article. I've started implementing both solutions. I have a standard base class that all pages can implement with common functions, as well as all authentication variables. And I use a partial class for all page specific functions and variables.
ReplyDeleteThe reason to separate the codes that have the most weight is:
ReplyDelete"Makes the C # code testable", great.