Question

I have implemented an interface IService that inherits functionality from a series of other interfaces and serves as a common ground for many different services.

Each of these services is being described by an interface, for example:

public interface IServiceOne : IService 
{
  //...
}

public class ServiceOne : IServiceOne
{
  //...
}

Everything up to that point works as expected:

IServiceOne serviceOne = new ServiceOne();
IServiceTwo serviceTwo = new ServiceTwo(); 

What I have to do now is to add a big list of constants (public variables) to each of these services which will however be different as per service type (for example, IServiceOne will have different constants than IServiceTwo, there will be constants in IServiceOne that will not exist in IServiceTwo, etc).

What I'm trying to achieve is something like that:

IServiceOne serviceOne = new ServiceOne();
var someConstantValue = serviceOne.Const.SomeConstant;

Just because the variables will differ as of service type I decided to implement an extra interface for each of them:

public interface IServiceOneConstants
{
   //...
}

and then broaden my IService definition:

public interface IServiceOne : IService, IServiceOneConstants 
{
  //...
}

public class ServiceOne : IServiceOne
{
  //...
}

The problem I have now is that I don't know how to implement the concrete class for IServiceOneConstants. Obviously by the time one of its variables (we called them constants here) will be called it has to be instantiated, so initially I though of a static class but then you cannot expose a static class's functionality through an interface. I then tried to do it with a singleton and expose its instance via a public non-static wrapper:

public class Singleton : IServiceOneConstants
{
    private static Singleton _instance;

    private Singleton()
    {
        SomeConstant = "Some value";
    }

    public static Singleton Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new Singleton();
            }
            return _instance;
        }
    }

    public String SomeConstant { get; set; }

    public Singleton Const
    {
        get
        {
            return Instance;
        }
    }
}

I then adjusted the IServiceOneConstants like that:

public interface IServiceOneConstants
{
   Singleton Const { get; }
}

but when I call this:

IServiceOne serviceOne = new ServiceOne();
var someConstantValue = serviceOne.Const.SomeConstant;

I get a null reference exception, as .Const is null.

What am I missing here?

Was it helpful?

Solution

You really helped yourself to get confused as possible, by naming different stuff same name ;)

So, first... what you're trying to do is to access singleton instance through instance property:

public Singleton Const
    {
        get
        {
            return Instance;
        }
    }

then you are using it like:

serviceOne.Const

but that variable was never assigned. In order to assign it, you should make an instance of Singleton class, assign it to serviceOne.Const property and then you might use it.

What you need is probably something like this:

public class ServiceOne : IServiceOne
{
   public Singleton Const
   { 
      get
      {
         return Singleton.Instance;
      }
   }
}

OTHER TIPS

You need to check to see if the singleton has been instantiated in ServiceOne.Const.SomeConstants` getter. If it's not, you need to instantiate it. Then return the value of the constant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top