Using VS2008 and Sql CE 3.5, and preferably Linq to Sql.
I’m learning database, and unsure about DAL methods return types and how/where to map the data over to my business objects: I don’t want direct UI binding.
A business object class UserData, and a class UserDataList (Inherits List(Of UserData)), is represented in the database by the table “Users”. I use SQL Compact and run SqlMetal which creates dbml/designer.vb file. This gives me a class with a TableAttribute:
<Table()> _
Partial Public Class Users
I’m unsure how to use this class. Should my business object know about this class, such that the DAL can return the type Users, or List(Of Users) ?
So for example the “UserDataService Class” is a part of the DAL, and would have for example the functions GetAll and GetById. Will this be correct : ?
Public Class UserDataService
Public Function GetAll() As List(Of Users)
Dim ctx As New MyDB(connection)
Dim q As List(Of Users) = From n In ctx.Users Select n
Return q
End Function
Public Function GetById(ByVal id As Integer) As Users
Dim ctx As New MyDB(connection)
Dim q As Users = (From n In ctx.Users Where n.UserID = id Select n).Single
Return q
End Function
And then, would I perhaps have a method, say in the UserDataList class, like:
Public Class UserDataList
Inherits List(Of UserData)
Public Sub LoadFromDatabase()
Me.clear()
Dim database as New UserDataService
dim users as List(Of Users)
users = database.GetAll()
For each u in users
dim newUser as new UserData
newUser.Id = u.Id
newUser.Name = u.Name
Me.Add(newUser)
Next
End Sub
End Class
Is this a sensible approach? Would appreciate any suggestions/alternatives, as this is my first attempt on a database DAL.
cheers!
EDIT:
Seems I have problems with the query/return types of GetAll() and GetAllById().. Not sure how to do this..
Open Visual Studio.
Open the server connections tab and connect to your SQL server.
In your project, add a new Linq To SQL Data Context. This will add a new file. Once this is open in the designer, drag and drop the tables from your database into the SQL Data context.
At this point, you can now go to your code and say.
If you are really trying to control the class that is created then I recommend that you look into the SQL Metal tool which can create data contexts from an xml file.