Designing 'value objects' in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Themel

    #1

    Designing 'value objects' in C#

    Hi,

    I'm currently trying to model a few complex data objects in C#. These
    have a huge number of attributes which are manipulated in large blocks
    by comparatively few API calls.

    For maintainability , I've come up with creating nested structs that
    contain the sets of attributes manipulated by one API call - so
    currently the way to access actual attributes is something like
    'theObject.Deta ils.Name' or 'theObject.Addi tionalDetails.T emplateID'.

    The obvious way to design the 'Details' and 'AdditionalDeta ils'
    properties would be as structs. However, this strikes me as an
    inefficient solution, since every time a client access
    'theObject.Deta ils.Something' a complete copy of the theObject.Detai ls
    struct is created, only to be discarded immediately.

    (If this is handled in a sane way by the runtime environment, it would
    make me happy - however, I didn't have time to get into IL enough to
    figure this out myself yet.)

    What I'm looking for is something equivalent to the C++ const& semantics
    for exposing structured properties. The only thing I've come up with is
    something like

    class Details
    {
    Details(string someAttribute, string someOtherAttrib ute)
    {
    SomeAttribute = someAttribute ;
    SomeOtherAttrib ute = someOtherAttrib ute ;
    }

    public readonly string SomeAttribute ;
    public readonly string SomeOtherAttrib ute ;
    }

    However, this is obviously a nightmare to maintain, since my structs
    contain about 20 members each.

    Any ideas on how to do this so that

    - access to object.Details. Something is possible without copying
    object.Details
    - there is no way for a caller to change object.Details. Something
    - I don't need to spend half of my time maintaining huge parameter
    lists

    ?

    thanks,
    --
    [*Thomas Themel*] The fundamental problem with vi is that it doesn't have
    [extended contact] a mouse and therefore you've got all these commands.
    [info provided in] - Bill Joy, <http://www.cs.pdx.edu/~kirkenda/joy84.html>
    [*message header*]
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Designing 'value objects' in C#

    Thomas,

    Like you said, you will create a copy every time. The only way around
    this is to create a class, but like you said, it seems like an nightmare to
    maintain.

    I would stil use the class route, using read-only properties, but I
    would write a program that would generate the code for you which you can
    compile. Using reflection, you can scan a type (your structure), and then
    have it spit out code which would expose the read-only properties which are
    copied over from the structure (through the constructor perhaps). If you
    want to get fancy, you can even write a static cast operator to your struct
    to convert from your class to an instance of the struct and embed it in the
    code.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Thomas Themel"
    <themel-microsoft.publi c.dotnet.langua ges.csharp-030915@isogsgle i.iwoars.net[color=blue]
    > wrote in message[/color]
    news:slrnbmb4r4 .rh2.themel-microsoft.publi c.dotnet.langua ges.csharp-030@eristoteles .iwoars.net...[color=blue]
    > Hi,
    >
    > I'm currently trying to model a few complex data objects in C#. These
    > have a huge number of attributes which are manipulated in large blocks
    > by comparatively few API calls.
    >
    > For maintainability , I've come up with creating nested structs that
    > contain the sets of attributes manipulated by one API call - so
    > currently the way to access actual attributes is something like
    > 'theObject.Deta ils.Name' or 'theObject.Addi tionalDetails.T emplateID'.
    >
    > The obvious way to design the 'Details' and 'AdditionalDeta ils'
    > properties would be as structs. However, this strikes me as an
    > inefficient solution, since every time a client access
    > 'theObject.Deta ils.Something' a complete copy of the theObject.Detai ls
    > struct is created, only to be discarded immediately.
    >
    > (If this is handled in a sane way by the runtime environment, it would
    > make me happy - however, I didn't have time to get into IL enough to
    > figure this out myself yet.)
    >
    > What I'm looking for is something equivalent to the C++ const& semantics
    > for exposing structured properties. The only thing I've come up with is
    > something like
    >
    > class Details
    > {
    > Details(string someAttribute, string someOtherAttrib ute)
    > {
    > SomeAttribute = someAttribute ;
    > SomeOtherAttrib ute = someOtherAttrib ute ;
    > }
    >
    > public readonly string SomeAttribute ;
    > public readonly string SomeOtherAttrib ute ;
    > }
    >
    > However, this is obviously a nightmare to maintain, since my structs
    > contain about 20 members each.
    >
    > Any ideas on how to do this so that
    >
    > - access to object.Details. Something is possible without copying
    > object.Details
    > - there is no way for a caller to change object.Details. Something
    > - I don't need to spend half of my time maintaining huge parameter
    > lists
    >
    > ?
    >
    > thanks,
    > --
    > [*Thomas Themel*] The fundamental problem with vi is that it doesn't[/color]
    have[color=blue]
    > [extended contact] a mouse and therefore you've got all these commands.
    > [info provided in] - Bill Joy,[/color]
    <http://www.cs.pdx.edu/~kirkenda/joy84.html>[color=blue]
    > [*message header*][/color]


    Comment

    • Thomas Themel

      #3
      Re: Designing 'value objects' in C#

      Nicholas Paldino [.NET/C# MVP] <nicholas.paldi no@exisconsulti ng.com>
      wrote on 2003-09-15:[color=blue]
      > I would stil use the class route, using read-only properties, but I
      > would write a program that would generate the code for you which you can
      > compile. Using reflection, you can scan a type (your structure), and then
      > have it spit out code which would expose the read-only properties which are
      > copied over from the structure (through the constructor perhaps). If you
      > want to get fancy, you can even write a static cast operator to your struct
      > to convert from your class to an instance of the struct and embed it in the
      > code.[/color]

      Thanks, I hadn't thought about this kind of stuff yet (being the old
      fashioned C++ person that I am). I think I'll hack up something to
      handle it this way.

      ciao,
      --
      [*Thomas Themel*] "That which is novice-friendly is too frequently
      [extended contact] expert-hostile, and vice versa."
      [info provided in] - Tom Christiansen, crossposting
      [*message header*] "GUIs considered harmful" to eight groups

      Comment

      Working...