CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    Join Date
    Aug 2007
    Posts
    50

    Basic inheritance question

    I've created an abstract class which I intend to use as a base for a bunch of data access objects. I am noticing that all of my inherited classes require identical constructors, so I'm wondering if there is a way I can define some constructors in the base class, such to save me from having to define them in every inherited class.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Basic inheritance question

    Parameterized constructors must be declared for the actual type (if they need parameterized constructors)...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Aug 2007
    Posts
    50

    Re: Basic inheritance question

    Yes, parameterized. So no choice but to write the same functions for every class?

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Basic inheritance question

    Quote Originally Posted by drowned View Post
    Yes, parameterized. So no choice but to write the same functions for every class?
    Yes...but...It would make me question the architecture and object model. Remember "Resource Allocation Is Initialization", so your design seems to indicate multiple derived classes (ie conceptually different entities which have an "IS-A" relationship with a common base), but have NO differences in what their state is at time of creation, and are therefore identical in terms of state....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Aug 2007
    Posts
    50

    Re: Basic inheritance question

    They handle different databases, all using different functions. They are the same in that they all use a private database member and a few common strings. I would appreciate any suggestions of better ways to do this.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Basic inheritance question

    Quote Originally Posted by drowned View Post
    They handle different databases, all using different functions. They are the same in that they all use a private database member and a few common strings. I would appreciate any suggestions of better ways to do this.
    I would need many more details (PM me if you wish) before making a "suggestion", but a few points.

    1) Is it valid to declare a variable of the base type, assign it to each of the various derived types, and invoke every public method/property? If not, then inheritance is inappropriate.

    2) Are all of the public methods depenand on at least one non-public portion? If not then they may not be appropriate as member functions.

    Additionally a "Builder/Decorator" combinational pattern can be useful for situations such as yours seems to be.

    You then only have one concrete accessible class, specializations are injected into the primary class (Decorator Pattern) by the Builder (A builder is a Factory with post construction initialization.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Aug 2007
    Posts
    50

    Re: Basic inheritance question

    Hmm, maybe inheritance is not what I want then. Here is basically what I'm doing:

    This is the base class I'm using:
    Code:
    public abstract class DBAccessBase
        {
            protected Database _db;
            protected string _conn;
            protected string _errorMessage;
    
            #region Properties
            public Database DB
            {
                get
                {
                   ...
                }
                set { this._db = value; }
            }
            public string ConnectionKey
            {
                get ... set ...
            }
            public string ErrorMessage
            {
                get ... set ...
            }
            #endregion
        }
    This is part of one of my data access objects:
    Code:
    public class DataFeedMemberDAO : DBAccessBase
        {
            public DataFeedMemberDAO()
            {
                this._conn = System.Configuration.ConfigurationSettings.AppSettings["connectionKey"];
                this._db = DatabaseFactory.CreateDatabase(this._conn);
            }
    
            public DataFeedMemberDAO(string connectionKey)
            {
                this._conn = connectionKey;
                this._db = DatabaseFactory.CreateDatabase(connectionKey);
            }
    
            public int InsertDataMember(a bunch of parameters)
            {
                DbCommand dbc = this._db.GetStoredProcCommand("InsertDataFeedMember");
    
                this._db.AddInParameter(dbc, etc...)
                //add all parameters
    
                return this._db.ExecuteNonQuery(dbc);
            }
    
        }
    I have a handful of other objects which inherit DBAccessBase and perform similarly.

  8. #8
    Join Date
    Aug 2007
    Posts
    50

    Re: Basic inheritance question

    Actually it looks like what I was trying to do is possible, I guess this is not a very well known feature of C#.
    Code:
    public class DerivedType : BaseType
        {
            public DerivedType() : base()
            {
            }
    
            public CompaniesDAO(string connectionKey) : base(connectionKey)
            {
            }
         }

  9. #9
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Basic inheritance question

    Quote Originally Posted by drowned View Post
    Actually it looks like what I was trying to do is possible, I guess this is not a very well known feature of C#.
    Code:
    public class DerivedType : BaseType
        {
            public DerivedType() : base()
            {
            }
     
            public CompaniesDAO(string connectionKey) : base(connectionKey)
            {
            }
         }
    I have never seen to be able to have two constructors in one cllass which doesn't have the same name, you may have lots of constructors in one class with different members but they all in your case needs to be
    Code:
    public class DerivedType:baseType{
       public DerivedType():base(){
         ....
       }
       public DerivedType(string connKey):base(connKey){
        ....
     
       }
       protected DerivedType(int internalUsedData, string connKey) : base(connKey){
       ...
      }
    }
    So it doesn't depend if your ctors are public, protected or private but all ctors needs to be named with the name of the derivedType. The amount of parameters you can hand to the base class depends on which ctors the base class already has.

    In your case, as already said by CPUWizard a decorator pattern IMHO should do its work. A decorator wraps your 'core'-class with a thiny wrapper which contaíns the needed methods. This way different wrappers can have totally different methods.

    If you need to have the same methods but the code inside them differs from class to class you can use Interfaces This can make your code very flexible as mybe you use some common names like
    GetInsertCommand and the code to get or create it may depend on the fact in which class your interface is used.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  10. #10
    Join Date
    Aug 2007
    Posts
    50

    Re: Basic inheritance question

    I have never seen to be able to have two constructors in one cllass which doesn't have the same name, you may have lots of constructors in one class with different members but they all in your case needs to be
    Ah... I forgot to change the name of my constructor. "CompaniesDAO" is the real name of the class in my code. I meant to call it DerivedType. Obviously all of my constructors have the same name.

    As far as a decorator pattern goes... I do not think that will do the job I want. I do not want to do something like "GetInsertCommand" or "GetSelectCommand" because it seems that would mean one of two things: either I have a single generic insert/update/select/delete command for each of my tables, or I have to pass in SQL code from the business layer. As it stands right now, I have every necessary data access function explicitly mapped out. For example, there are instances in my application where I need to update a field in a database or select some rows from multiple tables. I have one specific function that serves each of those purposes. In my business object, the method might be something like...
    Code:
    DataAccess.UpdateSingleField(int, string);
    DataAccess.SelectMultipleRows(int);
    Clearly this is a very generalized example, but my point is that... if I ever change the way data is accessed, all I have to do is change the specific functions in my data access layer, I never need to change queries in the business layer. If there is a way that I can use a decorator pattern, or any other pattern, to do this in a more elegant fashion, and to never pass SQL code from the business layer, I would greatly appreciate being pointed in that direction.

  11. #11
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Basic inheritance question

    Quote Originally Posted by drowned View Post
    ... I never need to change queries in the business layer. If there is a way that I can use a decorator pattern, or any other pattern, to do this in a more elegant fashion, and to never pass SQL code from the business layer, I would greatly appreciate being pointed in that direction.
    You obviously dont use DataSet, DataTable, DataAdapter and DataRow classes.
    Have you had an eye on this possibilities too ? Very powerful, very easy to read out data, also getting fieldNames fieldinformation like fieldlength in case of strind data.
    Last edited by JonnyPoet; February 11th, 2009 at 01:19 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  12. #12
    Join Date
    Aug 2007
    Posts
    50

    Re: Basic inheritance question

    I'm not sure why that's obvious, because I do use DataSets, DataTables, and DataRows. I don't use typed datasets, if that's what you mean. I have used them in the past but I don't anymore. I am trying to focus on N-tier architecture with very strict separation of my layers, and typed datasets in my experience just don't lend themselves well to this approach.

  13. #13
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Basic inheritance question

    Quote Originally Posted by drowned View Post
    I'm not sure why that's obvious, because I do use DataSets, DataTables, and DataRows. ....
    I see. I got the idea you would read / write all your data by use of Readers and writers with totally different classes for each table. This comes up because anywhere you wrote
    They handle different databases, all using different functions
    . So I got the idea you would do it accessing each field by its name, having lots of classes for different tables and different needs.
    Rereading the whole thread now I just realized that the basic question
    ...I am noticing that all of my inherited classes require identical constructors, so I'm wondering if there is a way I can define some constructors in the base class, such to save me from having to define them in every inherited class.
    is very simple to answer... Yes you can do all common values which are needed in each inheritance into the abstract class and also having different constructors there with the parameters you need just in the way of the mistyped example you have shown (That one, where I reclamated the different constructor names ) Yes this is very usual. You also can have virtual methods there and override them in the derived classes if you need so.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  14. #14
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Basic inheritance question

    Have you looked into either:

    1) Linq-To-SQL
    2) Entity Framework
    3) Third Party ORM? (e.g. nHibernate)

    ??????
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  15. #15
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Basic inheritance question

    i don't see any problem. by the way, if you have different kinds of objects in a layer you can group them into different base class. so it is okay to have more than one base class in a layer.
    Busy

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured