473,385 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3050
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
8
by: Chris Dunaway | last post by:
The next version of VS.Net (Whidbey) promises to add generics support to VB. I have a vague remembrance of Templates in C++, but I guess I need a refresher. What will generics allow us to do? ...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
8
by: Tony Johansson | last post by:
Hello! I have read that in practice, casting proved to be several times faster than using a generic. So the main reason to use generics is not that the performance is better because that's...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.