I am a newbie in MyGeneration and I always write my own ORM.
Suppose I have a table in my DBMS (this example is trivial and I'm interested in an idea):
Table1(Field1 int, Field2 char(2))
Writing code to get some data from this table I usually do:
Create an class (in my BLL):
- Code: Select all
Public Class MyClass
Private _Field1 As Integer = 0
Private _Field2 As String = \"\"
Public Sub New(field1 as integer, field2 as string)
Me.Field1 = field1
Me.Field2 = field2
End Sub
Public Property Field1() As String
Get
Return _Field1
End Get
Set(ByVal value As String)
_Field1 = value
End Set
End Property
Public Property Importance() As Integer
Get
Return _Field2
End Get
Set(ByVal value As Integer)
_Field2 = value
End Set
End Property
End Class
Create an abstract class in my DAL working as data provider for BLL
Public MustInherit Class GeneralProvider
Public MustOverride Function GetMyClassData() As List(Of MyClass)
Protected Overridable Function GetMyClassFromReader(ByVal reader As IDataReader) As MyClass
Return New MyClass(CInt(reader(\"Field1 \")), _
reader(\"Field1\").ToString )
End Function
Protected Overridable Function GetMyClassCollectionFromReader(ByVal reader As IDataReader) As List(Of MyClass)
Dim MyObjects As New List(Of MyClass)
While reader.Read()
MyObjects.Add(GetMyClassFromReader(reader))
End While
Return MyObjects
End Function
End Sub
And finally for concrete DBMS DAL implementation:
- Code: Select all
Public Class DBMS_Provider
Inherits GeneralProvider
Public Overrides Function GetMyClassData() As System.Collections.Generic.List(Of MyClass)
Using cn As New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand(\"some_command\", cn)
cmd.CommandType = CommandType.StoredProcedure
cn.Open()
Return GetMyClassCollectionFromReader(cmd.ExecuteReader)
End Using
End Function
Using MyGeneration I would like to automate creating classes (like my MyClass in example above) from any table, view or stored procedure.
I would like generate body of function like GetMyClassFromReader - the problem is with writing proper casting form reader and from field I choose:
return New MyClass(CInt(reader(\"Field1\")), reader(\"Field1\").ToString ....)
I thing this is very simple but I have no idea how to do it with MyGeneration.
