0

I have a CLR stored procedure defined in the below class. I am using property injection for MyObject but I'm not sure if it's thread-safe? Note: the CLR stored procedure needs to be static.

public class MyClass
{
    private static IMyObject _myObj;
    public static IMyObject MyObject
    {
        get { return _myObj ?? new MyObject(); }
        set {_myObj = value; }
    }

    static MyClass()
    {
        _myObj = MyObject;
    }

    public static void My_Stored_Procedure()
    {
        _myObj.DoStuff();
    }
}

Mainly the reason for adding the static property is for unit testing.

user3007447
  • 380
  • 3
  • 15
  • 1
    `static` tends to make unit testing _harder_ not easier. – mjwills Jul 08 '20 at 13:47
  • might i suggest injecting dependencies - have a few constructors to handle the logic for you. static makes abstractions hard. – Daniel A. White Jul 08 '20 at 13:47
  • Note that if you are using writable static members in a SQLCLR procedure, the assembly will need to be loaded as `unsafe`. [Refer to this thread](https://stackoverflow.com/questions/663124/what-is-the-sql-server-clr-integration-life-cycle/27310066#27310066) – allmhuran Jul 08 '20 at 14:00
  • Ideal situation to use constructor dependency injection – Nenad Jul 08 '20 at 14:39
  • I don't like creating constructors just for the sake of unit testing but it does seem like that's the best choice. Thanks. – user3007447 Jul 08 '20 at 15:30

0 Answers0