开发者

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

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> In开发者_运维百科stance
    {
        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).


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜