Return type from DAL class (Sql ce, Linq to Sql)
- by bretddog
Hi,
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!