471,570 Members | 936 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Generics n-tier design question

Hi,

We have stumbled across an issue using the type safe collection
System.Collections.ObjectModel.Collection <T> to retrieve data from our
data layer.

Say we have a customer object and want to get a type safe collection of
all customers who have spend = X
Within our datalayer we would currently have the following method

public static ArrayList GetCustomersWithSpend(Type type, double spend)
{
// use custom or mapper here
return ObjectSpace.GetCollection(type,"where spend =" +
spend.ToString());
}

However, say we wanted to use a type safe generic collection I guess we
would use this;

public static
System.Collections.ObjectModel.Collection<Customer >(double spend)
{
}

But this would break the n-tier seperation as our datalayer now knows
about the Customer type.

I guess we could still return an ArrayList from the datalayer and then
create the type safe collection in the business tier using the
ArrayList.

Does anyone have any thoughts on this?

Cheers

Russ

Dec 1 '05 #1
6 3005
Russ,

The thing is, in a logical sense, you are already breaking your n-tier
separation in your data layer. If you have a method named
"GetCustomersWithSpend", then obviously you are going to return customers,
and not something else. If anything, this is a business layer function
which in turn will call the data layer.

If you really want to keep your types out of the data layer, then return
an ArrayList, and then have a proxy/wrapper in the business side, which does
this:

// In business layer.
public static IList<Customer> GetCustomersWithSpend(Type type, double spend)
{
// Make the call to the data layer.
ArrayList objects = DataLayer.GetCustomersWithSpend(type, spend);

// Convert to an IList implementation:
List<Customer> retVal = new List<Customer>(objects.Count);

// Cycle through and add the item.
foreach (object o in objects)
{
// Add to the arraylist.
retVal.Add((Customer) o);
}

// Return.
return retVal;
}

You should use ICollection<T> instead of Collection<T>, since it will be
easier to swap out later (and there is nothing on the implementation which
isn't offered through the interface). Additionally, IList extends
ICollection<T>, and it offers an indexer, as well as being able to support
data binding easier. Because of this, I returned IList<Customer>.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"russ" <ru****@excite.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi,

We have stumbled across an issue using the type safe collection
System.Collections.ObjectModel.Collection <T> to retrieve data from our
data layer.

Say we have a customer object and want to get a type safe collection of
all customers who have spend = X
Within our datalayer we would currently have the following method

public static ArrayList GetCustomersWithSpend(Type type, double spend)
{
// use custom or mapper here
return ObjectSpace.GetCollection(type,"where spend =" +
spend.ToString());
}

However, say we wanted to use a type safe generic collection I guess we
would use this;

public static
System.Collections.ObjectModel.Collection<Customer >(double spend)
{
}

But this would break the n-tier seperation as our datalayer now knows
about the Customer type.

I guess we could still return an ArrayList from the datalayer and then
create the type safe collection in the business tier using the
ArrayList.

Does anyone have any thoughts on this?

Cheers

Russ

Dec 1 '05 #2
Many thanks for the reply Nicholas,

I agree that we shouldn't really have the GetCustomersBySpend method
but we struggled with how to get custom collections created using SQL
from the datalayer without creating static methods like
GetCustomersBySpend.

Your solution looks like the best option though :)

Dec 1 '05 #3
"russ" <ru****@excite.com> a écrit dans le message de news:
11**********************@g49g2000cwa.googlegroups. com...

| We have stumbled across an issue using the type safe collection
| System.Collections.ObjectModel.Collection <T> to retrieve data from our
| data layer.

| However, say we wanted to use a type safe generic collection I guess we
| would use this;
|
| public static
| System.Collections.ObjectModel.Collection<Customer >(double spend)
| {
| }

We do the following :

We would create a List<Customer> in the data layer

List<T> also implements IList

The method returns an IList

We assign the result of the method call, casting it back to the
List<Customer>

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 1 '05 #4
"Joanna Carter [TeamB]" <jo****@not.for.spam> a écrit dans le message de
news: OZ**************@TK2MSFTNGP14.phx.gbl...

| We do the following :

Better still, parameterise the retrieval method :

public List<T> GetList<T>()
{
...
}

This then allows the following syntax in calling :

List<Customer> = DataLayer.GetList<Customer>();

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 1 '05 #5
Hi Joanna,

Thats exactly what I needed :)

Many thanks for your help.

Cheers

Russ

Dec 2 '05 #6

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:em***************@TK2MSFTNGP09.phx.gbl...
Because of this, I returned IList<Customer>.

Hope this helps.


Thanks Nicholas.

From now on I will also start using IList<T> and not return implementing
classes.
I'm not much for serious data abstraction, but this makes a lot of sense to
me.

Happy Coding
- Michael S

Dec 5 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Bernardo Heynemann | last post: by
2 posts views Thread by Mr.Tickle | last post: by
8 posts views Thread by Chris Dunaway | last post: by
23 posts views Thread by Luc Vaillant | last post: by
1 post views Thread by Vladimir Shiryaev | last post: by
4 posts views Thread by Cedric Rogers | last post: by
13 posts views Thread by rkausch | last post: by
6 posts views Thread by =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post: by
8 posts views Thread by Tony Johansson | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | 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.