in

CodeSmith Community

Your Code. Your Way. Faster!

Current C# Collection Templates [1.6.0]

Last post 07-01-2004 2:11 PM by cnahr. 6 replies.
Page 1 of 2 (26 items) 1 2 Next >
Sort Posts: Previous Next
  • 01-15-2004 3:35 PM

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    Current C# Collection Templates [1.6.0]

    Attached to this post is the current version of the C# Collection Templates.
     
    These templates ship with CodeSmith and are installed in the subdirectory Samples\Collections, although the CodeSmith version is possibly older than the current version posted here.
     
    Please post bug reports & enhancement requests in this thread, or in this forum.
     
    Current version: 1.6.0 (released on 01 July 2004)

    Post Edited (Chris Nahr) : 7/1/2004 2:12:28 PM GMT

    • Post Points: 325
  • 01-15-2004 3:38 PM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: Current C# Collection Templates [1.6.0]

    What's new in version 1.4.1: I've added a "unique" wrapper to the ArrayList template, as requested by Eric.
     
    The wrapper is based on an idea posted by Joost in this forum but my implementation is somewhat different. It's strictly analogous to the standard wrappers (sync, read-only) in that there's only one property (IsUnique) and one method (Unique) that are specific to the new wrapper. Any operations that the wrapper doesn't like (adding duplicate elements) causes a documented NotSupportedException in the existing methods.
     
    Anyone who's interested in this feature, please download & try out the new version, and let me know if there are any problems with it.
    • Post Points: 5
  • 01-15-2004 4:33 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,185
    • Points 922,065

    RE: Current C# Collection Templates [1.6.0]

    Chris,

    Wow, that was fast! :-) Thanks!

    Eric J. Smith
    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect
    • Post Points: 5
  • 01-16-2004 4:45 PM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: Current C# Collection Templates [1.6.0]

    Well, I wasn't quite finished yet. :)

    I clarified a few comments in the version I just uploaded. If you don't find any problems that should be the version to include in the next CodeSmith release.

    Cheers, Chris
    • Post Points: 5
  • 01-16-2004 5:38 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,185
    • Points 922,065

    RE: Current C# Collection Templates [1.6.0]

    Chris,

    You know what else I was thinking would be a great addition to these template? :-) I think it would be extremely useful if the template would allow you to specify properties on the collection item to create an indexer for. You would also generate a IndexOf and Contains for each property that you wanted to be able to index by. This would be extremely useful for objects that have an ID or Name or something that you want to look them up by. You would just return the first object that matched the criteria and wouldn't need to worry about multiple items matching.

    What do you think?

    Eric J. Smith
    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect
    • Post Points: 5
  • 01-16-2004 5:52 PM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: Current C# Collection Templates [1.6.0]

    Eric,

    Are you suggesting some kind of dictionary collection whose key is not a separate object but a property of the "value" part of the key/value pair? That sounds interesting but I suspect many pitfalls and a lot of work there... personally I would be leaning towards a standard ArrayList instead, with a simple access method in whatever application class manages that collection.

    Chris
    • Post Points: 5
  • 01-16-2004 6:04 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,185
    • Points 922,065

    RE: Current C# Collection Templates [1.6.0]

    Chris,

    I've added these to my ArrayList template and the changes are very simple. It's sort of a dictionary, but it's really just a way to access an item in the collection by something other than it's index. A lot of times people store their custom entity objects in a collection and these entity objects have an ID or something that is always going to be unique in the collection. So you simply provide a way of accessing them by this item. One problem I can think of though is that if the property that you want to allow to be indexed on is an int then it couldn't just be an overload of the indexer. So maybe you would have to have something like a GetItemBy<%= PropertyName %> method or something. Or maybe you can think of some better design. But the basic idea is that sometimes you want to be able to locate an item in the collection by something other than it's index.

    Thanks,
    Eric J. Smith
    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect
    • Post Points: 5
  • 01-16-2004 6:08 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,185
    • Points 922,065

    RE: Current C# Collection Templates [1.6.0]

    Chris,

    A good example of this is the schema objects that I have added a this[string name] indexer to and a this[owner, name] indexer to. All they do is allow easy access to finding an item in the collection by the name of the schema object so you can do things like:

    tableSchema.Columns["MyColumnName"].DataType

    Thanks,
    Eric J. Smith
    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect
    • Post Points: 5
  • 01-17-2004 5:24 PM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: Current C# Collection Templates [1.6.0]

    Eric,
     
    Okay, you've convinced me that this enhancement makes sense. smile 
     
    My first implementation (just posted) is a bit simpler, though -- no indexer, just three methods: GetByKeyName, ContainsKeyName, and IndexOfKeyName. There are two new optional template properties, KeyName and KeyType, that refer to the name & type, respectively, of the ItemType field or property that serves as a key. The methods are only generated if both properties are set. The KeyType must have a valid == operator defined but that shouldn't be a problem for types that are used as keys.
     
    I'm not sure I like the idea of an indexer based on KeyName -- first of all, there's the issue that the KeyType can't be Int32, as you said, and shouldn't really be any numerical type to avoid confusion; but then there's also the problem of setting the indexer property, which would require testing if the specified index expression and the corresponding property of the new element are identical. Also, what about assigning null values to the collection? Or indexing with a null key?
     
    Cheers, Chris
    • Post Points: 5
  • 01-17-2004 6:06 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,185
    • Points 922,065

    RE: Current C# Collection Templates [1.6.0]

    Chris,

    This looks great. This should be very helpful.

    As far as the indexer, what I do is just make the this[string name] and this[string owner, string name] indexers read-only. They can only be used to retrieve items and can't be assigned to. If I am unable to find a matching item, I return null. I think what you have works perfect and should solve this need for 99% of the cases.

    Thanks,
    Eric J. Smith
    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect
    • Post Points: 5
  • 01-18-2004 10:43 AM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: Current C# Collection Templates [1.6.0]

    Eric,
     
    I apologize in advance for the new update... when I tried to integrate the new ArrayList in my own projects, I found that I really needed to make the ArrayList key access as much as possible like the key access of the regular dictionary classes so that they would be interchangeable.
     
    That means I renamed the key access methods to ContainsKey, GetByKey, and IndexOfKey, regardless of the actual key name. Supporting multiple different keys, as in your Item[owner, name] indexer, is therefore out of the question -- at least in the line of templates that I'm willing to support. Of course, you could always branch off another specialized ArrayList template tailored to your needs.
     
    On the other hand, the key indexer is in -- it's controlled by a new KeyIndexer on/off property so that we don't get an unwanted clash with the existing Int32 indexer. It's just a getter, as you said, so it's basically syntactical sugar for GetByKey.
     
    I've also added some more comments to the template and to the ReadMe file, explaining the new features, and KeyName now defaults to KeyType as per the .NET convention of naming properties after their types.
     
    Cheers, Chris
    • Post Points: 5
  • 01-18-2004 3:23 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,185
    • Points 922,065

    RE: Current C# Collection Templates [1.6.0]

    Chris,

    Looks good. Perhaps you should add a check to see if they are trying to use "System.Int32", "int", or "Integer" as the KeyType and they have KeyIndexer set to true. If so, then just throw an ApplicationException that says they can't do that.

    Also, I didn't see these new methods being defined in the IList interface. Shouldn't they be included there?

    Thanks,
    Eric J. Smith
    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect
    • Post Points: 5
  • 01-18-2004 5:00 PM In reply to

    • cnahr
    • Top 50 Contributor
    • Joined on 06-03-2003
    • Posts 105
    • Points 2,275

    RE: Current C# Collection Templates [1.6.0]

    Eric,

    You mean an ApplicationException in the CodeSmith template code? Sure, that's a good idea, I'll add it to the template.

    I don't think any of these methods should be in the interfaces. The purpose of the separate interfaces is to allow one collection of XYZ objects, typed or untyped, regular ArrayList or Keys/Values collection, to be substituted for another. That's only possible if the interfaces are restricted to the standard methods defined by System.Collections.

    Cheers, Chris
    • Post Points: 5
  • 01-18-2004 5:09 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,185
    • Points 922,065

    RE: Current C# Collection Templates [1.6.0]

    Chris,

    Yeah, throw an ApplicationException in the CodeSmith template and it will be translated to a message box in the GUI.

    You're right. They shouldn't be part of the interface. I didn't think of that.

    Thanks,
    Eric J. Smith
    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect
    • Post Points: 5
  • 01-18-2004 5:16 PM In reply to

    • ejsmith
    • Top 10 Contributor
    • Joined on 12-27-2002
    • Dallas, TX USA
    • Posts 2,185
    • Points 922,065

    RE: Current C# Collection Templates [1.6.0]

    Chris,

    Are you getting notifications of new messages from the forums? I'm not getting notifications any more. I need to look into it.

    Eric J. Smith
    Eric J. Smith
    CodeSmith Tools, LLC
    Chief Software Architect
    • Post Points: 5
Page 1 of 2 (26 items) 1 2 Next >
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems