473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. /// <summary>
  2.     /// Represents an adapter that can take different types of dataset objects and perform some basic functions.
  3.     /// </summary>
  4.     /// <typeparam name="TDataset">Any type of dataset.</typeparam>
  5.     /// <typeparam name="TTableAdapter">Any type of table adapter.</typeparam>
  6.     /// <typeparam name="TDataTable">Any type of datatable.</typeparam>
  7.     public class GridEntity<TDataset, TTableAdapter, TDataTable>  
  8.         where TDataset : DataSet, new()
  9.         where TTableAdapter : System.ComponentModel.Component, new()
  10.         where TDataTable : System.Data.DataTable, new()
  11.     {
  12.         protected TDataset dataset;
  13.         protected TTableAdapter mainTableAdapter;
  14.         protected TDataTable mainDataTable;
  15.         protected System.ComponentModel.IContainer components = null;
  16.         protected BindingSource mainBindingSource;
  17.  
  18.         /// <summary>
  19.         /// Returns the Binding Source Data Member.
  20.         /// </summary>
  21.         public virtual string BindingSourceDataMember
  22.         {
  23.             get{}
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Constructor
  28.         /// </summary>
  29.         public GridEntity(string datasetName)
  30.         {
  31.             dataset = new TDataset();
  32.             dataset.DataSetName = datasetName;
  33.             dataset.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
  34.             components = new System.ComponentModel.Container();
  35.             mainBindingSource = new System.Windows.Forms.BindingSource(this.components);
  36.             mainBindingSource.DataSource = dataset;
  37.             mainBindingSource.DataMember = BindingSourceDataMember;
  38.             mainTableAdapter = new TTableAdapter();
  39.             mainDataTable = new TDataTable();
  40.          }
  41.  
  42.  
  43.         /// <summary>
  44.         /// Returns the Main Dataset
  45.         /// </summary>
  46.         public TDataset MainDataSet
  47.         {
  48.             get
  49.             {
  50.                 return dataset;
  51.             }
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Returns the Main Table Adapter.
  56.         /// </summary>
  57.         protected internal TTableAdapter MainTableAdapter
  58.         {
  59.             get
  60.             {
  61.                 return mainTableAdapter;
  62.             }
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Returns the Main Data table.
  67.         /// </summary>
  68.         protected internal TDataTable MainDataTable
  69.         {
  70.             get
  71.             {
  72.                 return mainDataTable;
  73.             }
  74.         }
  75.  
  76.         /// <summary>
  77.         /// Returns the Main Binding Source.
  78.         /// </summary>
  79.         public BindingSource MainBindingSource
  80.         {
  81.             get
  82.             {
  83.                 return mainBindingSource;
  84.             }
  85.         }
  86.  
  87.         }
  88.  
  89.         public static GridEntity<TDataset, TTableAdapter, TDataTable> Instance()
  90.         {
  91.             return new GridEntity<TDataset, TTableAdapter, TDataTable>("MyDataset");
  92.         }
  93.    }
  94.  
  95.  
Then I have a class inherited from GridEntity:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Data;
  6.  
  7. public class ChildGridEntity : GridEntity<MyNamespace.Data.MyDataset, MyNamespace.MyData.MyDatasetTableAdapters.ChildAdapter TableAdapter, MyNameSpace.Data.MyDataset.ChildDataTable>
  8.     {
  9.  
  10.        some specific implementation...
  11.     }
  12. }
  13.  
Then I am trying to instantiate a generic class and pass it as a parameter:

Expand|Select|Wrap|Line Numbers
  1. public enum Entity
  2. {Child,
  3. Child2,
  4. ...
  5. }
  6.  
  7. public class GridEntityAbstractFactory()
Here is where I am having problems:

public static GridEntity(Entity entity)
Expand|Select|Wrap|Line Numbers
  1. {
  2.    switch (entity)
  3.     {
  4.       Case Entity.Child1: 
  5.             ChildGridEntity gridEntity=new ChildGridEntity()
  6.             break;
  7.       ...
  8.     }
  9. }
  10.  
And finally I can't seem to do this from the client:

Expand|Select|Wrap|Line Numbers
  1. public class Client
  2. {
  3.  
  4. public Client(
  5. {
  6. IntializeComponents(gridEntity);
  7. }
  8.  
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.
Mar 19 '10 #1
4 3294
tlhintoq
3,525 Expert 2GB
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.
Mar 19 '10 #2
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!
Mar 19 '10 #3
tlhintoq
3,525 Expert 2GB
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
[IMG]http://files.me.com/tlhintoq/j61pjn[/IMG]
Mar 19 '10 #4
Thanks for the info.
Mar 19 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an...
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
1
by: f00sion | last post by:
I found a good tutorial of how to supply the objects without having the implementation files on the client. This was working great until I realized that I couldnt use any constructors with server...
6
by: Martyn Lawson | last post by:
Hi, I am currently working as an Analyst on a .NET Web Project using ASP.NET and C#.NET. I have a couple of, at least what should be, quick questions: 1. My understanding of UML says that...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
6
by: Tamir Khason | last post by:
I have some classes that basicly do the same things but different way. There are no default constructors in those classes each constructor should recieve same value So Fooclass:MyBasicClass...
5
by: Michael McCarthy | last post by:
I want to develop plugin support for a system.montitor module I am working on. A lot of the modules will do mostly interop stuff for an older system, but I want to use it myself as well to monitor...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
0
by: Sean Chambers | last post by:
This is a fairly simple architecture question, just can't seem to wrap my head around the answer. I am using NHibernate 1.2 from the trunk to leverage generics support. I have a User Table...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.