473,785 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics n-tier design question

Hi,

We have stumbled across an issue using the type safe collection
System.Collecti ons.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 GetCustomersWit hSpend(Type type, double spend)
{
// use custom or mapper here
return ObjectSpace.Get Collection(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.Collecti ons.ObjectModel .Collection<Cus tomer>(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 3063
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
"GetCustomersWi thSpend", 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> GetCustomersWit hSpend(Type type, double spend)
{
// Make the call to the data layer.
ArrayList objects = DataLayer.GetCu stomersWithSpen d(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((Cus tomer) 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.co m

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

We have stumbled across an issue using the type safe collection
System.Collecti ons.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 GetCustomersWit hSpend(Type type, double spend)
{
// use custom or mapper here
return ObjectSpace.Get Collection(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.Collecti ons.ObjectModel .Collection<Cus tomer>(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 GetCustomersByS pend method
but we struggled with how to get custom collections created using SQL
from the datalayer without creating static methods like
GetCustomersByS pend.

Your solution looks like the best option though :)

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

| We have stumbled across an issue using the type safe collection
| System.Collecti ons.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.Collecti ons.ObjectModel .Collection<Cus tomer>(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.GetLi st<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.c om> wrote in
message news:em******** *******@TK2MSFT NGP09.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
2463
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 still no can do... Any info would be great!
2
3106
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
8
3369
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? How do they make coding easier? Is there a resource that will give me a basic understanding of what generics are and how generics will be useful (in the context of VB.Net)? Thanks
23
2554
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 vs C++ templates. Does anyone knows a workaround to do this ? Thx : public class C<T> { private T myValue;
11
2501
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 provide an implemation of the operator +. Now I want to write another generic class Plan<DType>, which can
1
2438
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 "ThrowInConstructor" and "ThrowInFoo". First one throws "MyOwnException" in constructor, second one in "Foo()" method. There is a "GenericCatch" generics class able to accept "ThrowInConstructor" and "ThrowInFoo" as type parameter "<T>". There are two methods in...
4
2819
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 RuleDelegate<T>(T item); In my class my goal is to use a generic list collection to contain my generic delegates. This will allow me to pass this List to another library and call this list of functions. This could provide a new way to build rule base...
13
3837
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
6
5470
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 type constraint 'System.Windows.Forms.ComboBox' must come before any other constraints If I switch place of ComboBox with TextBoxBase, it would gripe:
8
1471
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 not the case. The only reason is that it's type-safe. I must ask if anyone has made any significant performance improvement using
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.