ibatis-user-cs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Schall <mike.sch...@gmail.com>
Subject Re: .NET 2.0 and Generics
Date Fri, 20 Jan 2006 21:17:43 GMT
I would agree.  I want generic support.  These should help you get by
till then...

Public Function Load(Of T As New)( _
   ByVal queryName As String, _
   ByVal parameters As IDictionary(Of String, Object)) As T

	Dim mapper As SqlMapper = DatabaseManager.Instance()
	Dim rv As T

	rv = DirectCast(mapper.QueryForObject(queryName,
DirectCast(parameters, Object))

	Return rv

End Function

Public Function Enumerate(Of T As New)( _
   ByVal queryName As String, _
   ByVal parameters As IDictionary(Of String, Object)) As IEnumerable(Of T)

	Dim mapper As SqlMapper = DatabaseManager.Instance()

	Dim collector As TypedEnumerateCollector(Of T) = New
TypedEnumerateCollector(Of T)

	mapper.QueryWithRowDelegate(queryName, parameters, New
SqlMapper.RowDelegate(AddressOf collector.Collect))

	Return collector.Collected()

End Function

Private Class TypedEnumerateCollector(Of T)

	Private _list As IList(Of T)

	Public Sub New()
		_list = New List(Of T)
	End Sub

	Public Sub Collect(ByVal rowObject As Object, ByVal parameterObject
As Object, ByVal junkList As IList)
		_list.Add(DirectCast(rowObject, T))
	End Sub

	Public Function Collected() As IEnumerable(Of T)
		Return _list
	End Function

End Class

Public Function Map(Of Tk, Tv As New)( _
 ByVal queryName As String, _
 ByVal parameters As IDictionary(Of String, Object), _
 ByVal keySel As KeySelector(Of Tk, Tv)) As IDictionary(Of Tk, Tv)

	Dim mapper As SqlMapper = DatabaseManager.Instance()
	Dim collector As TypedMapCollector(Of Tk, Tv) = New
TypedMapCollector(Of Tk, Tv)(keySel)

	mapper.QueryWithRowDelegate(queryName, parameters, New
SqlMapper.RowDelegate(AddressOf collector.Collect))

	Return collector.Collected()

End Function

Public Delegate Function KeySelector(Of Tk, Tv)(ByVal v As Tv) As Tk

Private Class TypedMapCollector(Of Tk, Tv)

	Private _map As IDictionary(Of Tk, Tv)
	Private _keySelector As KeySelector(Of Tk, Tv)

	Public Sub New(ByVal keySel As KeySelector(Of Tk, Tv))
		_map = New Dictionary(Of Tk, Tv)
		_keySelector = keySel
	End Sub

	Public Sub Collect(ByVal rowObject As Object, ByVal parameterObject
As Object, ByVal junkList As IList)
		Dim v As Tv = DirectCast(rowObject, Tv)
		Dim k As Tk = _keySelector(v)
		_map.Add(k, v)
	End Sub

	Public Function Collected() As IDictionary(Of Tk, Tv)
		Return _map
	End Function

End Class

Mime
View raw message