On Thu, 13 Nov 2008 06:43:01 -0800, DabblerNL <an**@newsgroup.nospam>
wrote:
[...]
The .Net Methods OrderBy and ThenBy do exactly that.: ThenBy works on a
IOrderedEnumerable<T>, which is the return value of the OrderBy Method.
As
far as I know there is no other way to create a IOrderedEnumerable<T>
than
vby using OrderBy. I like that approach but are wondering how it is done.
There's nothing magic about it. In fact, it's not true that "there is no
other way to create an IOrderedEnumerable<TElement>". You can implement
the interface yourself if you like, and pass an instance of that
implementation to the methods that take it. As long as your
implementation behaves correctly, then everything will work fine. But, if
your implementation does not, there's nothing in LINQ that can enforce
that and ensure correct behavior in spite of it.
[...] I would rather have the Seat() (extension) method
of the List<Cellreturn a List<SeatedCellsand have the
AdaptToSeatingList
method accept this type as an argument. This is not difficult to
achieve, but
I would like to ensure that the only way to get a List<SeatedCellsis
through the List<Cell>.Seat() method.
How??
What you're asking for is not literally possible. Any code that knows the
SeatedCells type can create a List<SeatedCells>.
You can limit the opportunity for other code to do that by making the type
private nested within your outer class (and it's possible that would be a
good idea anyway, if all of this logic is private to the outer class), but
that won't stop code from within your outer class from misusing the type.
But note that this isn't really like the code you originally posted. In
particular, the type being passed around isn't your own type, but rather a
public type containing your own type. If the type being passed around was
in fact your own type, then you could provide better guarantees, by
putting the logic required to apply whatever state you want into the
constructor itself. That would make it impossible to get an instance of
that type without initializing it in just the way you want.
So, one approach you might do is wrap the List<Cellsinside a type of
your own making. For it to be truly protected, you would have to copy in
the original List<Cells>, otherwise some caller could potentially modify
the List<Cellspassed to the constructor after the fact. Your wrapper
class could contain the implementation of your "AdaptToSeatingList()"
method, or the wrapper class could provide whatever access to the list is
required by the "AdaptToSeatingList()" method (e.g. you could implement
the IList<Cellsinterface, and then have the "AdaptToSeatingList()"
method work on an IList<Cells>, rather than the specific List<Cells>).
Pete