0

I want to implement a class whose instance is global but whose property is to be initialized only once during the run time.

Also the initialization is to be done as an assignment from a result of function during execution.

Basically I want to do something like this

public class Configuration
{
     public string param1 { get ; set; }
     public int param2 { get; set; }
}

public static class AppConfig
{
    public static readonly configuration;   
}

public class Initialize
{
    public void InitConfig()
    {
        AppConfig.configuration = GetParamsFromDB();
    }
}

But I am unable to figure out how to implement it. Please ignore the above incorrect representation. It is just to present what is required.

EDIT

Also there is a need of seperate class Initialize because classes Configuration and AppConfig are in dll BO. GetParamsFromDB() is in DAL. DAL references BO hence BO cannot refere DAL hence GetParamsFromDB() cannot be used within AppConfig class

nishantv
  • 643
  • 4
  • 9
  • 27
  • [Implementing the Singleton Pattern in C# by Jon Skeet](http://csharpindepth.com/Articles/General/Singleton.aspx) – Habib Sep 13 '13 at 14:18

3 Answers3

1

It looks like you want a singleton.

See: Implementing the Singleton Pattern in C#

public static class AppConfig
{
    private static readonly Lazy<Configuration> _configuration = new Lazy<Configuration>(() => new Configuration());

    public static Configuration Instance { get { return _configuration.Value; } }
}

However, you should consider changing your design as singletons are often overused.

Consider something that can be used with dependency injection and inversion of control.

  • Dependency injection is a pattern that increases code reuse and minimize dependencies through interfaces.

  • Inversion of control is a pattern that binds objects together at runtime typically using an assembler object.

Example:

public interface IAppConfig
{
    Configuration Configuration { get; }
}

public sealed class AppConfig : IAppConfig
{
    private readonly Configuration _configuration;

    public AppConfiguration()
    {
        _configuration = new Configuration { };
    }

    public Configuration Configuration { get { return _configuration; } }
}

This can be used together with an IoC Container to provide configuration to all the objects that need it.

Community
  • 1
  • 1
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • You take the time to link to Jon's article, and then don't even use the solution that he suggests using in a context such as this... – Servy Sep 13 '13 at 14:43
  • If you're talking about laziness then from the article: "[...]you won't actually require full laziness - unless your class initialization does something particularly time-consuming" such as talking to a database. However, I haven't read the article in awhile and I don't think a singleton is a good approach in this case. – Dustin Kingen Sep 13 '13 at 14:51
  • The only advantage of using something like `Lazy` has over the 4th solution is that it allows you to initialize one static field and not another, which isn't relevant when there is only one static field. Being in the situation where you want to initialize one field and not another is quite uncommon, as Jon himself said, which is why the fourth solution is, in practice, virtually always sufficient in its laziness. – Servy Sep 13 '13 at 14:55
1

All you need to do is initialize it inline:

public static class AppConfig
{
    public static readonly configuration = GetParamsFromDB();   
}

The C# runtime will automatically ensure that the parameter isn't initialized until the class is accessed for the first time, giving you your desired behvaior.

Note that your configuration type is mutable, which if you want to ensure these values aren't changed, is a bad thing. You should refactor your configuration class to accept the two values in its constructor and not provide public setters for the properties.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Thanks for the reply Servy. I knew this option however the design is such that There is BO project and DAL project. GetParamsFromDB would access DAL and this will create circular reference as DAL references BO. Thats why i was wondering if this expressiion itself could be written elsewhere while acheiving the desired result. – nishantv Sep 14 '13 at 05:18
  • @nishantv If that's what's making the problem hard then why is none of that mentioned in the question itself? In any case, the solution is simply that the code referenced from one project in the other really belongs in a different DLL. – Servy Sep 14 '13 at 12:42
  • my bad definitely. I have updated the question. I would prefer not to move the class which represents only properties from BO to another dl. That is the last option though. – nishantv Sep 16 '13 at 07:32
0

What you are trying to do is kind of Singleton Pattern, It can be implemented as follows,

public sealed class Configuration
{
   private static volatile Configuration instance;
   private static object syncRoot = new Object();

   private Configuration() {}

   public static Configuration Instance
   {
      get 
      {
         if (instance == null) 
         {
            lock (syncRoot) 
            {
               if (instance == null) 
                  instance = new Configuration();
            }
         }

         return instance;
      }
   }
}
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
  • Why go through all of this work when the problem is really [this easy](http://stackoverflow.com/a/18788641/1159478). This pattern is longer, less readable, less performant, more error prone, and has no advantages in context. – Servy Sep 13 '13 at 14:21