Passing objects through forms

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
Hi guys, im looking for some help/opinions on the proper way to pass data/objects through various forms.

So im using EF5 but the legacy model, I have a form that will create/load a cars details and then another form to add orders to that car. And here is how i go about it.

My 'Home'(Main) form has db as the entitycontext which is provided by a function so the server address can be changed(good or bad way of doing this?)
VB.NET:
Private db As ImageEntities = GetDB()

Public Function GetDB() As ImageEntities
        Dim EntityBuilder As New EntityConnectionStringBuilder
        EntityBuilder.Provider = "System.Data.SqlClient"
        EntityBuilder.ProviderConnectionString = My.Settings.SQLServer_Address
        EntityBuilder.Metadata = "res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl"


        Dim EntityConnection As New EntityConnection(EntityBuilder.ToString)


        Return New ImageEntities(EntityConnection)
    End Function

So i open my Car form and create or load the car details.

VB.NET:
Dim form As New dia_CreateOrder(Car_Reg, db)
        Dim Result As DialogResult = form.ShowDialog(Me)


        If Result = Windows.Forms.DialogResult.OK Then
            Car.Orders.Add(form.Order)
            OrdersBindingSource.DataSource = Car.Orders
        End If
        form.Dispose()
When i open the form im passing the EntityContext (not sure if i should pass the old context or use a new one, I did run into an issue where the objects was already in use by another context) and the Reg of the car

would this be considered the way to do this , and should i be passing around the entitycontext from when the application loads or should i be using new contexts.

or does any of this really matter much.
 
You should definitely be using the same context because otherwise the data isn't actually related. What you're doing is OK but, to be honest, it would be better if the context was created separately to any form, wrapped in a service layer that both forms can access.
 
Hi, thanks for your input, I have a Module that I put all my code that is used through out or code that has a specific job but doesn't belong to a particular class, from what your saying i'm thinking I should dim the context in there and add a property that returns that context, or should i just declare it public

VB.NET:
Module GlobalCode


    Private _DB As ImageEntities = GetDB
    Public ReadOnly Property DB As ImageEntities
        Get
            Return _DB
        End Get
    End Property


    Public Function GetDB() As ImageEntities
        Dim EntityBuilder As New EntityConnectionStringBuilder
        EntityBuilder.Provider = "System.Data.SqlClient"
        EntityBuilder.ProviderConnectionString = My.Settings.SQLServer_Address
        EntityBuilder.Metadata = "res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl"


        Dim EntityConnection As New EntityConnection(EntityBuilder.ToString)


        Return New ImageEntities(EntityConnection)
    End Function

Also is there a better way to change the connection settings for the Entities if needed?
 
Back
Top