简体   繁体   中英

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.

You need to create public property for this. As there is no way you can access private variable using object of any class.

I suppose, Build should return the created matrix, so it becomes

public interface IGenerator
{
    object Build();
}

and

public class GenericGenerator : MatrixGeneratorBase, IMatrixGenerator<GenericItemRow>
{
    public object Build()
    {
         BuildMatrixTemplate();

         var genericMatrix = new GenericMatrix<GenericBudgetValueMatrixRow>();

         genericMatrix = //Some logic here

         return genericMatrix;
     }

}

Like Haukinger said: return the genericMatrix would be the simplest way, but I would define the Generator generic too, because you would want to build different Matrices. So you can do this:

public interface IGenerator
{
    IMatrix Build();
}

public class GenericGenerator<TRow> : MatrixGeneratorBase, 
IMatrixGenerator<GenericItemRow> where TRow : GenericItemRow
{

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

    public IMatrix Build()
    {
         BuildMatrixTemplate();

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

          _genericMatrix = //Some logic here
          return _genericMatrix;

     }

}

public interface IMatrix
{
  //Properties that you need to use.
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM