Custom KeyCollection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JoSkillz

    #1

    Custom KeyCollection

    I am trying to create a custom KeyCollection that will only allow my
    domain objects to be added to the Collection. I was hoping I could
    achieve the following implementation.


    GenericKeyedCol lection<int, Person> collection = new
    GenericKeyedCol lection<int, Person>();


    Person person1 = new Person();
    person1.ID = 1;
    person1.Name = "Jimmy";


    Person person2 = new Person();
    person2.ID = 2;
    person2.Name = "Chris";


    collection.Add( person1);
    collection.Add( person2);


    Assert.AreEqual (2, collection.Coun t);


    Now I thought I understood how to do this but I don't know what to
    put in the absratct protected override GetKeyForItem. Can someone
    please give me some guidance on what to put here?


    Here is my implmentation of the GenericKeyedCol lection.


    using System;
    using System.Collecti ons.Generic;
    using System.Collecti ons.ObjectModel ;
    using System.Text;


    namespace Unity.Domain
    {
    public class GenericKeyedCol lection<Key, Type> :
    KeyedCollection <Key,
    Type>
    {
    protected override Key GetKeyForItem(T ype item)
    {
    //What do I put here since at runtime
    //I don't know what the

    type will be?
    }
    }

  • Vadym Stetsyak

    #2
    Re: Custom KeyCollection

    IMHO you have 2 options:

    return the key that satisfies the value ( find value in the list that is
    equal to item and another may be auto-key generation for example

    Person person1 = new Person();
    person1.Name = "Jimmy";
    person1.ID = collection.GetK eyForItem(perso n1).;

    You can return either incremented value, hashcode, or some calculated value

    --
    Vadym Stetsyak aka Vadmyst
    Blog about software development, algorithms, network protocols, .NET, programming languages, tips &amp; tricks, coding techniques and more.


    "JoSkillz" <ocampoj@gmail. com> wrote in message
    news:1134426005 .282812.268480@ g44g2000cwa.goo glegroups.com.. .[color=blue]
    >I am trying to create a custom KeyCollection that will only allow my
    > domain objects to be added to the Collection. I was hoping I could
    > achieve the following implementation.
    >
    >
    > GenericKeyedCol lection<int, Person> collection = new
    > GenericKeyedCol lection<int, Person>();
    >
    >
    > Person person1 = new Person();
    > person1.ID = 1;
    > person1.Name = "Jimmy";
    >
    >
    > Person person2 = new Person();
    > person2.ID = 2;
    > person2.Name = "Chris";
    >
    >
    > collection.Add( person1);
    > collection.Add( person2);
    >
    >
    > Assert.AreEqual (2, collection.Coun t);
    >
    >
    > Now I thought I understood how to do this but I don't know what to
    > put in the absratct protected override GetKeyForItem. Can someone
    > please give me some guidance on what to put here?
    >
    >
    > Here is my implmentation of the GenericKeyedCol lection.
    >
    >
    > using System;
    > using System.Collecti ons.Generic;
    > using System.Collecti ons.ObjectModel ;
    > using System.Text;
    >
    >
    > namespace Unity.Domain
    > {
    > public class GenericKeyedCol lection<Key, Type> :
    > KeyedCollection <Key,
    > Type>
    > {
    > protected override Key GetKeyForItem(T ype item)
    > {
    > //What do I put here since at runtime
    > //I don't know what the
    >
    > type will be?
    > }
    > }
    >[/color]


    Comment

    • Gabriel Lozano-Morán

      #3
      Re: Custom KeyCollection

      You need to return the unique key:

      public class PersonCollectio n : KeyedCollection <int, Person>
      {
      public PersonCollectio n() : base() { }

      protected override int GetKeyForItem(P erson person)
      {
      return person.ID;
      }
      }

      PersonCollectio n pc = new PersonCollectio n();
      pc.Add(new Person(1, "Gabriel", "Lozano-Morán");

      Gabriel Lozano-Morán
      MCSD .NET
      Real Software

      Alles over telecom en technologie!



      Comment

      • JoSkillz

        #4
        Re: Custom KeyCollection

        You suggestion would work however I would lose the generic
        instantiation of the "GenericKeyedCo llection" class from a generics
        perspective. I want to be able to declare the following private field
        declarations.


        public class Chapter : DomainBase {}


        public class Book : DomainBase
        {
        private GenericKeyedCol lection<int, Chapter> chapters = new
        GenericKeyedCol lection<int, Chapter>();



        }


        public class Shelf : DomainBase
        {
        //Use string as key becuase we will be using the ISBN
        private GenericKeyedCol lection<string, Book> books = new
        GenericKeyedCol lection<string, Book>();


        }


        That way I would be able to use the GenericKeyColle ction with ANY
        object in the future and rely on the power of Generics to work for me.

        Comment

        • Gabriel Lozano-Morán

          #5
          Re: Custom KeyCollection

          One way to solve this is to make sure that the generic part, like Chapter or
          Person implement an interface for example:

          public interface IKeyed
          {
          int Key { get; }
          }

          Then add the generic constraint where T is IKeyed and in the GetKeyForItem()
          return the Key property.

          Gabriel

          "JoSkillz" <ocampoj@gmail. com> wrote in message
          news:1134429041 .694696.109260@ g14g2000cwa.goo glegroups.com.. .[color=blue]
          > You suggestion would work however I would lose the generic
          > instantiation of the "GenericKeyedCo llection" class from a generics
          > perspective. I want to be able to declare the following private field
          > declarations.
          >
          >
          > public class Chapter : DomainBase {}
          >
          >
          > public class Book : DomainBase
          > {
          > private GenericKeyedCol lection<int, Chapter> chapters = new
          > GenericKeyedCol lection<int, Chapter>();
          >
          >
          >
          > }
          >
          >
          > public class Shelf : DomainBase
          > {
          > //Use string as key becuase we will be using the ISBN
          > private GenericKeyedCol lection<string, Book> books = new
          > GenericKeyedCol lection<string, Book>();
          >
          >
          > }
          >
          >
          > That way I would be able to use the GenericKeyColle ction with ANY
          > object in the future and rely on the power of Generics to work for me.
          >[/color]


          Comment

          • JoSkillz

            #6
            Re: Custom KeyCollection

            How do I add a constraint to the generic to be a certain type? because
            in that case I can declare the type to always be of type DomainBase.

            Comment

            • Gabriel Lozano-Morán

              #7
              Re: Custom KeyCollection

              Eg:

              public class GenericKeyedCol lection<Key, Type> : KeyedCollection <Key, Type>
              where Type : IKeyed

              Gabriel


              Comment

              • JoSkillz

                #8
                Re: Custom KeyCollection

                LOL...bluring the lines between SQL and C# now we have where clauses

                Comment

                • Gabriel Lozano-Morán

                  #9
                  Re: Custom KeyCollection

                  I guess you are not familiar yet with Project LINQ ? :-)
                  http://msdn.microsoft.com/netframewo...q/default.aspx

                  Gabriel


                  Comment

                  • JoSkillz

                    #10
                    Re: Custom KeyCollection

                    Just when I just starting to wrap my hands around HQL and Generics they
                    do this! Geesh.. I need to upgrade my brain it's becoming outdated.

                    Comment

                    • Gabriel Lozano-Morán

                      #11
                      Re: Custom KeyCollection

                      I know it's hard to keep up. And then if you see what profiles employers
                      require, it's becoming impossible. I used to be a contractor but it was
                      really hard to get projects because being a contractor they expected from
                      you that you know everything.

                      Gabriel


                      Comment

                      Working...