Designing 'value objects' in C#

  • Thread starter Thread starter Thomas Themel
  • Start date Start date
T

Thomas Themel

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.Details.Name' or 'theObject.AdditionalDetails.TemplateID'.

The obvious way to design the 'Details' and 'AdditionalDetails'
properties would be as structs. However, this strikes me as an
inefficient solution, since every time a client access
'theObject.Details.Something' a complete copy of the theObject.Details
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 someOtherAttribute)
{
SomeAttribute = someAttribute ;
SomeOtherAttribute = someOtherAttribute ;
}

public readonly string SomeAttribute ;
public readonly string SomeOtherAttribute ;
}

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,

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]
- (e-mail address removed)

"Thomas Themel"
<themel-microsoft.public.dotnet.languages.csharp-030915@isogsglei.iwoars.net
wrote in message 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.Details.Name' or 'theObject.AdditionalDetails.TemplateID'.

The obvious way to design the 'Details' and 'AdditionalDetails'
properties would be as structs. However, this strikes me as an
inefficient solution, since every time a client access
'theObject.Details.Something' a complete copy of the theObject.Details
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 someOtherAttribute)
{
SomeAttribute = someAttribute ;
SomeOtherAttribute = someOtherAttribute ;
}

public readonly string SomeAttribute ;
public readonly string SomeOtherAttribute ;
}

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,
[*message header*]
 
Nicholas Paldino said:
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.

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,
 
Back
Top