sfdcnoob sfdcnoob - 2 days ago 4
C# Question

How can an interface method be called directly?

I'm reading some code, can you please explain what the below line does?

bool isFeatureEnabled = FeatureControl.Current.Features.AppDesigner.IsEnabled(organizationId,currentOrgDBVersion);


Here's the definitions of the above code

public sealed class FeatureControl : IFeatureControl
{
public static IFeatureControl Current { get; }
[XmlIgnore]
public IFeatureDetailContainer Features { get; set; }
....
}


public interface IFeatureControl
{
IFeatureDetailContainer Features { get; set; }
...
}


public interface IFeatureDetailContainer
{
IFeatureDetail AppDesigner { get; }
}

public interface IFeatureDetail
{
bool IsEnabled(Guid organizationId, Version currentOrgDBVersion);
}


I don't see any instances created, how does this work?

Sorry, I copied metadata, I just found the actual code:

public sealed class FeatureControl : IFeatureControl
{

private static readonly Lazy<IFeatureControl> current = new Lazy<IFeatureControl>(() => new FeatureControl());
private IFeatureDetailContainer features;
public static IFeatureControl Current
{
get
{
return current.Value;
}
}

/// <summary>
/// Accessor to the Features List for Developers to retrieve the information
/// </summary>
[XmlIgnore]
public IFeatureDetailContainer Features
{
get
{
return this.features;
}
set
{
this.features = value;
}
}
}

Win Win
Answer

It is a singleton pattern. Normally, the instance is created inside constructor.

public interface IFeatureControl { }
public sealed class FeatureControl : IFeatureControl
{
    public static IFeatureControl Current { get; }

    static FeatureControl()
    {
        if (Current == null)
        {
            Current = new FeatureControl();
        }
    }
}

[TestFixture]
public class FeatureControlTests
{
    [Test]
    public void IsFeatureControlSingleton()
    {
        IFeatureControl c1 = FeatureControl.Current;
        IFeatureControl c2 = FeatureControl.Current;
        Assert.AreSame(c1, c2);
    }
}
Comments