471,319 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

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.
GenericKeyedCollection<int, Person> collection = new
GenericKeyedCollection<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.Count);
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 GenericKeyedCollection.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace Unity.Domain
{
public class GenericKeyedCollection<Key, Type> :
KeyedCollection<Key,
Type>
{
protected override Key GetKeyForItem(Type item)
{
//What do I put here since at runtime
//I don't know what the

type will be?
}
}

Dec 12 '05 #1
10 4316
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.GetKeyForItem(person1).;

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

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"JoSkillz" <oc*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.
GenericKeyedCollection<int, Person> collection = new
GenericKeyedCollection<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.Count);
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 GenericKeyedCollection.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace Unity.Domain
{
public class GenericKeyedCollection<Key, Type> :
KeyedCollection<Key,
Type>
{
protected override Key GetKeyForItem(Type item)
{
//What do I put here since at runtime
//I don't know what the

type will be?
}
}

Dec 12 '05 #2
You need to return the unique key:

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

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

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

Gabriel Lozano-Morán
MCSD .NET
Real Software
http://www.realdn.net
http://www.realsoftware.be
Dec 12 '05 #3
You suggestion would work however I would lose the generic
instantiation of the "GenericKeyedCollection" 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 GenericKeyedCollection<int, Chapter> chapters = new
GenericKeyedCollection<int, Chapter>();

}
public class Shelf : DomainBase
{
//Use string as key becuase we will be using the ISBN
private GenericKeyedCollection<string, Book> books = new
GenericKeyedCollection<string, Book>();
}
That way I would be able to use the GenericKeyCollection with ANY
object in the future and rely on the power of Generics to work for me.

Dec 12 '05 #4
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" <oc*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
You suggestion would work however I would lose the generic
instantiation of the "GenericKeyedCollection" 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 GenericKeyedCollection<int, Chapter> chapters = new
GenericKeyedCollection<int, Chapter>();

}
public class Shelf : DomainBase
{
//Use string as key becuase we will be using the ISBN
private GenericKeyedCollection<string, Book> books = new
GenericKeyedCollection<string, Book>();
}
That way I would be able to use the GenericKeyCollection with ANY
object in the future and rely on the power of Generics to work for me.

Dec 12 '05 #5
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.

Dec 12 '05 #6
Eg:

public class GenericKeyedCollection<Key, Type> : KeyedCollection<Key, Type>
where Type : IKeyed

Gabriel
Dec 13 '05 #7
LOL...bluring the lines between SQL and C# now we have where clauses

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

Gabriel
Dec 13 '05 #9
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.

Dec 13 '05 #10
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
Dec 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Wayne | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.