Bernad Pakpahan Blog

real Geek or young Entrepreneur?
See also: Other Geeks@INDC

Why .Net doesn’t fully support Generics?

As we know, purposes of generics introduced in .NET 2.0 are to avoid boxing and unboxing. Generics will be improve our application performance and make it safer. I will not dwell on generics functionality. It’s have more specific topic.

 

I will talk about .NET supports in generics. I’m already making simple code to make generics collection.

 

TList<Orders> list=DataRepository.OrdersProvider.GetAll();  

 

OK, list is a generic collection from Orders domain object. In UI layer I need to bind this object. Fortunately, .NET 2.0 API support object as data source.  In this case I use windows form as UI so I will be use BindingSource class.

 

private BindingSource ordersBindingSource;

ordersBindingSource.DataSource = list;

 

BindingSource support generics data source, but it’s self isn’t generics control. When I see the metadata, I found this.

 

public object DataSource { get; set; }

 

Why BindingSource doesn’t have generics data source property? For example like this

 

public List<T> DataSource { get; set; }

 

Hemm, when I bind my generics object to BindingSource it will be boxing it to object. Also when I need the current object, It will be unboxing again to my domain object.

 

            Orders order =(Orders) ordersBindingSource.Current;

            orderid = order.OrderID;

 

Might I have very little knowledge in .NET, so I don’t know how to avoid boxing and unboxing in this case.  If you have any solution, it will be appreciated. CMIIW

Share this post: | | | |
Posted: Apr 12 2006, 08:21 AM by bernardpakpahan | with 21 comment(s)
Filed under:

Comments

bernardpakpahan said:

Hi there,

Firstly, boxing and unboxing only occurs in value types, not reference ones. If your "Orders" type is class, not a struct, then you don't have to worry about it.

Secondly, I think the issue that you are aware of is about strongly typed collection. I'm suggesting that you use "BindingList" class for such purpose. BindingList is a generic class which is sufficient for your need. Accessing current object (strongly-typed one) can be attained by using indexer of your "[objectBindingList]" and the "[objectBindingSource].Position" property.

Thirdly, I'm not sure why you are using "Orders", which is plural for accessing a single object. I think it's a better idea to use "Order" for singular meaning and "Orders" for plural one, commonly a collection of "Order".
# April 12, 2006 10:34 AM

bernardpakpahan said:

"Why .Net doesn’t fully support Generics?"
There is any wrong in your question..it should be "How do I write code fully support Generics". Remember .NET is A Framework, don't expect anymore.
About "your problem", you should build custom data binding that support Generics, it's the best solution!...If you have data source in Generics model then data binding provider should do that like it;)
# April 12, 2006 7:46 PM

bernardpakpahan said:

Ok, that the solution using BindingList Class.
TList class whih is generics collection class, will be imlepementing BindingList. so my generic class will be supporting data binding. It's working! Thanks for Mr Mahara and Mr Agus.
But what happen?
I can't bind this object to BindingNavigator object, BindingNavigator only support BindingSource. Should I create custom Binding Navigator again? Or I make navigation control manually?

I Know the answer, it's should be ;).

For Mr Mahara, dont ask me why it's "orders" not "order". Please ask Microsoft, because it's a Norhtwind table in SQL Server 2000 :P. But may be I have the answers, Orders is not really single, because it is reference by orderdetails table. One Orders record can be have more than one detail item.
# April 13, 2006 2:47 AM

bernardpakpahan said:

Bernard,

1. Purpose of Generics bukan utk avoid boxing & unboxing. Tp utk support "Parameterized Type" yg goal utamanya adalah strong typing dan capability polymorphism via template/generics. Performance benefit yg didapat "cuma" efek samping.

2. I guess you don't really understand what is CASTING and what is BOXING/UNBOXING.

BOXING adalah casting suatu value type ke reference type, UNBOXING adalah sebaliknya. Sedangkan jika casting dilakukan pd reference type ke reference type, tidak ada BOXING/UNBOXING yg tjd disitu. INi ada hubungannya dgn Value Type di store di Stack, sementara Reference Type di store di Heap. So, pd case kamu:

Orders order =(Orders) ordersBindingSource.Current;

This is NOT necessarily unboxing. It's just casting. Unless, Order is Value Type (struct, not class). But I'm sure it's a class, right? If you're using Struct for this, it's not a very good design then. So, BOXING/UNBOXING tidak terjadi di sini.

Coba explore lebih jauh tentang Value Type vs Reference Type dan juga Stack vs Heap. Jgn sampai salah menggunakan suatu terminologi. CASTING is NOT necessarily BOXING/UNBOXING. ;)

3. Your post title lebih tepat di buat:
"Why BindingSource doesn’t fully support Generics?". Karena .NET, down to CLR fully support Generics. Tidak spt VM Java misalnya yg menggunakan mekanisme erasure. So, yg gak support Generics itu cuma library itu saja (BindingSource). Why? Mungkin type tsb memang belum dibuat API generics-nya. ;) So, find another built in type yg support Generics or you can make one on your own.
# April 13, 2006 7:40 PM

bernardpakpahan said:

Ok right now I know that boxing/boxing happen in value type or struct. Is casting still safe? Forget about boxing/unboxing, generics still important when we make strongly type collection, so error coz incompatible reference should be happen in compile time not in runtime.

May be I have choice the wrong title, but my concern is objectdatasource not fully support generics. I really happy when one of new features of .NET 2.0 is support object as data source. And with Visual studio 2005, you can do drag and drop programming without write complex code.

It will help some programmer especially UI programmer to develop application. Can you imagine when you drop an objectdatasource, you will be automatically create needed control with bindingsource and bindingnavigator. It's very helpful rather than previous development tool.

But what happen when technical leader, or architure ask for generics. objectdatasource wizard in Visual studio 2005 will be not useful. costum collection, custom binding, and custom navigator, and everything will be custom.

Microsoft try to convert complex programming become easy programming with his tool, something is very successfull, and something not really, But I know, a good programmer should be independent for all kind of tools.

Just for review, although I'm really satisfy with .NET 2.0 and Visual Studio 2005.
# April 13, 2006 9:15 PM

bernardpakpahan said:

"I can't bind this object to BindingNavigator object, BindingNavigator only support BindingSource. Should I create custom Binding Navigator again? Or I make navigation control manually?
"
hehehehe...man, sepertinya kau belom dapat filosofi dari goal dari .NET terutama di OO. Kalau memang solusi standard component tidak sesuai dengan requirement mu maka .NET menyediakan opsi untuk exteded komponen tersebut atau buat baru yg implementasi dari Contact (interface) nya...so disini skill OO yang dipertaruhkan;)
Sayangnya dulu kita jarang diskusi waktu kantor lantaran kita blom pernah satu project;)
# April 13, 2006 10:25 PM

bernardpakpahan said:

Hehehehe iya deh Boss....
Gw tau banget "Nggak ada yang nggak bisa di dunia programming". If you can't use standard library you can make your own. Makanya gw bilang "I Know the answer, it's should be ;).". Di kantor yang baru gw belajar banyak tentang itu. Bahkan boss gw udah create type query yang mau nyaingin LINQ. Try to become OO geek. hehehehehe
# April 13, 2006 10:48 PM

bernardpakpahan said:

Hehehe, kemarin mau post feedback disini tapi error (kebanyakan orang yg mau komentar kali)...but dont worry, nggak usah malu kalo salah, toh banyak yg nuntun ke jalan yg benar :)

Wah, si Om Wok mau bikin LINQ sendiri? Tuh orang harusnya kerja di Redmond :)
# April 14, 2006 12:57 AM

bernardpakpahan said:

>> I can't bind this object to BindingNavigator object, BindingNavigator only support BindingSource. Should I create custom Binding Navigator again? Or I make navigation control manually? <<

I'm not sure enough about what you are really doing with the BindingSource and BindingList. I think you should just bind your BindingSource's DataSource property to your BindingList's object and bind your BindingNavigator's DataSource property to your BindingSource's object. It should just work fine.



>> For Mr Mahara, dont ask me why it's "orders" not "order". Please ask Microsoft, because it's a Norhtwind table in SQL Server 2000 :P. But may be I have the answers, Orders is not really single, because it is reference by orderdetails table. One Orders record can be have more than one detail item. <<

There has been an issue with naming a table, whether it should be singular and plural one. One is better to use a singular one. Referring to Northwind (a database sample provided for MSSQL 2000), you may find that it still uses a plural one. But if you see the latest database sample provided in MSSQL 2005 (Adventure Works), Microsoft has already used a singular table name (except for BillOfMaterials). The reason is simply because it's just an entity.
# April 14, 2006 8:25 PM

bernardpakpahan said:

My Man Mahara, there is no issue with Northwind or SQL Server 2000. You say every naming method in table must be singular, coz all table are entity. Maybe I agree with you. But please change your point of view, SQL Server 2000 has been realeased before .NET 1.0 and the most important thing SQL Server is RDBMS event SQL Server 2005 also RDBMS. If you are classic VB programmer or other Non OO programmer, I think doesn't matter with orders table name.

It's different if your database is OO database like db4o, everything must be OO taste. So no issue with Northwind database, coz it's sample database for all programming language. I know I use this database for OO programming, but it's a bad idea if I must change the table name in database or in my code (just for dummy my friend).So my asnwers should be clear. Btw, thank you for your comment.
# April 17, 2006 8:22 PM

bernardpakpahan said:

Frans Bouma (C# MVP) also found similar issues with me. See this issues in his blog
http://weblogs.asp.net/fbouma/archive/2005/12/23/433899.aspx
# April 17, 2006 11:21 PM

bernardpakpahan said:

Nice Article and Site, Booked for Reference
# July 16, 2006 12:53 PM

bernardpakpahan said:

Informative Page
# July 16, 2006 12:54 PM

shoes-news said:

Hello! ;)

hey... what brainsick news!

what do you consider about it?

# February 7, 2007 4:28 AM

RaymonWazerri said:

Hey,

I love what you'e doing!

Don't ever change and best of luck.

Raymon W.

# April 21, 2007 7:53 AM

RandyJones said:

Looks Like Dallas is in trouble!

Phoenix might end up blowing them all away.

PHX vs. Det. Hmmm..Could be interesting?

# April 26, 2007 12:57 AM

MaryAnne said:

I'm not quite understanding what all

this is supposed to be about?

Must be me or something...

# April 27, 2007 12:29 AM

Transmission said:

Hey,

Great stuff here!

I'll definitely bookmark this place and come back soon.

Robby

# May 3, 2007 1:14 AM

JerryGreen said:

How green is the grass on the other side of the fence?

Not much. Don't believe it I tell you.

Jerry

# May 3, 2007 2:45 AM

Garamoff said:

<a href=www.spring-neverlands.ru/.../viewtopic.php порно видео</a>

<a href=www.scottmartinchallenge.com/.../viewtopic.php инцест видеоролик</a>

# April 29, 2009 7:48 PM

Semkamanz said:

[url=http://nokia-n72.ru/]темы для nokia n72[/url]

[url=http://ford4.ru/]Ford[/url]

Mau =^-^=

# May 3, 2009 3:18 PM