Generic repository interface for entity framework 4

Huy Nguyen has a great write up on implementing a generic repository interface that supports Entity Framework 4. His code is written in C# and today I had to rewrite it in VB.Net. The VB.Net version looks like this

Public Interface IRepository
    Inherits IDisposable

    Function GetQuery(Of TEntity As Class)As IQueryable(Of TEntity)
    Function GetAll(Of TEntity As Class)() As IEnumerable(Of TEntity)
    Function Find(Of TEntity As Class)(predicate As Expression(Of Func(Of TEntity, Boolean))) As IEnumerable(Of TEntity)
    Function [Single](Of TEntity As Class)(predicate As Expression(Of Func(Of TEntity, Boolean))) As TEntity
    Function First(Of TEntity As Class)(predicate As Expression(Of Func(Of TEntity, Boolean))) As TEntity
    Sub Add(Of TEntity)(entity As TEntity)
    Sub Delete(Of TEntity)(entity As TEntity)
    Sub Attach(Of TEntity)(entity As TEntity)
    Sub SaveChanges()
    Sub SaveChanges(options As SaveOptions)

End Interface

Worth noting is that Single is a keyword in VB.Net and needs to be enclosed in brackets, also the generics and linq syntax in VB is very verbose. Calling the generic functions is just as bad…

repositoryInstance.Find(Of EntityType)(Function(entity As EntityType) entity.codehere)

Unfortunatetly you cannot use multiline functions for the expression in the current version of VB.Net.

Leave A Comment...

*