0
Answer

Implementing abstract factory without using reflection

Timur Rozenfeld

Timur Rozenfeld

14y
4.7k
1
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.

/// <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:

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.ChildAda pter 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(Entity entity)
{
switch (entity)
{
Case Entity.Child1:
ChildGridEntity gridEntity=new ChildGridEntity()
break;
...
}
}

And finally I can't seem to do this from the client:

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 InitializeComponents 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.