newbe question

Any template questions or comments or suggestions

newbe question

Postby mirekh on Fri Apr 27, 2007 8:12 am


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
                Return _Field1
            End Get
            Set(ByVal value As String)
                _Field1 = value
            End Set
        End Property

        Public Property Importance() As Integer
                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()
            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
                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.
Posts: 1
Joined: Fri Apr 27, 2007 7:24 am
Location: Poland

Postby mike.griffin on Fri Apr 27, 2007 11:53 am

Sorry, I'm just heading out, will post back later, however, I'm not sure why you always write your own? You could save months by using dOOdads or even better using EntitySpaces?
User avatar
Site Admin
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby mgnoonan on Fri Apr 27, 2007 12:14 pm

I agree with Mike, that you should check out some of the existing templates and ORMs. They will save you a ton of time implementing and testing your own solution, and most of them have been around a while so they are well tested.

But, if you are determined to roll your own solution, the best way to learn template writing is to take apart one of the existing business entity templates to see how it works. It's not hard to do, although writing a template that covers all situations is definitely a challenge.

Then when you are done, you can upload your new creation to the template library and share with the rest of the world! 8)
Matt Noonan
EasyObjects.NET - The O/RM for the Enterprise Library
User avatar
Posts: 1019
Joined: Tue Sep 14, 2004 3:17 am
Location: Springboro, OH

Postby dnparsons on Fri Apr 27, 2007 1:35 pm

The source for the dOOdads architecture and all the templates are included in MyGeneration. In the MyGeneration Default Settings you'll find Language and Database target mappings. These will be reflected in the MyMeta Browser for each column and are the key to generating type specific code.

As you reverse engineer the Business Entity template for one of the architectures in order to roll your own, my guess is a lightbulb will eventually go on (as it did for me), and you'll think (as Mike and Matt have suggested), \"This does everything I want and more. Why am I spending so much time on something that was solved years ago by [insert favorite template/architecture here].\" EntitySpaces, dOOdads, EasyObjects, NHibernate, etc. :-)
David Neal Parsons
User avatar
Lieutenant General
Posts: 292
Joined: Sun Jan 09, 2005 2:10 pm
Location: Miami, Florida

Return to General Template Questions/Comments

Who is online

Users browsing this forum: No registered users and 1 guest