I really wonder why you have only 43K subscribers ?!!🤷♂️ It should be at least 1 million 👍 Very informative channel and contents. Greetings from Iran.
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.
@@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 :).
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. 😊
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; } }
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)
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.
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.
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() { } }
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.
As long as the lock is not on critical code path and the creation process doesn’t take too long
I really wonder why you have only 43K subscribers ?!!🤷♂️
It should be at least 1 million 👍
Very informative channel and contents.
Greetings from Iran.
Share the channel maybe we’ll get there ;) thank you for the kind words
Thank you! That was a really great example!
Thank you for watching
This is extremely well done content! Thank you.
Glad you like it
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?
No
This is the best example
greate explanation, bty whats ur headset?
It’s shit, don’t recommend - smthn Bose, not the noice cancellation ones
Thanks, amazing content as always!
Thank you
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.
And works for net5?
@@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 :).
That exception is expected because we aren’t using a concurrent dictionary. As for null I’d double check what is actually null
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. 😊
Cheers
Good explanation of the pattern)
Cheers
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;
}
}
that is fine if your creation process is that simple :)
Graitude Brother...Thank you
Thank you for watching
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)
If u use task, can use concurrent dictionary for same caching?
Sure
3:28 I don't understand the down side of using a static ctor? it looks cleaner than using locks
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.
@@RawCoding Thank you :)
what a fucking god.
Thank you, no god though just a nerd )
why you didn't use lock(_instance) ? 7:37
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.
@@RawCoding Thank you for explanation :) can't wait for next episodes. Good work !
👍🏽
)
ты работаешь программистом сам, или только ютюб?
Работаю
Do Observer and Strategy :)
We’ll get there
#suggestion
Would you please make a video about how to choose our apps architecture and design patterns with real world example ?
Thank you.
It’s a hard topic to cover I’ll see what I can do
@@RawCoding Thank You 👍
Please make video System.IO.Pipeline
I’ll do, it’s quite a niche api didn’t have to use it that much
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()
{
}
}
What happened to you? Where’s the beard?
🔪
still too advanced for me. I will come back 6 months later
No rush )
First :P
👏