If block TagHelper

Ever wonder if there’s another way to rewrite if block statement in razor?

Adding a TagHelper can help you transform the if block statement into this:

[HtmlTargetElement("*", Attributes = "[asp-if]")]
public class IfTagHelper : TagHelper
{
    /// <summary>
    /// Shows the element when this condition is true
    /// </summary>
    [HtmlAttributeName("asp-if")]
    public bool IsTrue { get; set; } = true;

    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        if (!IsTrue)
        {
            output.TagName = null;
            output.Content.Clear();
        }
    }
}

Simply replace asp-if to any attribute name you want. Gist: https://gist.github.com/bingzer/78852e67ec4320a0b9d9381b109230fa

Lazy load JavaScript?

TLDR: Yes, you can. Here: https://github.com/bingzer/js-inject/blob/main/index.js

Consider the following use-case:

Fetch and execute a JavaScript library on certain page only after a certain user’s action. One good example of this is to load CKEditor after a user’s intent to write a comment in a specific page.

This can be done by injecting <script> on the page after the intended user’s action. One thing to consider is to not re-inject if CKEditor is already loaded.

So I came up with a simple code snippet: js-inject

js-inject

js-inject contains only one function injectScript(). This function will inject <script src="..."> into document only if existsFunctions() returns a falsy

In this scenario, injectScript() will inject the CKEditor script only if window.CKEditor is not already loaded.

js-inject is a code snippet (and no, it’s not an npm package) that you can copy + paste to inject <script> after DOMContentLoaded event.

Grab it here: https://github.com/bingzer/js-inject/blob/main/index.js

Rescheduling Hangfire Jobs using appSettings.json

Hangfire is a powerful tool to schedule a background job. With its simple-to-use API, one can easily register a background job using a couple of lines of code.

 RecurringJob.AddOrUpdate<DirectoryCleaner>("DirectoryCleaner", job => job.Cleanup(), Cron.Daily);
 RecurringJob.AddOrUpdate<CacheCleaner>("CacheCleaner", job => job.CleanCache(), Cron.Daily);

This code tells Hangfire to create two background jobs to run on everyday at midnight: “DirectoryCleaner” and “CacheCleaner”.

However, there are some drawbacks with this approach. If there’s a need to disable or to modify the occurrence of these jobs, you have 2 options:

  1. Remove the job from the code, recompile and redeploy.
  2. Using Hangfire Dashboard and delete the job.
    However, the job may get added back in when the app pool recycle.

Hangfire.Rescheduler

So, I created Hangfire.Rescheduler to allow more flexibility around job scheduling. Consider the following approach:

appSettings.json

{
  "jobOptions": {
    "schedules": [{
        "jobId": "CacheCleaner",
        "isEnabled": false,
        "cron": "0 1 * * *"
      },{
        "jobId": "DirectoryCleaner",
        "isEnabled": true,
        "cron": "0 1 * * *",
        "timezoneId": "UTC"
      },
    ]
  }
}

Program.cs

...
RecurringJob.AddOrUpdate<DirectoryCleaner>("DirectoryCleaner", job => job.Cleanup(), Cron.Daily);
RecurringJob.AddOrUpdate<CacheCleaner>("CacheCleaner", job => job.CleanCache(), Cron.Daily);

IServiceProvider services = ...
IConfigurationRoot root = ...

var options = root.getConfiguration<JobReschedulingOption>("JobOptions");
var jobRescheduler = services.GetService<IJobRescheduler>();
jobRescheduler.LoadSchedules(options);
...

IJobRescheduler allows you to modify your Hangfire jobs using appSettings.json (or other ways using JobReschedulingOption) . It provides a way to turn on/off, reschedule (using Cron expression) a job by its id.

This library is really simple to implement check out the code on github: https://github.com/bingzer/Hangfire.Rescheduler

A WordPress.com Website.

Up ↑