jeudi 17 août 2017

c# ,Get Generic Type Property from Abstract Factory Classes

I am implementing the abstract factory pattern to create a dynamic Matrix DataTable. The idea is create different generator instance base on a factory class. However, at the end of the structure, i wish to obtain the end result as an object that would going to expose to the MVC View. The problem is I cant think of a way to extract the output because the generator instance bear with generic attribute. My code is like this :

Enum and Generic Classes

public enum GeneratorType
    {
        GenericBudgetGenerator = 1
    }


public class GenericMatrix<T>
{
    public List<string> MatrixHeaders { set; get; }
    public List<T> MatrixRow { set; get; }
    public T MatrixFooterRow { set; get; }
}

public class GenericItemRow
    {
        public string EntityName { set; get; }
        public List<double> Values { set; get; }
        public double Total { set; get; }

    }

Generator and Interface

public interface IGenerator
    {
        void Build();
    }

public interface IMatrixGenerator<TRow>
        : IGenerator
    {
        IEnumerable<TRow> CreateCellValues();
    }

public class MatrixGeneratorFactory
{
    public static IGenerator Create(Enum _type)
    {
        switch (_type)
        {
            case GeneratorType.GenericGenerator :
                return new GenericGenerator ();
            default:
                throw new ArgumentException("No generator exist");
        }
    }
}

public class MatrixGeneratorBase
{         
            public void BuildMatrixTemplate()
        {
              //some logic
         }

}

public class GenericGenerator : MatrixGeneratorBase, IMatrixGenerator<GenericItemRow>
{

        public IEnumerable<GenericItemRow> CreateCellValues()
        {
              //some logic here
         }

        public void Build()
        {
             BuildMatrixTemplate();

             GenericMatrix<GenericBudgetValueMatrixRow> _genericMatrix = new GenericMatrix<GenericBudgetValueMatrixRow>();

              _genericMatrix = //Some logic here
         }

}

Client Code

 class Program
 {
    static void Main(string[] args)
    {
       IGenerator generator = MatrixGeneratorFactory.Create(GeneratorType.GenericGenerator );
       generator.Build();

     //How can i get the _genericMatrix out from the generator instance

     }
 }

Since the _genericMatrix is a strong typed property in the GenericGenerator, i cant find a way to retrieve it back with the IGenerator.

Aucun commentaire:

Enregistrer un commentaire