On 11 December 2015 at 10:42, Arjang Assadi <[email protected]> wrote:
> Anyone knows whats a good way to refer to a underlying object's array
> property in through the wrapper class?
>
> Suppose We have vlasses A and B where B is a an array propery of A as such
> Let W be Wrapper for A and Z be wrapper for B then
>
> class A
> {
>   public Property B[] DataArray{get;set;}
> }
>
> in the Wrapper class I have
>
> class W
> {
>   private A underlyingobject;
>
>   public Property Z[] DataArray{get;set;} //what is a way to implement this
> without copying the array of  B //into W and convert them all, instead
> having them yielded as if they were in W trough A and then //converted to Z
> ? these operations are read only for now.
>
> }
>
> class Z
> {
>   private B underlyingobject;
> }
>
> Any patterns relating to this in C#?

On 11 December 2015 at 13:18, Mark Hurd <[email protected]> wrote:
> Z could be (a composed version of) Lazy<B>, so the copying only
> happens on first access.

If I understand your question correctly, you want to process a
sequence of Zs, created element-wise from a sequence of Bs, without
holding the entire array of Zs in memory all at once.

Using Lazy<Z> [1] as in Mark's answer will let you defer the
conversion of individual Z cells until needed. However, the "spine" of
the array is still completely in memory. This might still be okay for
you if you only end up evaluating a subset of the cells. If you
evaluate the Z in every cell, however, then the entire array of Zs
will eventually be in memory.

Alternatively, if you know you are going to need every Z eventually,
but not all at once, then the solution is to stream them lazily
(similar to streaming chunks of a large file without loading the whole
file into memory). Arrays are not appropriate for lazy streaming. You
should look at IEnumerable instead. LINQ [2] and the C# "yield"
keyword [3] both make working with lazy IEnumerables a breeze.

If you want to stream lazily *and* convert lazily, it is perfectly
okay to combine the two approaches.

You mention that you may want to make Z writeable at some point. If
you proceed with that, you will be mixing lazy evaluation with
mutability, which is almost always a bad idea and a recipe for bugs.
If you intend to evaluate Bs and Zs lazily, I recommend that B, Z, and
the source B array are all immutable. Lazy evaluation often causes
things to execute at unpredictable times. Immutability is a guarantee
that you will get the same results no matter when (or on which thread)
the lazy value is forced. The C# "readonly" keyword [4] is used to
make immutable types.

[1] https://msdn.microsoft.com/en-us/library/dd642331.aspx
[2] https://msdn.microsoft.com/en-us/library/bb546168.aspx
[3] https://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx
[4] https://msdn.microsoft.com/en-us/library/acdd6hb7.aspx

--
Thomas Koster

Reply via email to