hello everybody i'm nick and today i'm
going to show you how you can create
your own fluent apis
in c sharp now when i say fluent apis i
don't
actually mean web apis and endpoints and
stuff like that
but rather the actual c sharp to
represent itself
as a fluent api chain method approach
and it's actually a very useful
technique that many many libraries do
around the new ecosystem and then the
dotnet ecosystem
because it makes a lot of sense in the
context of a library
you effectively are guiding your users
in a very specific path by providing a
fluent api because i don't have to know
exactly
how your library works and how it is
wired up you just provide the entry
point for me and then you
guide me into that experience so in this
video i'm going to show you how you can
do that and the easiest way to describe
what i'm going to be building is to show
you this so here's an example of the
fluent docker
library which allows you to use a
builder and then from that builder you
have a fluent api
to create your own container build it
and start it so you can see that it
reads like
english it's new builder use container
use this image and expose this specific
port
with those environment variables and
wait for port
this on tcp for 30 seconds and then
build that and start it we're gonna
do the same thing and i'm gonna show you
two ways the simple way
and kind of stupid way depending on what
you're gonna do and also
more handhold hand-holdy and more guided
way which i personally prefer
it might look a bit tricky in the
beginning but it is actually pretty
straightforward and pretty
powerful if you like a type of content
and you want to see more make sure you
subscribe make the certification bell
to get a lighted when i upload a new
video so let me just delete this because
we don't really
need this and let me just show you the
simplest way of
using the fluent api approach
and the simplest one is actually at the
builder pattern so i'm gonna create a
new user builder
here and you could say something like
with
uh login and i'm building here the
github user
model with name nick chaps and then a
bunch of other stuff as well
and at the end you build it and what you
end up with
is a user model so if i show you here
this would be user now this is a very
naive or very primitive implementation
it doesn't really
tie to what i'm going to be showing you
today what i'm going to be showing you
is more something like this where you
have the
connection and this will be i used to
build um
a sql connection fluent way and this
will be something like simple
fluent sequel connection dot for server
and the server will be localhost
and database and the database will be
mydb as
user neck and password
password and now at the end of all this
all you do is connect and let's see how
we bring this together so in this class
here
i have a bunch of private fields
effectively
and then on each step i'm setting that
field value based on what i got from the
method
and then returning this single instance
of the uh the builder
or the fluent uh class and then at the
exit method i actually return the thing
i'm
intending to build which is a connection
which i
open and return now this is a very naive
approach
in my opinion when it comes down to
fluent apis depending on what you're
doing
but mainly when something is not
actually optional for example user and
password isn't optional or database
isn't optional
but i could remove it and the thing
would still work and we don't really
want that most of the time we want to
guide someone in a specific way and
let's say we want to tell them
to select the server and then select the
database and select the user and select
the password or maybe here instead of
user select a token so you have a
fork and you can make a decision there
as well and then go to different
branches
so what i'm going to show you now is how
you can create this staged
or layered fluent api experience for
your users
it's gonna look something like this i'm
gonna name this connection
one and i'm gonna go here and create a
new class
called fluent sql connection and we're
gonna do the proper
way here and the proper way actually
shares quite a lot of stuff with the not
proper way which is
the idea of setting fields behind the
scenes now instead of
actually allowing the user to
instantiate the class i prefer to make
the constructor private so i'll say a
private fluent sql connection here
and if i do that you can't just go
anywhere
and do a new of this class i can't do
new fluent simple connection because
it's private
so instead i'm going to follow the model
where you have a static way to create an
instance of this connection a builder
and i'm going to go here i'm going to
say public static
and this for now it's going to return a
fluent sql connection but
this will change but it will look
something like this where it's like
create connection method then return
new affluent connection here and then we
still need a way to actually set those
values and they're going to look very
much the same as they look
here but they are gonna differ in a very
very specific way in order to achieve
this layered approach you or at least
the way i like to do it is like this and
these interfaces can actually go in a
separate file but i'll just keep it here
just so you can see everything in one
file for visibility so a public
interface called i what's the first
thing i'm doing selecting a server so i
server selection stage
and that stage or that interface will
have a single method in our case
called public and i database
selection state which is the next stage
and now this method will be called
for server assuming i
called that yes uh the same thing here
so for server goes here
and string seven name goes here and now
the next interface i'm gonna create
is actually called this which is my next
stage so public interface
and the name of the of the stage and
then
public i and what do i want to do select
a user so i
user selection stage goes here
and this will be for or actually not for
server what do i call it and database so
and database
string database and i'm actually going
to do that
all the way until the actual connect
method which is
also going to be an interface to
represent the final stage so let me just
add them here
so here we go so we go from server and
database
user password and connect in the end and
then
what you need to do is actually quite
interesting you need to
implement all of these interfaces here
so i
server selection stage i database
selection stage i user selection stage
and
i password selection stage as well as i
connection initializer states i'm going
to go ahead and implement all the
missing members
and the static method will no longer
return the fluent sql connection because
we don't want
direct access to that it's going to
return the
i server selection interface stage
and this will still return a new fluent
sql connection here and then the
implementation for this
is actually the same it's just uh
setting the field and returning the
instance
so server equals server and um
return this and i'm gonna have to do
this for every other method in here so
let me just do that and in very similar
fashion return the idb connection so
this
technically looks the same as what we
had before
but there is a huge difference in
how you actually deal with this look at
this now you have the fluent sql
connection and you say
create connection and that's the only
thing you can see you can't instantiate
this class in any other way
and then you can say for server and
that's it
nothing else so it's a very guided
experience so for server localhost
and then what comes next well next comes
the database so end database
my db so what's next let's say user so
ask user
and in here you can say nick or whatever
your username is
with password password and then
connect and this will
let you create this new connection and
that's it and this is a
better experience in my opinion if you
want a guided and controlled way
because now you have a fluent api that
goes through every single stage that it
needs to go
and you could very much actually have a
fork stage here where it goes from
as user maybe or instead of as user to
with
token or with some other form of
authentication completely up to you
but this is just an example of how you
can do that now for bonus points i don't
know if you've seen this but some
libraries do something like this where
you can have a
sort of config delegate here then you
say something like config
dot connection let's say name equals
next connection and it looks like
something like this where you you have
this lambda looking thing this delicate
looking thing
and to do further configuration this is
also a quite
interesting thing that i'm going to show
you in this video and it looks something
like this
first you need to have the actual uh
configuration class so let's say public
class sequ or just connection
configuration something like that and
then have the property
which i named a connection name here and
you can have many but i'm just going to
have one
and then what you need to have in that
is an
action of that type
and now the thing actually compiles but
just having the action doesn't mean you
have the value
but here's how you get the value you
create
the actual config so you say
configuration equals
new connection configuration and you
do config which is your actual action
here
question mark because it might be null
dot invoke
on the configuration this is weird i
know
but it works effectively what i will do
is we'll apply
the delegate the action on the new
instantiated version
so if i am to actually show you how this
will work by debugging it
i'm gonna do this now i'm gonna execute
this and hopefully step on that
create yes here you can see that we have
the
action and if i step over this
configuration this configuration has
nothing it's just a
null string nothing but then if i
step over this one which has the
instruction then you see that
now my initial object has the
configuration and then you can take that
and do
whatever you want with this and this
should include everything you need to
get started
with fluent apis and method training and
smart configuration
or flexible configuration in.net and
c-sharp
that's all i had for you for this video
thank you very much for watching special
thanks to my patreons for making videos
possible
if you want to support me as well you're
going to find the link description down
below leave a like if you liked this
video subscribe for more content like
sharing the bell as well
and i'll see you in the next video keep
coding