C # - Is this a "good" data access pattern and what is it called?

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

I have 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.");
        }
    }

}

      

Then I 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 used by several 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;

}

      

ClientProvider, SiteProvider, etcProvider all just contain a specific instance of the PositionProvider class. The only difference between them is the entity type and the RP_PositionType enumeration. This way I can use the same concrete Sql implementation in GetList () to drop all records from a specific table based on PositionType (PositionTypeId when the enum is converted to int).

+2


source to share


3 answers


What an Abstract Factory Pattern . And yes, it is a useful and widely used design.



+2


source


I would say it looks like a Factory pattern . Part of it, at least DalHelper.CreateInstance

.



+2


source


This is actually the pattern Singleton

that I believe. Also, why are you doing an explicit "set" statement for PositionType?

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

0


source







All Articles