Singleton Design Pattern (C#, Microservices)

Поделиться
HTML-код
  • Опубликовано: 8 ноя 2024

Комментарии • 60

  • @johnviggogrnbech1089
    @johnviggogrnbech1089 3 года назад +5

    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.

    • @RawCoding
      @RawCoding  3 года назад +2

      As long as the lock is not on critical code path and the creation process doesn’t take too long

  • @ShayanFiroozi1
    @ShayanFiroozi1 2 года назад +2

    I really wonder why you have only 43K subscribers ?!!🤷‍♂️
    It should be at least 1 million 👍
    Very informative channel and contents.
    Greetings from Iran.

    • @RawCoding
      @RawCoding  2 года назад +1

      Share the channel maybe we’ll get there ;) thank you for the kind words

  • @ЕгорБрызгалов-й7ж
    @ЕгорБрызгалов-й7ж 3 года назад +4

    Thank you! That was a really great example!

    • @RawCoding
      @RawCoding  3 года назад

      Thank you for watching

  • @thatcreole9913
    @thatcreole9913 3 года назад +6

    This is extremely well done content! Thank you.

  • @ehvlullo
    @ehvlullo 3 года назад +3

    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?

  • @nandyad
    @nandyad Год назад

    This is the best example

  • @TheNorthRemember
    @TheNorthRemember 3 года назад +1

    greate explanation, bty whats ur headset?

    • @RawCoding
      @RawCoding  3 года назад +1

      It’s shit, don’t recommend - smthn Bose, not the noice cancellation ones

  • @LucasMarinoElementh
    @LucasMarinoElementh 3 года назад +1

    Thanks, amazing content as always!

  • @piotrc966
    @piotrc966 3 года назад +1

    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
      @RawCoding  3 года назад +1

      And works for net5?

    • @piotrc966
      @piotrc966 3 года назад +1

      @@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 :).

    • @RawCoding
      @RawCoding  3 года назад +1

      That exception is expected because we aren’t using a concurrent dictionary. As for null I’d double check what is actually null

  • @mohankumarsantha1770
    @mohankumarsantha1770 3 года назад +3

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

  • @ДмитрийЯворский-е1ф
    @ДмитрийЯворский-е1ф 3 года назад +1

    Good explanation of the pattern)

  • @babybob8823
    @babybob8823 3 года назад +1

    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;
    }
    }

    • @RawCoding
      @RawCoding  3 года назад +1

      that is fine if your creation process is that simple :)

  • @rajenlenka7806
    @rajenlenka7806 3 года назад

    Graitude Brother...Thank you

    • @RawCoding
      @RawCoding  3 года назад

      Thank you for watching

  • @muradazimzadadev
    @muradazimzadadev 2 года назад

    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)

  • @teseract7442
    @teseract7442 2 года назад +1

    If u use task, can use concurrent dictionary for same caching?

  • @superpcstation
    @superpcstation 3 года назад +1

    3:28 I don't understand the down side of using a static ctor? it looks cleaner than using locks

    • @RawCoding
      @RawCoding  3 года назад +2

      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.

    • @superpcstation
      @superpcstation 3 года назад

      @@RawCoding Thank you :)

  • @XpLoeRe
    @XpLoeRe 3 года назад +1

    what a fucking god.

    • @RawCoding
      @RawCoding  3 года назад

      Thank you, no god though just a nerd )

  • @tomthunderforest1681
    @tomthunderforest1681 3 года назад +1

    why you didn't use lock(_instance) ? 7:37

    • @RawCoding
      @RawCoding  3 года назад +2

      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.

    • @tomthunderforest1681
      @tomthunderforest1681 3 года назад +1

      ​@@RawCoding Thank you for explanation :) can't wait for next episodes. Good work !

  • @clearlyunwell
    @clearlyunwell 3 года назад +1

    👍🏽

  • @madd5
    @madd5 3 года назад +1

    ты работаешь программистом сам, или только ютюб?

  • @algarud
    @algarud 3 года назад +1

    Do Observer and Strategy :)

  • @ShayanFiroozi1
    @ShayanFiroozi1 2 года назад +1

    #suggestion
    Would you please make a video about how to choose our apps architecture and design patterns with real world example ?
    Thank you.

    • @RawCoding
      @RawCoding  2 года назад +1

      It’s a hard topic to cover I’ll see what I can do

    • @ShayanFiroozi1
      @ShayanFiroozi1 2 года назад

      @@RawCoding Thank You 👍

  • @wisnu7734
    @wisnu7734 3 года назад +1

    Please make video System.IO.Pipeline

    • @RawCoding
      @RawCoding  3 года назад

      I’ll do, it’s quite a niche api didn’t have to use it that much

  • @darkomartinovic1955
    @darkomartinovic1955 6 месяцев назад

    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()
    {
    }
    }

  • @stefanioan7569
    @stefanioan7569 3 года назад +1

    What happened to you? Where’s the beard?

  • @cigdemturkmen
    @cigdemturkmen 3 года назад +1

    still too advanced for me. I will come back 6 months later

  • @godsonjoseph
    @godsonjoseph 3 года назад +1

    First :P