Non CSLA question from a newbie

Non CSLA question from a newbie

Old forum URL: forums.lhotka.net/forums/t/3479.aspx


Bobby posted on Wednesday, September 05, 2007

Just starting with the Expert VB 2005 book but I have a general question as to how to handle where the data for my business objects is stored.

For now, I am using single SQL server database. I have a data access library which gets called from various business objects to store/retrive the business object's data. I use factory methods to create the objects so the end user can't create these business objects using the New keyword.

My problem is that now I need to copy some business objects from 1 database to the other. I will retrieve some of the objects from 1 database and then do some modifications and store them in the second database.

I just don't know how to handle this. I don't know how to tell my business objects which underlying data store they should use. Since for now I have been using 1 data store, I read database/server name from a config file and my data access layer handles the storing/retreiving of the relation data. The objects do the O/R mapping.

I have seen some code where people use separate Factory class to create objects of a particular type but then it creates some other problems as now I need to make the GUID keys (that I use for uniquely idenfying my objects) to be Public rather then Private. Also the constructor for the Account class can't be private then.

Here is an example of an Account class that I have using 1 database.

Public Class Account

Private Sub New()

End Sub

Private _AccountID As Guid

Public Property AccountID() As Guid

Get

Return _AccountID

End Get

Private Set(ByVal value As Guid)

_AccountID = value

End Set

End Property

Private _AccountNumber As String

Private _DateCreated As DateTime

' The get/set methods for various properties

Public Shared Function GetAccount(ByVal accountID As Guid) As Account

Dim a As Account = Nothing

Dim dt As DataTable

dt = DataAccessLayer.ExecuteDataTable("dbo.GetAccountByAccountID", _

DataAccessLayer.CreateParameter("@AccountID", SqlDbType.UniqueIdentifier, accountID))

a = New Account

If a.MapData(dt) = False Then

a = Nothing

End If

Return a

End Function

Public Shared Function CreateAccount(ByVal accountNumber As String) As Account

Dim a As Account = New Account

a.AccountNumber = accountNumber

a.DateCreated = Now

Return a

End Function

Public Shared Function MapObject(ByVal dr As DataRow) As Account

Dim a As Account = New Account

If a.MapData(dr) = False Then

a = Nothing

End If

Return a

End Function

Private Function MapData(ByVal dr As System.Data.DataRow) As Boolean

Me.AccountID = DatabaseHelper.GetGuidValue(dr, "AccountID")

Me.AccountNumber = DatabaseHelper.GetStringValue(dr, "AccountNumber")

Me.DateCreated = DatabaseHelper.GetDateTimeValue(dr, "DateCreated")

Return True

End Function

End Class

How should I go about changing this Account class so that I can tell that this Account object comes from this database but another Account object that I create comes from another database?

Hope I am clearly explaining this. I have tried reading more on the net but most of the examples talk about very basic stuff and don't provide any real meat.

 

RockfordLhotka replied on Thursday, September 06, 2007

It looks like you have a formal DAL handling the database communication.

But what you are saying is that the DAL needs to be aware of some element of the object's state to do its work. As in: the object knows (logically) what database to talk to, and needs to communicate this to the DAL.

So your DAL needs to allow the object to (logically) indicate the database to use, and then the DAL can translate that to an actual database connection as appropriate.

So unless I'm missing something, your object's state (possibly only private state) must include some logical database identifier so it knows what database it wants to talk to.

RikGarner replied on Thursday, September 06, 2007

 You might want to consider looking at the Microsoft Enterprise Library Data Access Block - this allows you to define database connections in the config file and refer to them in code by strings. In your case you could then use code such as

database1 = DatabaseFactory.CreateDatabase("SourceDB")

when doing a Fetch, and then

database2 = DatabaseFactory.CreateDatabase("TargetDB")

in the Insert.

 

Bobby replied on Thursday, September 06, 2007

Thanks.

Copyright (c) Marimer LLC