[2005] I need clear idea about generics and collections?
Hi All,
I am developing a addressbook application. The application reads,stores and finds the id,name,address,city,phone.
I am inheriting Array list collection for CONTACTS class which holds id,name,address... and so on. The add and remove methods uses to store and delete purpose in the contacts collection.
Doubt no. 1. How could I map the fields with the members(name,address...)
If I need to store the arraylist values to the database, Is I have to iterate through all the array list elements? If there is a minor change in the array list data value, then how could I update the database values? Is i have to pass full array list or particular element in the array list? All the thinks should be in Generic.
Any help like code explanation, web links are appreciated.
Regards,
Senthil
Re: [2005] I need clear idea about generics and collections?
ArrayList is not a generic collection; List(Of T) is. If you want, you can specialise this by inheriting from it [List(Of MyType)].
How you load/save these to a database is completely up to you. You could use write custom serialisation functions or leave it up to your data access layer to work it out; it depends on the architecture of your application. We would need to know a bit more about that before suggesting anything concrete.
Re: [2005] I need clear idea about generics and collections?
Your Business Logic Layer is where your types should be declared. You might have a Person class and a PersonCollection class. Your BLL gets data from the Data Access Layer and creates business objects from the data, e.g. it might create a PersonCollection full of Person objects from a DataTable full of DataRows. The collection is then passed on the the Presentation Layer for display and manipulation by the user. Once done the PL passes the collection back to the BLL. The BLL analyses the changes, packages them up in a form appropriate for the DAL, i.e. a DataTable, and then sends that data to the DAL to be saved to the database. Note that your DAL should be using a typed DataSet, which makes translation of the data easier. Your Person class could even have a Friend constructor that takes a typed DataRow as a parameter.
I've glossed over a lot of the detail there but that's basically how it works. Note that if you don't want to create an n-tier application one or more of those abstract layers may be incorporated into the same physical layer. That's up to you.
With regards to generics, that's implemented in the PersonCollection. Depending on the functionality you want you would derive it from List(Of Person) or, more likely in this scenario, from System.Collections.ObjectModel.Collection(Of Person). You can also have your Person class implement various interfaces to make it easier to do certain things.