Created
December 9, 2010 13:15
-
-
Save gshutler/734701 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface IHeader | |
| { | |
| string Value { get; } | |
| IEnumerable<string> Values { get; } | |
| } | |
| public sealed class Header : IHeader | |
| { | |
| readonly IEnumerable<string> values; | |
| readonly string value; | |
| public Header(IEnumerable<string> values) | |
| { | |
| this.values = values.ToArray(); | |
| value = this.values.FirstOrDefault() ?? string.Empty; | |
| } | |
| public string Value | |
| { | |
| get { return value; } | |
| } | |
| public IEnumerable<string> Values | |
| { | |
| get { return values; } | |
| } | |
| } |
Author
It could be that trying to wedge my idea into a generic container is a bad idea. I'll write some code and try some ideas out. It could be I go back to IDictionary<string, IEnumerable<string>> or it could be I end up at a specialized IHeaderCollection.
I'm against being reliant on extension methods for completely standard interactions. They aren't discoverable if you don't have the right namespace imported and force you to use methods rather than properties or indexes.
Thanks for your feedback by the way, it's much appreciated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll also restate my latest reply then :-)
Given the host header, one could use
Headers["host"].Single(). To me, this reads out clear and will neither yield anullfrom the dictionary or anInvalidOperationExceptionfrom the sequence, if the requirement is to always provide a single item enumeration (I'd actually prefer it to yield anull, but that's another discussion).So, whether you have to handle a
nullwill be a detail of the implementation in both cases.I think we pretty much agree on what is best and what is most practical. I guess I'm just more idealistic about how I would design a framework. As an example I'd prefer extension methods for making it easier rather than introducing multiple ways of achieving the same in the interface.