473,609 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing abstract factory with generics without using reflection

3 New Member
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(Enti ty 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 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.
Mar 19 '10 #1
4 3309
tlhintoq
3,525 Recognized Expert Specialist
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
technomaget
3 New Member
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 Recognized Expert Specialist
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
technomaget
3 New Member
Thanks for the info.
Mar 19 '10 #5

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

Similar topics

3
15373
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 abstracct superclass, with an abstract method such that all subclasses that implement this method make that method static. basically, the abstract class declares an abstract method that should be static for all implementations (for example a factory...
17
6633
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. So I shall have an Abstract Base called 'Car' implemented by Toyota, Ford, and Buick. Further I'd like to enable to client to say Car *factory;
1
1821
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 activated objects, so I switched to client activated objects only to run into the next roadblock, doh! can't instantiate abstract classes... here is a simple example of the structure, im sure there is a way to do it to allow instantiation but I am...
6
2752
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 the Controller classe of a Sequence Diagram should be implemented as a private class within a component. However, my Programmer has said that since the ASP code lives outside the
33
3332
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 near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
6
3281
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 or Fooclass:IBasicClass and I want to be able to call form program something like this:
5
3667
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 event logs and other things as I think of them... I've seen two ways of doing this, the abstract class factory, or the interface... the module logic is fairly simple, watch something -> get the result -> notify / fix / and or log it... I've...
5
4566
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 constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
0
2670
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 looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
0
2420
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 that has 4 different types of users: Patron Promoter Admin
0
8139
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8579
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8555
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8232
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8408
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7024
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.