Fishy wrote:
I'm trying to determine which is the best practice for using the adapter. Currently I use manager classes for my bl. The following is a snippet from one of those classes:
Private Shared _Adapter As New DataAccessAdapter
Public Shared Function GetAllSchools() As EntityCollection
Dim Sorter As ISortExpression = New SortExpression
Sorter.Add(SortClauseFactory.Create(Elementary.GradeBook.LLBL.TermFieldIndex.StartDate, SortOperator.Ascending))
Dim Sorter2 As ISortExpression = New SortExpression
Sorter2.Add(SortClauseFactory.Create(Elementary.GradeBook.LLBL.SpecialScoreFieldIndex.SpecialScoreId, SortOperator.Ascending))
Dim PrefetchPath As IPrefetchPath2 = New PrefetchPath2(CType(EntityType.SchoolEntity, Integer))
PrefetchPath.Add(SchoolEntity.PrefetchPathCalendar).SubPath.Add(CalendarEntity.PrefetchPathTerm, Nothing, Nothing, Nothing, Sorter)
PrefetchPath.Add(SchoolEntity.PrefetchPathSpecialScore, Nothing, Nothing, Nothing, Sorter2)
GetAllSchools = New EntityCollection(New SchoolEntityFactory)
_Adapter.FetchEntityCollection(GetAllSchools, Nothing, Nothing, Nothing, PrefetchPath)
Return GetAllSchools
End Function
Should I be doing it this way? Should I create a new adapter in each routine? Should I create one adapter for the entire bl and not for each manager class?
Thanks,
Fishy
btw, Frans, I think you may have misspelled 'Practises' in your documentation
I haven't tested this, but I think you'll run into problems having a shared/static adapter. The main problem, I think, is the fact that, under Adapter, transactions are nested in the Adapter object itself. So, if you have two calls coming in for data, and one calls Adapter.StartTransaction(), both will be running under that transaction (and getting rolled back, in the event the commit fails). Badness.
I have a simple method called "GetAdapter()" made available in my base manager class that returns a fresh Adapter initialized with the current connection string. I use this adapter for each method in my BL, then dispose of it in a Finally block. This is VB, of course, whereas I would just use a Using block in C#.
Jeff...