aamir sajjad
aamir sajjad

Reputation: 3039

Storing Predefined Objects

I am using predefined objects nested inside static class to store objects information at single place, and later i used them after some logic to populate normal objects with properties of predefined object.

What will you say that this approach is fine or I can achieve the same by some other efficient and better way?

//Example of storing predefined objects

 public static class RegistrationGift
    {
        public class InvitedRegistrationGift
        {
            public const int Token = 5;
            public const int Dollar = 0;

        }
    }

//how it will be used to populate credit entity (conversion)

Credit credit = new Credit();
credit.Token = RegistrationGift.InvitedRegistrationGift.Token;  

// and so on

Upvotes: 0

Views: 1023

Answers (3)

Marcelo De Zen
Marcelo De Zen

Reputation: 9497

Using static classes will not bring major problems, however the singleton pattern may fits better in this case.

public class RegistrationConfig {
   // private static instance
   private static RegistrationConfig _instance = new RegistrationConfig()

   // private constructor prevents the class from being instantiated from outside
   private RegistrationConfig() { }

   // instance public accessor
   public static RegistrationConfig Current { get { return _instance; } }

   public int InvitationToken { get; set; }
   public int InvitationDollar { get;set; }
}

Consuming:

var credit = new Credit();
credit.Token = RegistrationConfig.Current.InvitationToken;

A variation of this is not to implement the singleton pattern, but keep the .Current accessor and define a public setter, so you can change the active config.

public class RegistrationConfig {
   // current
   private static RegistrationConfig _current;

   // instance public accessor
   public static RegistrationConfig Current { get { return _current; } }

   // public setter
   public static void SetCurrent(RegistrationConfig current)
   {
       _current = current;
   }

   public int InvitationToken { get; set; }
   public int InvitationDollar { get;set; }
}

Then, on the app startup you set the config.

RegistrationConfig.SetCurrent(new RegistrationConfig() { ... });

And consume:

credit.Token = RegistrationConfig.Current.InvitationToken;

The advantage is that you can create instances with predefined values ​​to use, for example, in unit tests.

Upvotes: 1

Wesley
Wesley

Reputation: 5621

This seems like a scenario where abstraction would fit better.

Depending on the variety of implementations, I would do something like this:

abstract class Credit
{
   protected int Token; 
   protected int Dollar;
}

class InvitedRegistrationGift : Credit
{
   public override int Token
   {
       get 
       {
           return 5;
       }
   }
}

Credit credit = new InvitedRegistrationGift();

Upvotes: 0

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

This feels similar enough to the object-oriented design pattern known as Prototype. Since you're using C#, you might want to have your Credit object implement the ICloneable interface and return a clone of the prototypical object you've defined to populate the credit entry.

Upvotes: 0

Related Questions