-2

I'd like to store LicenseInformations for multiple domains in my application.

The structure looks the following way:

   public class LicenseData
    {
        // properties...
        public List<LicenseDomain> Domains { get; set; }
        // other properties...
    }

    public class LicenseDomain
    {
        // properties...
        public object LicenseConfig { get; set; }
    }

We have multiple domains with total different properties, but the license may contain multiple configurations..

For example:

{
   "MaxValidUsers": 5
}
{
   "Property": "xy"
   "SubProperty": { "Foo" : "Bar" 
   }
}

The generation is no problem in any way.. But if I restore the informations from my signed json file I deserialize to object..

Which pattern / possiblity I have to work with Interfaces / Abstracts / that I can (RE)store generic informations here..

Right now I hack with:

JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(domain.LicenseConfig))

But I can't agree with myself.

Andréw
  • 96
  • 1
  • 11

1 Answers1

1

So, based on the pieces of context I can grab, I would actually recommend having your LicenseConfig stored as a JSON string, which would give you the ability to do something like this:

public class LicenseDomain
{
    // properties...

    // Depending on how this is loaded,
    // this property (or at least its setter) could be made private/protected/internal
    public string LicenseConfigJson { get; set; }

    public T LicenseConfig<T>() where T : BaseLicenseConfig
    {
        if (string.IsNullOrWhiteSpace(LicenseConfigJson))
        {
            return null;
        }
        return JsonConvert.DeserializeObject<T>(LicenseConfigJson);
    }
    public void SaveLicenseConfig<T>(T config) where T : BaseLicenseConfig
    {
        if (config == null)
        {
            LicenseConfigJson = null; 
        }
        else
        {
            LicenseConfigJson = JsonConvert.SerializeObject(config);
        }
    }


}

Or if each LicenseDomain can only have one type of LicenseConfig, you could make it a generic parameter to the class:

public class LicenseData
{
    // properties...
    public List<LicenseDomain<BaseLicenseConfig>> Domains { get; set; }
    // other properties...
}

public class LicenseDomain<T> where T : BaseLicenseConfig
{
    // properties...

    // Depending on where this value comes from, you could do this a variety of ways, 
    //but this is just one
    public string LicenseConfigJson { get; set; }
    public T LicenseConfig
    {
        get
        {
            if (string.IsNullOrWhiteSpace(LicenseConfigJson))
            {
                return null;
            }
            return JsonConvert.DeserializeObject<T>(LicenseConfigJson);
        }
        set
        {
            if (value == null)
            {
                LicenseConfigJson = null;
            }
            else
            {
                LicenseConfigJson = JsonConvert.SerializeObject(value);
            }
        }
    }
}

public abstract class BaseLicenseConfig
{
    
}

public class LicConfig1 : BaseLicenseConfig
{
    public int MaxValidUsers { get; set;}
}

public class LicConfig2 : BaseLicenseConfig
{
    public string Property {get;set;}
    public SubProp SubProperty {get;set;}
}

public class SubProp
{
    public string Foo {get;set;}
}

In both cases, the BaseLicenseConfig class is strictly to enforce that everything in the domain list can come from a base class of some kind. If that's not important, you don't need the base class and can remove the where T : BaseLicenseConfig from LicenseDomain class.

s0n1c
  • 78
  • 9