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 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.

Advice for future Web Designers

#1: Web Designers don’t need HTML/CSS knowledge

They do. Period. Not an advanced one, but at least the basics… What is the difference between a designer and a web designer that doesn’t know the basics of a web page? None.

Far too many times, I had to implement a design that is outright impossible to make, or hard to change, or is totally inefficient performance-wise. Imagine if an an architect presented a concept with a floating building… Fortunately for most engineers, most people are familiar with basic laws of physics, so the aforementioned architect will be put in a straitjacket right away.

#2: Learn the basics

You want to be a web designer, not just designer… I’m sorry, but you’ll need to specialize a bit more. I can throw a few concepts in Photoshop, Illustrator, or XD, but that does not make me a designer. Basic knowledge of HTML/CSS will help you to understand the limitations. If your team uses some CSS framework, check it out. Try to use most of its templates, don’t make the developers override an entire thing.

#3: Think, dammit

Some designs I saw so far… You don’t even need to know anything about web or design to figure out that they’re wrong. Pop-up dialogues within pop-up dialogues, disregard for different screen sizes / logical responsive behavior… I could continue with even more bizarre examples, but these two are the most frequent… and something I simply can’t understand…

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.


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:

and download 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 /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