Results 1 to 10 of 10

Thread: Constructor style preference

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Constructor style preference

    Given the following auto generated code:-

    VB Code:
    1. '------------------------------------------------------------------------------
    2. ' <auto-generated>
    3. '     This code was generated by a tool.
    4. '     Runtime Version:4.0.30319.42000
    5. '
    6. '     Changes to this file may cause incorrect behavior and will be lost if
    7. '     the code is regenerated.
    8. ' </auto-generated>
    9. '------------------------------------------------------------------------------
    10.  
    11. Option Strict Off
    12. Option Explicit On
    13.  
    14.  
    15. Namespace test
    16.    
    17.     Partial Public Class Duncan_s_Class_Test
    18.        
    19.         #Region "Private members"
    20.         Private _Email As String
    21.        
    22.         Private _Height__cm As Integer
    23.        
    24.         Private _Blood_Alcohol As Decimal
    25.         #End Region
    26.        
    27.         Sub New(ByVal DuncansClassIn As IDuncansClass)
    28.             MyBase.New
    29.             _Email = DuncansClassIn.Email
    30.             _Height__cm = DuncansClassIn.Height__cm
    31.             _Blood_Alcohol = DuncansClassIn.Blood_Alcohol
    32.         End Sub
    33.        
    34.         Sub New(ByVal Email_In As String, ByVal Height__cm_In As Integer, ByVal Blood_Alcohol_In As Decimal)
    35.             MyBase.New
    36.             _Email = Email_In
    37.             _Height__cm = Height__cm_In
    38.             _Blood_Alcohol = Blood_Alcohol_In
    39.         End Sub
    40.        
    41.         '''<summary>
    42.         '''Email address of the user
    43.         '''</summary>
    44.         Public ReadOnly Property Email() As String
    45.             Get
    46.                 Return _Email
    47.             End Get
    48.         End Property
    49.        
    50.         '''<summary>
    51.         '''Recorded height in cm
    52.         '''</summary>
    53.         Public ReadOnly Property Height__cm() As Integer
    54.             Get
    55.                 Return _Height__cm
    56.             End Get
    57.         End Property
    58.        
    59.         '''<summary>
    60.         '''Recorded blood/alcohol
    61.         '''</summary>
    62.         Public ReadOnly Property Blood_Alcohol() As Decimal
    63.             Get
    64.                 Return _Blood_Alcohol
    65.             End Get
    66.         End Property
    67.     End Class
    68. End Namespace

    Which constructor type do you prefer - individually explicitly named properties or instance of interface?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Constructor style preference

    You mean the difference between _Email and Me.Email?

    Traditionally I use the backing variable, _Email in this case... unless there is a reason to use the property (such as data dependance or logic that needs to be executed.)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Constructor style preference

    Between:-
    VB Code:
    1. Sub New(ByVal DuncansClassIn As IDuncansClass)
    2.             MyBase.New
    3.             _Email = DuncansClassIn.Email
    4.             _Height__cm = DuncansClassIn.Height__cm
    5.             _Blood_Alcohol = DuncansClassIn.Blood_Alcohol
    6.         End Sub
    and
    VB Code:
    1. Sub New(ByVal Email_In As String, ByVal Height__cm_In As Integer, ByVal Blood_Alcohol_In As Decimal)
    2.             MyBase.New
    3.             _Email = Email_In
    4.             _Height__cm = Height__cm_In
    5.             _Blood_Alcohol = Blood_Alcohol_In
    6.         End Sub

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Constructor style preference

    Neither. I wouldn't have individual members for email, height and blood alcohol but would simply have an IDuncansClass member. So my Duncan_s_class_test would receive in an IDuncansClass but it wouldn't be opening it up to get at the individual members.

    I'm not sure why the individual members have been broken out at all. Surely that's just breaking encapsulation. Am I missing something here?
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: Constructor style preference

    Blood Alcohol as decimal is a good call. If you could measure mine in ints, it would result in an overflow.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Constructor style preference

    Actually... it depends... I might do both... depends on how I plan to call it... will I always have an IDuncansClass object I can pass in? It kind of depends on the context and what's expected as the minimum needed for the class.

    I get what Funky suggested, I'm not sure I completely agree... I create classes that are often based on DataRows... but that doesn't mean I'm going to make a DataRow property and expose the data that way. Otherwise what's the point. However, given his example, it makes sense since then the data will remain in synh (what if the address of one instance is changed? it won't propogate the value to the other class.)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Constructor style preference

    I create classes that are often based on DataRows
    That's a good point I hadn't considered. If this class is abstracting an iDuncansClass then I'd go with the second constructor (you'd have to because the first exposes an iDuncansClass).

    I think I'd probably still be storing the values in a private iDuncansClass member though. Assuming iDuncansClass has some functionality (so it's more than just a collection of values) then, if I stored the values individually, I would need to construct a new iDuncansClass from the individual values each time I wanted to use that functionality.

    If I don't want to expose iDuncansClass publically then I can still expose it's members publically through the properties of Duncan_s_Class_Test. E.g.
    VB Code:
    1. Public ReadOnly Property Email() As String
    2.             Get
    3.                 Return myIDuncansClass._Email
    4.             End Get
    5.         End Property

    I still can't help feeling I'm missing something on this one though.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Constructor style preference

    IF taken in tandem with Merri's recent posts, he's working on a code generator. So my guess is that he's working out how to create the initial codebase to then work form.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Constructor style preference

    Name:  dsl_design_001.png
Views: 126
Size:  75.1 KB

    Yeah - each of the entities in the diagram becomes an interface and partial class pair - the end goal being that the tool builds your CQRS framework and you can add in any custom functionality.

    Since there is no functional difference between the styles I have added an option to choose which to use:
    VB Code:
    1. ''' <summary>
    2.     ''' What type of constructor to use in the generated classes
    3.     ''' </summary>
    4.     ''' <remarks>
    5.     ''' This could be a per-model rather than per-user preference
    6.     ''' </remarks>
    7.     Public Enum ConstructorPreferenceSetting
    8.         ''' <summary>
    9.         ''' (Default) Generate both an interface derived constructor and a parameters derived constructor
    10.         ''' </summary>
    11.         GenerateBoth = 0
    12.         ''' <summary>
    13.         ''' Only generate an interface derived constructor
    14.         ''' </summary>
    15.         InterfaceOnly = 1
    16.         ''' <summary>
    17.         ''' Only derive a parameters derived cosntructor
    18.         ''' </summary>
    19.         ParametersOnly = 2
    20.     End Enum

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Constructor style preference

    On that note... sometimes I do both... as I mentioned I often will use a DataRow as my class source, but I may not always have a datarow. Borrowing form another thread, take the Duck example, if I'm loading ducks from a database I'll have a datarow... but when creating a new duck, I won't... so I'll have a base constructor (no params), one with a data row, and one with parameters - I don't expose ALL properties as parameters, but I'll expose the non-optional kind.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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