lundi 27 juin 2016

C# Implementing concrete factory (derived from an abstracf factory) for Generic Interface type

I have an abstract factory that I use to implement some other concrete factories. This factory is implemented this way:

    public class AbstractFactory<T> {

    public delegate List<T> AbstractProductCreator();

    private Dictionary<string, AbstractProductCreator> _creators = new Dictionary<string, AbstractProductCreator>();

    private static AbstractFactory<T> _instance = null;
    public static AbstractFactory<T> Instance {
        get {

            if( _instance == null ) {

                _instance = new AbstractFactory<T>();
            }

            return _instance;
        }
    }

    private AbstractFactory() { }

    public bool registerProduct( string setName, AbstractProductCreator creator ) {

        if( !_creators.ContainsKey( setName ) ) {

            _creators[setName] = creator;
            return true;
        }

        return false;
    }

    public List<T> getProduct( string setName ) {

        if( _creators.ContainsKey( setName ) ) {

            return (_creators[setName])();
        }

        return null;
    }
}

On the other hand, I have a generic interface for managing different entities of a DB:

    public interface IEntitySetMgr<T> {

    /// <summary>
    /// The columns involved in the query. Only fields in those columns
    /// will be retrieved from the db.
    /// </summary>
    List<ColumnInfo> Columns { get; set; }

    /// <summary>
    /// The number of entities found in the last query to db.
    /// </summary>
    int EntityCount { get; }

    bool Init();

    bool Synch( QueryFilter queryFilter = null );

    /// <summary>
    /// Retrieves the entities build from data of db.
    /// </summary>
    ObservableCollection<T> GetEntities();
}

It is possible to have a concrete factory for registering classes that implement that interface? I've tried this without success:

    public class EntitySetMgrFactory : AbstractFactory<IEntitySetMgr<T>> {

}

But it is going no where. I remember have made something similar in C++ with templates. I know that generics are not the same as C++ templates, but I thought there should be a way of doing this. I have also tried to create an abstract implementation of IEntitySetMgr and creating a concrete factory for this instead of the interface. I've also tried to define another abstract factory for this case with two parameters, but cannot reach a solution the compiler does not complains and work.

Any help would be appreciated.

Regards,

David.

Aucun commentaire:

Enregistrer un commentaire