Singleton-like pattern for a project I'm changing the inner implementation of a project.  In order to do so, I've created an interface that will be implemented in 2 different ways.

One of this ways is by class A, which is a wrapper for an outside class (class "B") from a .dll file (a product of other team).

Class B is a private a member of class A.  Class B ctor takes an argument, which does not change during the run of my application. It needed to be created only once, and to stay available for class A everywhere in the code.

Class A is an implementation of interface.  It use the capabilities of class B in order to implement it's functions, and obviously needed to be available also everywhere in the code. 

I've concluded that:

Class A has to be:


explicitly created once (hence, not static)  
only once, (because I need B to be created only once.) 
accessible from anywhere in the code
have a member (B) that must initialized once, and only once (private member, needed only inside class A)


Concerning condition #4, ideally I would like to initialize B in the constructor.

A bit similar to singleton pattern, with the exception of wanting to initialize a member in the constructor.

I've come to a solution, and would like to get feedback.

public sealed class A
{
   private static SomeClass B = null; // the "must initialized only once" member
   // setting B:
   private void setB(int i)
   {
      B = new SomeClass(i);
   }
   // ctor
   public A(int i)
   {
      if(B == null)
         setB(i);
      else throw new Exception("An instance of A can be created only once");
   }     
}


The idea is to assign the class to static object at the beginning of the code.  It's not very neat, but it compiles and seems to do the job just right.  I don't use singleton since it won't be possible to initialize the member in the ctor.  By the way, thread safety is not a concern here.   

SomeClass needed only inside class A. SomeClass comes from dll, and class A kind of wrapping it.  A does answering cross-cutting concern, it is an implementation of Interface used all over the code. I've tried to implement singleton, but as much as I tried it didn't answer my needs.
If you need to wrap SomeClass, make your wrapper an abstraction:

public interface ISomeClassWrapper
{
    // expose the methods and properties you want to wrap
}


Then everywhere it's needed in the code, replace static method calls like this:

public void DoSomething()
{
    SomeClass.Xyz();
}


With something like this:

public class MyOtherClass
{
    private readonly ISomeClassWrapper _wrapper;

    public MyOtherClass(ISomeClassWrapper wrapper)
    {
        _wrapper = wrapper;
    }

    public void DoSomething()
    {
        _wrapper.Xyz();
    }
}


Now, if you're worried about how many instances of SomeClassWrapper (which implements ISomeClassWrapper but MyOtherClass doesn't need to know that), don't instantiate it. ever.

How do you ensure there's only 1 instance of it?

When you've replaced SomeClass.Xyz(); with _wrapper.Xyz();, you've done several things:


MyOtherClass is now decoupled from SomeClass, and even from SomeClassWrapper.
The ISomeClassWrapper dependency is now statically declared as such, so just by looking at MyOtherClass's constructor you know it needs an implementation for ISomeClassWrapper to carry out its tasks.
You can now provide a fake/mock implementation of ISomeClassWrapper, and write a test for DoSomething() without worrying about side-effects in code that you have no control over.
The code that's responsible for providing the ISomeClassWrapper instance/implementation, is also responsible for passing everyone that wants an ISomeClassWrapper instance, the same instance. Most IoC containers can do this very, very easily.