views:

197

answers:

3

First of all, I apologise for the vagueness of the question title and if this has been asked elsewhere already. I struggled to find a similar answer due to the amount of other 'what is this pattern called' questions.

I've got the following abstract class:

public abstract class PositionProvider<T> : DalProvider<T>, IDalProvider 
    where T : IPositionEntity
{

    protected PositionProvider()
        : base() { }

    public RP_PositionType PositionType
    {
        get
        {
            return _positionType;
        }
    }
    private RP_PositionType _positionType;

    public abstract List<T> GetList();

    internal void SetPositionType(RP_PositionType positionType)
    {
        if (_positionType == null)
        {
            _positionType = positionType;
        }
        else
        {
            throw new NotSupportedException(
                "PositionType can only be set once.");
        }
    }

}

I've then got a concrete implementation of this class:

public class SqlPositionProvider<T> 
    : PositionProvider<T> where T : IPositionEntity
{
    public override List<T> GetList()
    {
        int positionTypeId = (int)this.PositionType;
        // Return the matching items here
    }
}

This is then utilised by a number of different 'item' classes, such as the following, but replacing CustomerEntity with SiteEntity/MajorEntity/MinorEntity:

public class CustomerProvider
{

    public static PositionProvider<CustomerEntity> Instance
    {
        get
        {
            if (_instance == null)
            {
                DalHelper.CreateInstance<PositionProvider<CustomerEntity>>(
                    out _instance);
                _instance.SetPositionType(RP_PositionType.Customer);
            }
            return _instance;
        }
    }
    private static PositionProvider<CustomerEntity> _instance;

}

The CustomerProvider, SiteProvider, etcProvider are all just hold a specific instance of the PositionProvider class. The only difference between them is the entity type and the RP_PositionType enum. This is so I can use the same Sql concrete implementation within GetList() to pull back all records from a particular table based on the PositionType (PositionTypeId when the enum is converted to an int value).

+2  A: 

I would say that this looks like the Factory pattern. At least, the DalHelper.CreateInstance part.

Konamiman
+2  A: 

That's the Abstract Factory Pattern. And yeah, it's an useful and widely used design pattern.

Jorge Córdoba
A: 

This is actually the Singleton pattern I believe. Also, why would you do an explicit 'set' statement for PositionType?

You are better off using a 'set' in the property instead of a function, it is much clearer for developers using the class.

Erich
I didn't want a set in my property because I only ever want it set once. As it can potentially be one of 5 enums, I want to ensure it's never changed once set. There are definately aspects of a Singleton in play here, but that's only for the instance side of things. Also it's not strictly a correct singleton as it's not thread safe.
GenericTypeTea