I have been using locking just like this for my caching. Just nice to see that I am actually doing it as you explain it here with locking.
@RawCoding3 жыл бұрын
As long as the lock is not on critical code path and the creation process doesn’t take too long
@ShayanFiroozi12 жыл бұрын
I really wonder why you have only 43K subscribers ?!!🤷♂️ It should be at least 1 million 👍 Very informative channel and contents. Greetings from Iran.
@RawCoding2 жыл бұрын
Share the channel maybe we’ll get there ;) thank you for the kind words
@ЕгорБрызгалов-й7ж3 жыл бұрын
Thank you! That was a really great example!
@RawCoding3 жыл бұрын
Thank you for watching
@superpcstation3 жыл бұрын
3:28 I don't understand the down side of using a static ctor? it looks cleaner than using locks
@RawCoding3 жыл бұрын
It runs on app start, so long creation processes will cause long startup time, I know it can sound silly but some apps are REALLY BIG. Additionally it’s hard to control the flow of construction as well as lack of being able to use async.
@superpcstation3 жыл бұрын
@@RawCoding Thank you :)
@thatcreole99133 жыл бұрын
This is extremely well done content! Thank you.
@RawCoding3 жыл бұрын
Glad you like it
@ehvlullo3 жыл бұрын
Awesome stuff. Do you ever see reason to write your own singleton (/locking) pattern in a production environment that has a DI container in place?
@RawCoding3 жыл бұрын
No
@LucasMarinoElementh3 жыл бұрын
Thanks, amazing content as always!
@RawCoding3 жыл бұрын
Thank you
@TheNorthRemember3 жыл бұрын
greate explanation, bty whats ur headset?
@RawCoding3 жыл бұрын
It’s shit, don’t recommend - smthn Bose, not the noice cancellation ones
@nandyad Жыл бұрын
This is the best example
@ДмитрийЯворский-е1ф3 жыл бұрын
Good explanation of the pattern)
@RawCoding3 жыл бұрын
Cheers
@muradazimzadadev2 жыл бұрын
Great elucidation! But a problem here is that private constructor of safethread singleton class called 3 times, however it should have been called once. Anyone has any idea? ( No difference in the code)
@tomthunderforest16813 жыл бұрын
why you didn't use lock(_instance) ? 7:37
@RawCoding3 жыл бұрын
Because instance is null and we need to instantiate it, if it’s null there’s nothing to lock. You can create an empty implement that will indicate it needs instantiation but at that point it’s too much work.
@tomthunderforest16813 жыл бұрын
@@RawCoding Thank you for explanation :) can't wait for next episodes. Good work !
@teseract74422 жыл бұрын
If u use task, can use concurrent dictionary for same caching?
@RawCoding2 жыл бұрын
Sure
@piotrc9663 жыл бұрын
Interesting case. 'bad' version: if (!c.Contains("job_id", "job1")) { c.Write("job_id", "job1"); Console.WriteLine("Big Operation"); } when set .net core 3.1 in LinqPad generate null reference exception for : { _registry[key] = value; } Same exeption is in Visual Studio. :) but just change for: { Console.WriteLine(_registry == null); _registry[key] = value; } And it's OK.
@RawCoding3 жыл бұрын
And works for net5?
@piotrc9663 жыл бұрын
@@RawCoding Yes, in LinqPad works fine, but in Visual Studio throws: "System.InvalidOperationException: 'Operations that change non-concurrent collections must have exclusive access. " This exception seems OK, but for net 3.1 "null reference" in VS and LinqPad seems very strange - probably some bugs in .NET :).
@RawCoding3 жыл бұрын
That exception is expected because we aren’t using a concurrent dictionary. As for null I’d double check what is actually null
@babybob88233 жыл бұрын
Thanks for the video!! It was great. Can we do this without static ctor and lock? Wouldn't be it thread-safe and the instance will be created only once when Create() is invoked? Correct me if I'm wrong. public class MemoryCache { private static readonly MemoryCache _instance = new MemoryCache(); private MemoryCache() {} public static MemoryCache Create() { return _instance; } }
@RawCoding3 жыл бұрын
that is fine if your creation process is that simple :)
@mohankumarsantha17703 жыл бұрын
I like this video, but little advice to you. Try to give a small explanation about the pattern and then start with an example that makes more sense to learners. 😊
@RawCoding3 жыл бұрын
Cheers
@rajenlenka78063 жыл бұрын
Graitude Brother...Thank you
@RawCoding3 жыл бұрын
Thank you for watching
@madd53 жыл бұрын
ты работаешь программистом сам, или только ютюб?
@RawCoding3 жыл бұрын
Работаю
@ShayanFiroozi12 жыл бұрын
#suggestion Would you please make a video about how to choose our apps architecture and design patterns with real world example ? Thank you.
@RawCoding2 жыл бұрын
It’s a hard topic to cover I’ll see what I can do
@ShayanFiroozi12 жыл бұрын
@@RawCoding Thank You 👍
@clearlyunwell3 жыл бұрын
👍🏽
@RawCoding3 жыл бұрын
)
@devjutsu3 жыл бұрын
Do Observer and Strategy :)
@RawCoding3 жыл бұрын
We’ll get there
@XpLoeRe3 жыл бұрын
what a fucking god.
@RawCoding3 жыл бұрын
Thank you, no god though just a nerd )
@wisnu77343 жыл бұрын
Please make video System.IO.Pipeline
@RawCoding3 жыл бұрын
I’ll do, it’s quite a niche api didn’t have to use it that much
@stefanioan75693 жыл бұрын
What happened to you? Where’s the beard?
@RawCoding3 жыл бұрын
🔪
@cigdemturkmen3 жыл бұрын
still too advanced for me. I will come back 6 months later
@RawCoding3 жыл бұрын
No rush )
@darkomartinovic19558 ай бұрын
What about Lazy? public sealed class Singleton { private static readonly Lazy lazy = new Lazy(() => new Singleton()); public static Singleton Instance { get { return lazy.Value; } } private Singleton() { } }