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 AccountPrivate
Sub New() End SubPrivate
_AccountID As Guid Public Property AccountID() As Guid Get Return _AccountID End Get Private Set(ByVal value As Guid)_AccountID = value
End Set End PropertyPrivate
_AccountNumber As StringPrivate
_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 DataTabledt = DataAccessLayer.ExecuteDataTable(
"dbo.GetAccountByAccountID", _DataAccessLayer.CreateParameter(
"@AccountID", SqlDbType.UniqueIdentifier, accountID))a =
New Account If a.MapData(dt) = False Thena =
Nothing End If Return a End FunctionPublic
Shared Function CreateAccount(ByVal accountNumber As String) As Account Dim a As Account = New Accounta.AccountNumber = accountNumber
a.DateCreated = Now
Return a End FunctionPublic
Shared Function MapObject(ByVal dr As DataRow) As Account Dim a As Account = New Account If a.MapData(dr) = False Thena =
Nothing End If Return a End FunctionPrivate
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 FunctionEnd
ClassHow 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.
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.
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.
Copyright (c) Marimer LLC