How to write Fluent Api in C#

For the first serious post, I’ve decided to make this tutorial. Fluent api stuff isn’t very complicated, but when I was searching on how to write one I’ve failed to find detailed explanation on how to do it. Plus, I’ve run into tons of bad examples…

A side note before beggining. Making fluent api isn’t complicated, but it can get messy quickly. So plan ahead. For that reason, I’m about to make two of them. Barebones introductory one, and then something more complex.

Fluent Api, or Fluent Interface Pattern is a way to neatly chain functions in your code. The best example is https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/. Onward to making our own.
Suppose we have the following class:

Now we wanna make a secretary object out of it. We also want a young secretary, between 22 and 28 years of age, with some sexy name of our choosing (talk about objectifying women). Regular way of doing this would be:

Instead the aim is to make it like this:

First, we need to make WomanScaffolder class. Duh…

Notice the keyword sealed. It’s a common and good practice. We don’t have any reason to inherit this class anyway.
Since this scaffolder class eventually returns object of type Woman, we need to keep that object within the class, so we can modify it. One more thing, if you check out the picture above, you’ll notice that there’s no new keyword in front of WomanScaffolder. We are gonna keep the constructor private, and access this class in a different way.

To make chaining our functions possible, we must put those functions into interfaces. WomanScaffolder class will then implement them. These interfaces also control the order in which you can chain them. For now, the most simple version is this: you make the First interface, which has a function that returns Second interface, which has a function that returns Third interface, and so on. Planning this is the hardest part..

In the code above, you’ll see that I have three methods. Create(), Name() and Age(). Ignore Create() for the moment. I want to focus on interfaces first. For simplicity I’m gonna call them IFirstFunctions and ISecondFunctions for order clarity.

Now the implementation:

Now, we’re missing a way to access IFirstFunctions. This is done through that Create() function. It’s a static method that will call WomanScaffolder class constructor, and return IFirstFunctions, like this:

That’s it. It is fully functional. However, I do not like the layout. This Create function would make more sense at the end of the chain. Something to indicate ending. We also shouldn’t be limited by order. Maybe you don’t want to specify age, just the name. Also, maybe the name of the secretary would be some variable, not hard coded string, and we wanna have validation for that. So, I’ll modify the Create() function, and WomanScaffolder constructor like this:

This is the new entry point now. I’ll also modify the interfaces that we had, and add another one called ICheckout.

The Create() method now goes at the end of the chain, and it can be called anytime. This is why ICheckout is implemented by every other interface. Finally, our WomanScaffolder class looks like this:

The implemented validation is kinda crappy… Oh well, fits nicely on printscreen. Go ahead, type: var secretary = WomanScaffolder.Name(“Nia”) into your Main() and play with it.

Fun with delegates

Fun with delegates

Can a method be passed as a parameter to another method in C#? Sure… Why not…

Part One (basics):
The official definition of delegate is: A delegate is pointer to a function. Nice and simple description. You can view it as a blueprint for a function that you intend to pass. The return type, and parameters of a function and delegate which points to that function, must be the same. Sounds a wee bit confusing. Take a look at the picture bellow:

We have a delegate called TheDelegate that returns nothing and takes no parameters. Then we have TargetFunction() that takes our delegate as a parameter. Well, it can take any method that has the same declaration as TheDelegate. Finally, we made a ParameterFunction() that has required identical declaration as our delegate, and we passed it as a parameter. When you run this, “Yellow World” will be printed out.
Check the picture bellow. I’ll be using this from now on to keep he code cleaner:

Now let’s make a few modifications. Suppose we don’t want to hardcode “Yellow World”, but we want to print an incoming string:

So, the delegate declaration is changed to accept a string parameter. TargetFunction must take method, and parameter for it separetly. Such is the syntax. You can also pass an anonymous function on the spot. For those unfamiliar with what an anonymous function is:

So we can even avoid declaring ParameterFunction, and write everything like this:

Delegates can return values as well:

This will print “Hello” plus whatever the user entered.

.Net already has two types of delegates built in, so you don’t have to declare one yourself. One is called Action<T> the other is Func<T, TResult>, where T is parameter and TResult is return type. Action is used for delegates with void return type, while Func returns something (TResult).
Here is the basic setup:

If you’re confused by this Test3() method, I’m getting to it.

Part Two (method fields)
When we create a class, we usually make something like this:

Imagine if we could make this CheckAgeConstraint… uhm… gettable and settable, just like any field in a class, so you can change what is acceptable on the fly. I’m not saying that this is a good idea, I’m just saying that it is possible.
So, we are making software for human trafficking organization ( work for those who pay the most), and we need to find a way to check and store humans that are suitable for child labour. Between 6 and 14 years old. Lets modify the class:

Now to use it:

So there… You can make… delegated properties so to speak, that can change object’s method behaviour. All of this is also applicable to both Action<T> or pure delegate. How are you gonna use it, is up to you.

Links:
Func
Action

Vivaldi video stream issue on GNU/Linux

Vivaldi ( and Opera ) won’t play online videos out of the box on Gnu/Linux. This is something that bugged me a few months back. Fortunately, the solution should be simple. Go to this link: https://github.com/iteufel/nwjs-ffmpeg-prebuilt/releases?after=0.32.1

and download ffmpeg.so file. I recommend trying version 0.30.5 first. Some of the newer ones didn’t work for me. Download and unpack it, then copy it to /usr/lib64/vivaldi under root privileges.

sudo cp libffmpeg.so /usr/lib64/vivaldi

Reopen Vivaldi, and problem should be fixed. The same applies to Opera, except that you should copy the file to /usr/lib65/opera.

Hope this helps