Implementing abstract factory with generics without using reflection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • technomaget
    New Member
    • Mar 2010
    • 3

    Implementing abstract factory with generics without using reflection

    Hi,

    I am trying to implement a multi-purpose user control with generics. I can develop the generic class, and can instantiate concrete child classes inherited from the genric class, but have problems passing the instantiated concrete classes to methods.

    Code:
    /// <summary>
        /// Represents an adapter that can take different types of dataset objects and perform some basic functions.
        /// </summary>
        /// <typeparam name="TDataset">Any type of dataset.</typeparam>
        /// <typeparam name="TTableAdapter">Any type of table adapter.</typeparam>
        /// <typeparam name="TDataTable">Any type of datatable.</typeparam>
        public class GridEntity<TDataset, TTableAdapter, TDataTable>  
            where TDataset : DataSet, new()
            where TTableAdapter : System.ComponentModel.Component, new()
            where TDataTable : System.Data.DataTable, new()
        {
            protected TDataset dataset;
            protected TTableAdapter mainTableAdapter;
            protected TDataTable mainDataTable;
            protected System.ComponentModel.IContainer components = null;
            protected BindingSource mainBindingSource;
            
            /// <summary>
            /// Returns the Binding Source Data Member.
            /// </summary>
            public virtual string BindingSourceDataMember
            {
                get{}
            }
    
            /// <summary>
            /// Constructor
            /// </summary>
            public GridEntity(string datasetName)
            {
                dataset = new TDataset();
                dataset.DataSetName = datasetName;
                dataset.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
                components = new System.ComponentModel.Container();
                mainBindingSource = new System.Windows.Forms.BindingSource(this.components);
                mainBindingSource.DataSource = dataset;
                mainBindingSource.DataMember = BindingSourceDataMember;
                mainTableAdapter = new TTableAdapter();
                mainDataTable = new TDataTable();
             }
    
     
            /// <summary>
            /// Returns the Main Dataset
            /// </summary>
            public TDataset MainDataSet
            {
                get
                {
                    return dataset;
                }
            }
    
            /// <summary>
            /// Returns the Main Table Adapter.
            /// </summary>
            protected internal TTableAdapter MainTableAdapter
            {
                get
                {
                    return mainTableAdapter;
                }
            }
    
            /// <summary>
            /// Returns the Main Data table.
            /// </summary>
            protected internal TDataTable MainDataTable
            {
                get
                {
                    return mainDataTable;
                }
            }
    
            /// <summary>
            /// Returns the Main Binding Source.
            /// </summary>
            public BindingSource MainBindingSource
            {
                get
                {
                    return mainBindingSource;
                }
            }
    
            }
    
            public static GridEntity<TDataset, TTableAdapter, TDataTable> Instance()
            {
                return new GridEntity<TDataset, TTableAdapter, TDataTable>("MyDataset");
            }
       }
    Then I have a class inherited from GridEntity:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Data;
    
    public class ChildGridEntity : GridEntity<MyNamespace.Data.MyDataset, MyNamespace.MyData.MyDatasetTableAdapters.ChildAdapter TableAdapter, MyNameSpace.Data.MyDataset.ChildDataTable>
        {
    
           some specific implementation...
        }
    }
    Then I am trying to instantiate a generic class and pass it as a parameter:

    Code:
    public enum Entity
    {Child,
    Child2,
    ...
    }
    
    public class GridEntityAbstractFactory()
    Here is where I am having problems:

    public static GridEntity(Enti ty entity)
    Code:
    {
       switch (entity)
        {
          Case Entity.Child1: 
                ChildGridEntity gridEntity=new ChildGridEntity()
                break;
          ...
        }
    }
    And finally I can't seem to do this from the client:

    Code:
    public class Client
    {
    
    public Client(
    {
    IntializeComponents(gridEntity);
    }
    The purposes is for me to have my factory create child objects which can then be passed as needed like in the method InitializeCompo nents to do the same tasks irrespective of which specific child object they are. I know this can be done with reflection, but I am trying to avoid doing that. If you see a way of making this work, or another way of doing this, it would be greatly appreciated.

    Thanks.
    Last edited by tlhintoq; Mar 19 '10, 04:54 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • technomaget
      New Member
      • Mar 2010
      • 3

      #3
      Hi,

      I am not sure I understand your answer. I used the code tags and the code seems to be embedded correctly. When I looked at my original post, I didn't see anything wrong with the formatting. Could you clarify what doesn't look right and what you mean by wrapping the code tags with # , as I thought I just needed to put the code tags around my code and that would take care of it.

      Thanks!

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Your code tags were <code>
        instead of [code]

        The reason it looks right is that I went in and edited the tags (if you look at the bottom of the post it will say
        Last edited by tlhintoq; 1 Hour Ago at 09:54 AM. Reason: [code] ...Your code goes between code tags [code]
        I was just pointing out instead of typing [code] you can just select a block then hit the '#' button in the format bar. As well as giving you a link to all the other formatting tags that aren't so well known like HIGHLIGHT or how to insert an image without it being a thumbnail.
        [IMGNOTHUMB]http://files.me.com/tlhintoq/j61pjn[/IMGNOTHUMB]
        instead of

        Comment

        • technomaget
          New Member
          • Mar 2010
          • 3

          #5
          Thanks for the info.

          Comment

          Working...