Posted on

Introduction To Ettissal Library

Ettissal is a nuget package that provides a component for Blazor WASM to check application connectivity to internet.

Installation

Install the nuget package in your Blazor WASM project:

dotnet add package Ettissal

Or

NuGet\Install-Package Ettissal

How to use?

You have a Blazor WASM project and you want to check the application connectivity to internet. You can use the Ettissal component to do that.

Add Ettissal Library

Add the following line to the service collection in Program.cs file:

builder.Services.AddEttissal();

Use Ettissal ConnectedComponent

Using this component, you can check the application connectivity to internet. You can use it in any page or component in your Blazor WASM project.

<ConnectedComponent>
    <Online>
        <p>You're Online</p>
    </Online>
    <Offline>
        <p>You're Offline</p>
    </Offline>
</ConnectedComponent>

This will help you to show the content based on the application connectivity to internet. As you can see, you can use the Online and Offline components to show the content when the application is online or offline.

Result

I hope this can help 🙂

Here the link to the library source code : https://github.com/mabroukmahdhi/Ettissal

Posted on

Display a Markdown file content in Balzor

You have for example many github repositories with well-done documentation and you want to use those files (*.md) in your own Blazor application? This is very simple. You only need to follow this article.

Introduction

To convert a DM file (a file with the .dm extension) to HTML in Blazor, you’ll need to use a library or tool that is capable of parsing the DM file and generating the corresponding HTML output.

One option is to use the Discord Markdown Parser library, which is a Python library that can parse DM files and generate HTML output. You can use this library by calling its API from your Blazor code using HttpClient.

Easy peasy…

If you want to simply display the content of your file, you can install and use the library Markdig.

Installation

You can install the package like this:

  • Nuget: NuGet\Install-Package Markdig
  • .NET CLI: dotnet add package Markdig

How to use?

Here’s an example of how you can use the Markdig library to convert a DM file to HTML in Blazor:

@page "/markdown"
@inject HttpClient Http

@MarkdownHtml

@code {
   
    private MarkupString MarkdownHtml { get; set; }

    protected async override Task OnInitializedAsync()
    {
        string markdown = await Http.GetStringAsync("MarkdownFile.md");
        MarkdownHtml = new MarkupString(Markdig.Markdown.ToHtml(markdown));
    }
}

For your info, I placed the file MarkdownFile.md in wwwroot in my Blazor WASM sample app. Here is the content of this file:

# My title
## My subtitle
 I am writing something here.

## Second subtitle
**Wow** this is _amazing_.

Oh I have a [link](https://mahdhi.com/) too!

And if you run your application, the result will be:

Displaying Markdown file content in Blazor

Conclusion

Keep in mind that this is just one way to convert a DM file to HTML in Blazor. There may be other libraries or tools that you can use for this purpose. If you know more about this subject ? Please share 😀

Posted on 2,983 Comments

Write fluent guard in your C# methods

Intoduction

What are guard clauses ? Guard clauses are true/false expressions (predicates) found at the top of a method or function that determine whether the function should continue to run. Guard clauses test for preconditions and either immediately return from the method or throw an exception, preventing the remaining body of code from executing.

Example

A common example is to check if an argument is null and throw an ArgumentNullException. However, one may be performing other forms of validation on arguments such as if dates fall within a certain date range, integer values are positive, strings are a certain length, etc. Any assumptions about the arguments or state of the object that are being made by the remaining block of code are typically made explicit by the guard clauses.

void Add(User user)
{
    // Guard clause  
    if (user == null)
        throw new ArgumentNullException(nameof(user));

    // rest of the code...
}

What is Fluent Guard?

Fluent Guard is a simple library that helps you to use Microsoft.Toolkit.Diagnostics.Guard extensions with more fluency.

Logo

logo of fluent guard library

The idea is not new, I tried to copy some code and personalize some code from the project FluentAssertions.

Installation

You can download and install the package manually from Nuget org:

Nuget

Or run this command on the package manager of your Visual studio:

Install-Package Mahdhi.GuardFluently.Core

Usage

Let’s say we have a service SomeService that defince the following method:

 public void DoSomeCall(string name, object length)
{ 
  // do something
}

Here you will sure need to make some checks for your parameters name and length.

Let’s say :

  • the name shouldn’t be null and should have at least 10 chars.
  • the length should be assignable to the type int

In this case your check can look like this,

public void DoSomeCall(string name, object length)
{
  //guard
  name.Should()
      .NotBeNullOrWhiteSpace()
      .And
      .HaveLengthGreaterThan(10);

  length.Should().BeAssignableTo(typeof(int));

 // do something
}

In this example there are three exceptions that can be thrown:

  • System.ArgumentNullException: Thrown if name is null.
  • System.ArgumentException: Thrown if name is empty or whitespace.
  • System.ArgumentException: Thrown if length is not assignable to the type int.

If you call this method as following:

SomeService someService = new();
try
{
    someService.DoSomeCall("A", 7);
    Console.WriteLine("No exception...");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

Here the name ‘A’ is shorter than 10 letters, in this case an exception will be thrown:

But with this example, where name and length are valid:

try
{
    someService.DoSomeCall("My size is over that 10", 7);
    Console.WriteLine("No exception...");
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

In this case no exception will be thrown.

I hope this blog could give you some new infos 🙂

References

David Hayden – Guard Clauses in Computer Science