Skip to main content

Posts

Recent posts

C# to JS: LINQ Expressions and ExpressionVisitor

How to translate (some) C# to JavaScriptIf you’ve ever used something like EF Core you may have noticed that methods like LINQ’s Where take an Expression<Func<...>> as the first argument, instead of simply a Func<...>:
Expressions were introduced in .NET 3.5 along with the rest of LINQ and they represent the code tree that a lambda is composed of. For example, if you had a lambda like () => a + b, this is how the resulting expression tree would look like:
.NET 4.0 later introduced ExpressionVisitor, which lets you traverse the expression tree. Take this visitor for example:publicclassConsolePrinterVisitor: ExpressionVisitor {//This is just a helper method to get the Expression, normally you would already have it//so you'd just do ConsolePrinterVisitor.Visit(expression)publicvoidWriteLambda(Expression<Func<object>> expression){this.Visit(expression);}protectedoverride Expression VisitBinary(BinaryExpression node){ Console.Write("("…

ConditionalWeakTable, what does it do?

C#'s ConditionalWeakTable, what does it do?C# has many lesser known features, some more useful than others, and one of them is the ConditionalWeakTable<TKey, TValue> (keep in mind that TKey and TValue must be reference types).
You can think of this type as a dictionary where the keys are weakly referenced, meaning that they won’t count when the GC checks if the object has to be collected. Additionally, when the keys do eventually get collected by the GC, that entry will get removed from the dictionary. This means that you can attach arbitrary objects to any object, allowing you to do something like this:publicstaticclassExtensions{privatestatic ConditionalWeakTable<object,dynamic> Table =newConditionalWeakTable<object,dynamic>();publicstaticdynamicData(thisobject obj){if(!Table.TryGetValue(obj,outvar dyn)) Table.Add(obj, dyn =newExpandoObject());return dyn;}}...var myObject ="hello"; myObject.Data().Foo ="bar"; Assert.AreEqual(myO…

Hangfire: setup and usage

How to setup Hangfire, a job scheduler for .NETInstalling HangfireSetting up HangfireSetting up storageMySQL StorageIn-memory storageRunning jobsFire-and-forget jobsDelayed jobsContinuation jobsRecurring jobsASP.NET Core job integrationConclusionHangfire is a job scheduler for .NET and .NET Core that lets you run jobs in the background. It features various different job types:Fire-and-forget jobs are executed only once, shortly after creation.Delayed jobs are very similar to fire-and-forget jobs but they wait for a specified amount of time before running.Continuation jobs, which run after their parent job has finished.Recurring jobs, perhaps the most interesting of all, run repeatedly on an interval. If you like Unix’s cron you’ll feel right at home, as this kind of job lets you specify the job interval as a cron expression. If you have never used this I recommend crontab.guru, it has a live expression editor, as well as some examples to get you started.Hangfire has two more job type…

Welcome to Pipe's Coding Clues

Hi, I'm Felipe Martínez (aka Pipe) from Spain. I've been coding for 5 or 6 years, my main language is C# but I'm also comfortable in C++, JS, Java and Python. In this blog I hope to tell you about my experiences while coding, as well as give you some tips!