More on migrating to ASP.Net 2.0
- Hi again folks. Well I have spent some of my convalescence migrating my wifes web site to ASP.Net 2.0.
On the whole it was pretty easy to get the migration done however some issues needed addressing. These were nearly all to do with badly formed HTML markup in the aspx pages such as using MSO schemas without referencing them correctly. So I probably spent about an hour fixing all my bad markup.
I then decided to migrate from the Access database to SQL server 2005. I only have a few tables in the original database, including a user/customer table that I can delete and use the provider stuff for SQL instead. So I am left with :
- a Products table,
- a Categories table and
- a linking table CategoryProduct that manages the many many relationship between products and categories. (Janine wanted the products to be in more than one category).
Migrating the schema was done by hand and I also took the opportunity to add some admin pages to enter descriptions and managed the product-category relationship.
The main things I have noticed so far are that if you are happy with 2-tier programming you really dont have to write any code at all. The wizards will even write your stored procedures for you. And if you want to use three tier data access with middle tier business objects then that is still trivial.
I spend most of the time now configuring data stores or middle tier objects in fact. One thing I wanted to do was to allow an admin page to list all the pictures in the ProductImages folder in the site.
I wrote a simple peice of code for my App_Code folder...
Imports
Microsoft.VisualBasic
Imports
System.Collections.Generic
Public
Class PictureInfo
Private _ImageUrl As String
Public Sub PictureInfo()
End Sub
''' <summary>
''' url for the two images
''' </summary>
Public Property ImageURL() As String
Get
Return Me._ImageUrl
End Get
Set(ByVal value As String)
Me._ImageUrl = value
End Set
End Property
End
Class
Public
Class PictureHelper
Public Shared Function GetProductImages() As List(Of PictureInfo)
Dim ret As New List(Of PictureInfo)
Dim fullpath As String = System.Web.HttpContext.Current.Server.MapPath("..\ProductImages")
Dim files() As String = System.IO.Directory.GetFiles(fullpath, "*.jpg")
For Each file As String In files
Dim info As New PictureInfo
info.ImageURL = StripFileName(file)
ret.Add(info)
Next
files = System.IO.Directory.GetFiles(fullpath,
"*.gif")
For Each file As String In files
Dim info As New PictureInfo
info.ImageURL = StripFileName(file)
ret.Add(info)
Next
Return ret
End Function
Private Shared Function StripFileName(ByVal fullname As String) As String
Dim sep As String = "/\"
Dim segments() As String = fullname.Split(sep.ToCharArray)
Return segments(segments.GetLength(0) - 1)
End Function
End
Class
So I have a helper class that looks in the file system and generates generic List(Of ProductInfo). I will add some more stuff to the productinfo class in due course such as the size of the image and so forth. But for now its useful enough.
Next I created a page that used the PictureHelper class as a data source, and used the GetProductImages() shared function to return the list of ProductInfo.
Copy it over and give it a try.
Now many people have asked me about this data access stuff, especially using a middle tier object. Here are the rules.
- It is preferable to have a shared/static method on the helper class although you can use an instance method if you wish and the framework will create an instance.
- For the class that you return in a list make sure that you have ACCESSOR METHODS not just public fields. It wont work if you only use fields.
- Have an update method that takes the elements of the object not an object itself ie Update(productID as integer, categoryID as integer, description as string) rather than Update(stuff as myClass)
I have also ripped out most of the code behind for the regular pages for customers to view the products. With templates on the data controls I really dont need to write any code. The only real code left in the project is specific data access wrappers in app_code and also fragments of code to embedded into the aspx pages. I will give an example of that in my next post.