Disclaimer!
This post is a little bit ridiculous and over the top but I'd be doing injustice if I do not share my thoughts on how the journey of learning something new feels like. But if you're in a rush, just scroll to the snippet at the bottom
So here's the thing. Years ago, when I had just started programming, as part of my degree, I hated C#. I didn't want anything to do with any project related to Microsoft related tech. Here's the rationale behind this (it's a dumb rationale) Why does C# have a 'C' in its name but looks almost exactly like Java? I also believed that a good programmer must know Java, C/C++, PHP for server programming, and Python just to prove you're smart. I swear this is a phase that I literally went through.
This was because when you've just started coding, for some reason there's this notion of being defined by the programming languages you know and the libraries you use. I'm sure that was greatly influenced by the "Top 10 best languages to learn in 201X" or "Language X is dead, here's why", even "Programming languages used by Facebook" articles that I actively looked for! just so that I don't get left behind in the industry.
Of course, a lot changed in the way I view tech and my career but what I felt about C# or anything related to it never changed, until about 6-7 months ago. See, at work, they use a shit ton of .NET (or dotnet. I'm still getting the hang of this) so I'm learning it now. A few months ago, I completed a 'Basics for beginners' udemy course. It was fine I guess🙄 but learned a lot of cool stuff 🤗
Now I'm creating a project so that I learn more of this freaky stuff. Enter .NET Core 3! The most overwhelming piece of framework I've ever encountered (this year. I have to be clear on this). I have never given up so many times but come back to try again, in just a single week, ever for any project that I ever started to learn. When I finally got my thingie to give me a token for authentication, after days and nights of struggle, I ran the most satisfyingly powerful git command known to mankind
git commit -m 'Initial commit'
I was happy. Then I had my supper while rereading up on what I've implemented and that's when I saw it
The masterpiece
private User GetUserByToken(string token)
{
var user = (from u in _context.Users
join t in _context.Tokens
on u.Id equals t.UserId
where t.Body == token
select u).SingleOrDefault();
return user;
}
It's beautiful isn't it? I thought so too
I mean do you understand that the last time I saw something this cool was when I saw JSX for the first time, which looked so confusing but really not so trivial after some time of use. My brain doesn't have to context switch between application code and database scripts but the thing just works seamlessly. And the syntax highlighting plus the IntelliSense! I am stunned.
If you're reading this and you do not understand WTF is going on here, it's okay, it's not your time yet. But later on in your life, you will see what I saw and you'll say "shit, that dude on dev.to wrote a weird post about this feeling I'm feeling right now".
Latest comments (64)
Mastering programming may seem like a daunting task, but with the right help, it becomes more attainable. anyknows.ai/ is a place where you can find various materials and courses to help you develop your programming skills. Thanks to this resource, you can expand your knowledge and become more confident in programming step by step.
I have just started learning this programming language. And I don't understand everything about it yet, but it's still interesting. I also needed c++ assignment help, which is also difficult for me. And I am glad that there are platforms ready to help me understand it.
I like LINQ syntax, but I think it's not really necessary here. I'd write it as:
Of course, this is a subjective matter, so any approach is OK if the team is fine with it.
You have never written in a functional language, true that?
FSharp is far superiour and tons more declarative as CSharp ever can be.
It is a superset, csharp has nothing that fsharp has not and the other way around has fsharp everything that csharp has and uses barely anything of it, since every fsharp dev knows, it is the by far weakest part of the language.
Sadly, scientific evidence counts less in a world full of self declared "computer scientists"
fsharp.org/testimonials/
Lovely article Beautus! As other commenters mention I also remember this thrill and power when witnessed at first. It is truly an amazing feeling and I share your excitement with JSX. It is great to hear you have bound with feature of a language such as LINQ as that will guide you to discover other amazing features like maybe the newest pattern matching in switch clauses? Which again might lead you to a totally different but .NET language as F#.
Keep on mind, LINQ syntax gives you range variables which you do not have in the lambda function syntax. The LINQ syntax eventually ends up “compiled” into lambda syntax, i.e. just a bunch of methods, read a ref explanation here: stackoverflow.com/a/20573973
If you are interested to understand the fantastic IEnumerable “driver” of the LINQ and how to literally reimplement then google for EduLINQ by Jon Skeet. Harnessing the yield keyword with iterators and generators is what made love C# even more. Afterwords you can google how LINQtoEntities works (thats the one you use with _context), hint/spoiler its an amazing compiler that helps it with “translation” into SQL with a ton of expression visitor pattern implementations composed to build a compiler pipeline... uh :-)
Also a side note on async as I am very allergic to that topic :-), it will make your code slower but it will take care of memory better in longer (scale up scenario) shots. It is not a silver bullet and it has nothing to do with increasing performance at all (only maybe decreasing it). Google Stephen Cleary and his blog for more info.
Cheers,
V.
I just read through the stackoverflow link and I'm stunned. But through that revelation, I had to wonder how performant a Lambda api would fare up against 100 million rows of data as opposed to using a raw query
Hey. Your thoughts stream to right questions! Actually EF is a lot slower than raw cons (or Dapper queries), for a test in the following link 10x to 15x times, but the test is not a real world scenario, i.e. do not infere that Dapper or ADO is better, as speed is just one treat. Read more about the test: exceptionnotfound.net/dapper-vs-en...
Also EF has to cache your lambdas after translating them for the first time into SQL strings. Also note some queries can not be cached due to their dynamic construction (you can build up a query with multiple C# statements) such as any query using a .Where(x => someNumbers.Contains(x.Id)), as the SQL “where in” (it is actually a ton of OR statements if you look into SQL) part is actually calculated each time.
Using EF is certainly not a question of speed.
"Enter .NET Core 3! The most overwhelming piece of framework I've ever encountered"
to be honest I think .net core is not a framework
Thanks for the inspiration bro, 'cause I have just started with the language and I must say for a beginner it's frustrating with all it's .Net types😩😣😭 but after reading this for sure nami I'll be set👍
I also hate C# because of so many libraries and many built-in methods in these libraries. But i enjoyed working on C# Windows Form Applications.
This is sexy
Now are any employers going to move to .NET Core 3 when .NET 5 is right around the corner and promises to unify everything? Probably not unless you're a business that rebuilds everything from scratch every 2 years. You'd be a fool to use .NET Core 3 if your core product gets rebuilt only every 5-10 years.
So yeah, we will see. Smart money is on waiting.
I will add my voice to the others here who recommend avoiding LINQ syntax. Most MS development shops have gotten away from LINQ syntax and your query shows one good reason:
It might be more efficient for the
where
to execute before thejoin
. With the LINQ syntax you can do it but it looks odd.Now compare:
For your specific case, I suggest you turn
_context.Tokens
into anIDictionary<string, Token>
or evenIDictionary<string, User>
where the token body is the key. Then your method looks like this:I have a question with this one. How would my DBContext class look like, will this UsersByToken field have to be mapped to a table in the bd too?
One thing that has been mentioned a couple of times is that I should consider using IDictionary, I'm looking forward to it
Ah. I did not know what
_context
was and I was not aware you were using EF. I assumed_context
was some kind of property bag or class you controlled. So, the answer is no you would not have aUsersByToken
property on yourDbContext
and it would not map to the database. Instead it should be a local field/property in your login class. Now that I understand you are making round trips to the database, you will findIDictionary<>
to be even more efficient than your current code, but you will need a fall back to retrieving the user from the database if it is not found associated the provided token in your dictionary.Thank you for this. Seems like IDictionary and IQueryable are my best bet for getting the most out of EF