From: Richard B. <rb...@us...> - 2004-10-18 01:36:55
|
Update of /cvsroot/jcframework/dotnet In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22604 Modified Files: AToMSFramework.vbproj CClassMap.vb CInjectedObject.vb CPersistenceBroker.vb CXMLConfigLoader.vb Log Message: It's now possible to persist interfaces instead of concrete classes. Must provide a factory name that implements IClassFactory (XML config only) Index: AToMSFramework.vbproj =================================================================== RCS file: /cvsroot/jcframework/dotnet/AToMSFramework.vbproj,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- AToMSFramework.vbproj 14 Oct 2004 05:25:02 -0000 1.18 +++ AToMSFramework.vbproj 18 Oct 2004 01:36:43 -0000 1.19 @@ -359,6 +359,11 @@ BuildAction = "Compile" /> <File + RelPath = "IClassFactory.vb" + SubType = "Code" + BuildAction = "Compile" + /> + <File RelPath = "IConfigLoader.vb" SubType = "Code" BuildAction = "Compile" Index: CPersistenceBroker.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CPersistenceBroker.vb,v retrieving revision 1.66 retrieving revision 1.67 diff -u -d -r1.66 -r1.67 --- CPersistenceBroker.vb 18 Oct 2004 00:08:10 -0000 1.66 +++ CPersistenceBroker.vb 18 Oct 2004 01:36:43 -0000 1.67 @@ -956,9 +956,16 @@ Public Function getClassMap(ByVal pType As Type) As CClassMap Dim ClassMap As CClassMap - ClassMap = m_classMaps.Item(pType.Name) + ClassMap = getClassMapByTypeName(pType.Name, pType.FullName) If ClassMap Is Nothing Then - ClassMap = m_classMaps.Item(pType.FullName) + 'try to find an interface that is mapped - first mapped interface we find will be used + Dim intType, interfaces() As Type + For Each intType In pType.GetInterfaces() + ClassMap = getClassMapByTypeName(intType.Name, intType.FullName) + If Not ClassMap Is Nothing Then + Exit For + End If + Next End If If (ClassMap Is Nothing) Then Throw New NoClassMapException("No class map for " & pType.FullName) @@ -966,6 +973,18 @@ Return ClassMap End Function + Private Function getClassMapByTypeName(ByVal name As String, ByVal fullname As String) As CClassMap + Dim ClassMap As CClassMap + ClassMap = m_classMaps.Item(name) + If ClassMap Is Nothing Then + ClassMap = m_classMaps.Item(fullname) + End If + If (ClassMap Is Nothing) Then + Return Nothing + End If + Return ClassMap + End Function + '''----------------------------------------------------------------------------- ''' <summary> ''' Retrieves a _CRelationalDatabase object by name Index: CInjectedObject.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CInjectedObject.vb,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- CInjectedObject.vb 15 Oct 2004 06:42:56 -0000 1.2 +++ CInjectedObject.vb 18 Oct 2004 01:36:43 -0000 1.3 @@ -98,14 +98,33 @@ Public Function getClassMap() As CClassMap Implements IPersistableObject.getClassMap Dim ClassMap As CClassMap + ClassMap = getClassMap(TypeName(m_object), m_object.GetType.FullName) + If ClassMap Is Nothing Then + 'try to find an interface that is mapped - first mapped interface we find will be used + Dim intType, interfaces() As Type + For Each intType In m_object.GetType.GetInterfaces() + ClassMap = getClassMap(intType.Name, intType.FullName) + If Not ClassMap Is Nothing Then + Exit For + End If + Next + End If + If (ClassMap Is Nothing) Then + Throw New NoClassMapException("No class map for " & m_object.GetType.FullName) + End If + Return ClassMap + End Function + + Private Function getClassMap(ByVal name As String, ByVal fullname As String) As CClassMap + Dim ClassMap As CClassMap Dim persistenceBroker As CPersistenceBroker persistenceBroker = getPersistenceBrokerInstance() - ClassMap = persistenceBroker.getClassMap(TypeName(m_object)) + ClassMap = persistenceBroker.getClassMap(name) If ClassMap Is Nothing Then - ClassMap = persistenceBroker.getClassMap(m_object.GetType.FullName) + ClassMap = persistenceBroker.getClassMap(fullname) End If If (ClassMap Is Nothing) Then - Throw New NoClassMapException("No class map for " & m_object.GetType.FullName) + Return Nothing End If Return ClassMap End Function Index: CXMLConfigLoader.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CXMLConfigLoader.vb,v retrieving revision 1.23 retrieving revision 1.24 diff -u -d -r1.23 -r1.24 --- CXMLConfigLoader.vb 7 Oct 2004 05:57:33 -0000 1.23 +++ CXMLConfigLoader.vb 18 Oct 2004 01:36:44 -0000 1.24 @@ -261,6 +261,7 @@ Dim attrDatabase As XmlAttribute, attrSuperClassName As XmlAttribute, attrReadOnly As XmlAttribute, attrModifyOnly As XmlAttribute Dim attrClassNameSpace, attrAssemblyPath, attrSuperClassNameSpace As XmlAttribute Dim attrSharedField As XmlAttribute, attrSharedValue As XmlAttribute + Dim attrClassFactory As XmlAttribute attrClassName = node.GetAttributeNode("name") attrTable = node.GetAttributeNode("table") attrDatabase = node.GetAttributeNode("database") @@ -273,6 +274,7 @@ attrClassNameSpace = node.GetAttributeNode("namespace") attrAssemblyPath = node.GetAttributeNode("assemblypath") attrSuperClassNameSpace = node.GetAttributeNode("superclassnamespace") + attrClassFactory = node.GetAttributeNode("factory") Dim ClassMap As CClassMap Dim dbMap As CDatabaseMap Dim tblMap As CTableMap @@ -328,6 +330,9 @@ If Not attrSuperClassNameSpace Is Nothing Then ClassMap.SuperClassNameSpace = attrSuperClassNameSpace.Value End If + If Not attrClassFactory Is Nothing Then + ClassMap.IClassFactoryName = attrClassFactory.Value + End If dbMap = New CDatabaseMap dbMap.Name = attrDatabase.Value tblMap = New CTableMap Index: CClassMap.vb =================================================================== RCS file: /cvsroot/jcframework/dotnet/CClassMap.vb,v retrieving revision 1.34 retrieving revision 1.35 diff -u -d -r1.34 -r1.35 --- CClassMap.vb 18 Oct 2004 00:08:10 -0000 1.34 +++ CClassMap.vb 18 Oct 2004 01:36:43 -0000 1.35 @@ -64,6 +64,9 @@ Private m_sqlFindStatement As CSqlStatement Private m_sqlRetrieveStatement As CSqlStatement + Private m_classFactoryName As String + Private m_classFactory As IClassFactory + '''----------------------------------------------------------------------------- ''' <summary> ''' The name of the class being mapped @@ -602,6 +605,15 @@ End Set End Property + Public Property IClassFactoryName() As String + Get + Return m_classFactoryName + End Get + Set(ByVal Value As String) + m_classFactoryName = Value + End Set + End Property + '''----------------------------------------------------------------------------- ''' <summary> ''' Initializes a new instance the of CClassMap class. @@ -772,7 +784,7 @@ 'm_deleteStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & map.ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.getValueByAttribute(map.Name)))) m_deleteStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & map.ColumnMap.getFullyQualifiedName & "=" & m_relationalDatabase.getParamHolder(i)) End If - m_deleteStatement.addSqlParameter(i, obj.getValueByAttribute(map.Name), map.ColumnMap) + m_deleteStatement.addSqlParameter(i, obj.GetValueByAttribute(map.Name), map.ColumnMap) Next i End If getDeleteSqlFor = m_deleteStatement @@ -1045,10 +1057,10 @@ For i = 1 To ClassMap.getFindSize AttrMap = ClassMap.FindAttributeMaps(i) If isFirst Then - m_selectStatement.addSqlClause("" & ClassMap.getFindAttributeMap(i).ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.getValueByAttribute(AttrMap.Name)))) + m_selectStatement.addSqlClause("" & ClassMap.getFindAttributeMap(i).ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.GetValueByAttribute(AttrMap.Name)))) isFirst = False Else - m_selectStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & ClassMap.getFindAttributeMap(i).ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.getValueByAttribute(AttrMap.Name)))) + m_selectStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & ClassMap.getFindAttributeMap(i).ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.GetValueByAttribute(AttrMap.Name)))) End If Next i ClassMap = ClassMap.SuperClass @@ -1057,9 +1069,9 @@ For i = 1 To Me.getKeySize AttrMap = Me.getKeyAttributeMap(i) If i > 1 Then - m_selectStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & AttrMap.ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.getValueByAttribute(AttrMap.Name)))) + m_selectStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & AttrMap.ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.GetValueByAttribute(AttrMap.Name)))) Else - m_selectStatement.addSqlClause("" & AttrMap.ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.getValueByAttribute(AttrMap.Name)))) + m_selectStatement.addSqlClause("" & AttrMap.ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.GetValueByAttribute(AttrMap.Name)))) End If Next i End If @@ -1113,7 +1125,7 @@ AttrMap = getIdentityAttributeMap(1) If Not AttrMap Is Nothing Then 'Identity columns must be integers. A zero value means it is not set. - If CInt(RelationalDatabase.getValueFor(obj.getValueByAttribute(AttrMap.Name))) <> 0 Then + If CInt(RelationalDatabase.getValueFor(obj.GetValueByAttribute(AttrMap.Name))) <> 0 Then includeIdentity = True End If End If @@ -1181,7 +1193,7 @@ For i = 1 To Me.getSize AttrMap = Me.getAttributeMap(i) If Not AttrMap.ColumnMap.IsIdentity Then - m_insertStatement.addSqlParameter(i, obj.getValueByAttribute(AttrMap.Name), AttrMap.ColumnMap) + m_insertStatement.addSqlParameter(i, obj.GetValueByAttribute(AttrMap.Name), AttrMap.ColumnMap) End If Next i If Not Me.SharedTableValue Is Nothing Then @@ -1246,7 +1258,7 @@ For i = 1 To Me.getSize AttrMap = Me.getAttributeMap(i) If Not AttrMap.ColumnMap.IsIdentity Then - m_updateStatement.addSqlParameter(i, obj.getValueByAttribute(AttrMap.Name), AttrMap.ColumnMap) + m_updateStatement.addSqlParameter(i, obj.GetValueByAttribute(AttrMap.Name), AttrMap.ColumnMap) End If Next i paramCount = Me.getSize @@ -1269,7 +1281,7 @@ For i = 1 To Me.getKeySize AttrMap = Me.getKeyAttributeMap(i) 'keyValuesCol.Add(m_relationalDatabase.getValueFor(obj.getValueByAttribute(AttrMap.Name))) - keyValuesCol.Add(obj.getValueByAttribute(AttrMap.Name)) + keyValuesCol.Add(obj.GetValueByAttribute(AttrMap.Name)) Next End If @@ -1300,7 +1312,7 @@ paramCount += 1 'm_updateStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & Me.getAttributeMap(i).ColumnMap.getFullyQualifiedName & m_relationalDatabase.getClauseStringEqualTo(m_relationalDatabase.getValueFor(obj.getValueByAttribute("Original" & Me.getAttributeMap(i).Name)))) m_updateStatement.addSqlClause(" " & RelationalDatabase.getClauseStringAnd & " " & Me.getAttributeMap(i).ColumnMap.getFullyQualifiedName & "=" & m_relationalDatabase.getParamHolder(paramCount)) - m_updateStatement.addSqlParameter(paramCount, obj.getValueByAttribute("Original" & Me.getAttributeMap(i).Name), Me.getAttributeMap(i).ColumnMap) + m_updateStatement.addSqlParameter(paramCount, obj.GetValueByAttribute("Original" & Me.getAttributeMap(i).Name), Me.getAttributeMap(i).ColumnMap) End If Next i End If @@ -1378,7 +1390,7 @@ End If Catch ex As Exception End Try - obj.setAttributeValue(AttrMap.Name, val) + obj.SetAttributeValue(AttrMap.Name, val) If Not IsDBNull(val) And Not val Is Nothing Then obj.Persistent = True End If @@ -1457,7 +1469,7 @@ Catch ex As Exception End Try - obj.setAttributeValue(AttrMap.Name, tmpObj) + obj.SetAttributeValue(AttrMap.Name, tmpObj) If Not IsDBNull(tmpObj) And Not tmpObj Is Nothing Then obj.Persistent = True End If @@ -1971,6 +1983,54 @@ End Property Public Function CreateObjectInstance() As IPersistableObject + If m_classFactoryName Is Nothing Then + Return CreateObjectInstanceNoFactory() + End If + + 'Try to locate the object factory + Dim obj As Object + Dim pbroker As CPersistenceBroker + If m_classFactory Is Nothing Then + Try + If AssemblyPath <> String.Empty Then + 'For late loading assemblies + Dim objHdl As ObjectHandle + If ClassNameSpace = String.Empty Then + objHdl = Activator.CreateInstanceFrom(AssemblyPath, m_classFactoryName) + Else + objHdl = Activator.CreateInstanceFrom(AssemblyPath, ClassNameSpace & "." & m_classFactoryName) + End If + obj = CType(objHdl.Unwrap, Object) + Else + Dim t As Type + Dim asmArray As [Assembly]() = AppDomain.CurrentDomain.GetAssemblies + For Each asm As [Assembly] In asmArray + Try + If ClassNameSpace = String.Empty Then + For Each tt As Type In asm.GetTypes() + If tt.Name = m_classFactoryName Then + t = tt + Exit For + End If + Next + Else + t = asm.GetType(ClassNameSpace & "." & m_classFactoryName) + End If + Catch + End Try + If Not t Is Nothing Then Exit For + Next + obj = Activator.CreateInstance(t) + End If + Catch ex As Exception + Throw New XMLMappingException("The factory for class " & Name & " could not be found") + End Try + m_classFactory = CType(obj, IClassFactory) + End If + Return CreateObjectInstanceViaFactory() + End Function + + Private Function CreateObjectInstanceNoFactory() As IPersistableObject Dim obj As Object Dim ip As IPersistableObject Dim pbroker As CPersistenceBroker @@ -2016,4 +2076,19 @@ Return ip End Function + Public Function CreateObjectInstanceViaFactory() As IPersistableObject + Dim obj As Object + Dim ip As IPersistableObject + Dim pbroker As CPersistenceBroker + + obj = m_classFactory.CreateObject + Try + ip = CType(obj, IPersistableObject) + Catch ex As Exception + pbroker = getPersistenceBrokerInstance() + pbroker.StartTracking(obj) + ip = pbroker.getInjectedObject(obj) + End Try + Return ip + End Function End Class \ No newline at end of file |