Repository Pattern通过添加Add1来实现方法,但应该使用baseclass的方法

时间:2011-08-29 09:40:30

标签: asp.net-mvc vb.net oop design-patterns

这是c#

中的原始代码
public class CategoryRepository: RepositoryBase<Category>, ICategoryRepository
    {
    public CategoryRepository(IDatabaseFactory databaseFactory)
        : base(databaseFactory)
        {
        }           
    }
public interface ICategoryRepository : IRepository<Category>
{
}

我必须在VB.Net中更改它并将其更改为我当前的情况(其他对象,但主体是相同的),所以它变成这样:

Public Class UserRepository
    Inherits RepositoryBase(Of Domain.Master.User)
    'Implements IUserRepository

    Public Sub New(databaseFactory As IDatabaseFactory)
        MyBase.New(databaseFactory)
    End Sub
End Class


Public Interface IUserRepository
    Inherits IRepository(Of User)

End Interface

问题是,它仍然必须实现IUserRepository,这是不可能的,因为我必须重写我的代码,因为它想要添加已经在RepositoryBase中实现的方法(添加,删除......) )

这是我的基类,问题可能是

Public MustInherit Class RepositoryBase(Of T As Class)

    Private m_masterContext As MyMasterContext
    Private ReadOnly dbset As IDbSet(Of T)

    Protected Sub New(databaseFactory__1 As IDatabaseFactory)
        DatabaseFactory = databaseFactory__1
        dbset = DataContext.[Set](Of T)()
    End Sub

    Protected Property DatabaseFactory() As IDatabaseFactory
        Get
            Return m_DatabaseFactory
        End Get
        Private Set(value As IDatabaseFactory)
            m_DatabaseFactory = value
        End Set
    End Property
    Private m_DatabaseFactory As IDatabaseFactory

    Protected ReadOnly Property DataContext() As MyMasterContext
        Get
            Return If(m_masterContext, (InlineAssignHelper(m_masterContext, DatabaseFactory.[Get]())))
        End Get
    End Property
    Public Overloads Sub Add(entity As T)
        dbset.Add(entity)
    End Sub
    Public Overridable Sub Update(entity As T)
        dbset.Attach(entity)
        m_masterContext.Entry(entity).State = EntityState.Modified
    End Sub
    Public Overridable Sub Delete(entity As T)
        dbset.Remove(entity)
    End Sub
    Public Overridable Sub Delete(where As Expression(Of Func(Of T, Boolean)))
        ' Dim objects As IEnumerable(Of T) = dbset.Where(Of T)(where).AsEnumerable()
        'For Each obj As T In objects
        '    dbset.Remove(obj)
        'Next
    End Sub
    Public Overridable Function GetById(id As Long) As T
        Return dbset.Find(id)
    End Function
    Public Overridable Function GetById(id As String) As T
        Return dbset.Find(id)
    End Function
    Public Overridable Function GetAll() As IEnumerable(Of T)
        Return dbset.ToList()
    End Function
    Public Overridable Function GetMany(where As Expression(Of Func(Of T, Boolean))) As IEnumerable(Of T)
        Return dbset.Where(where).ToList()
    End Function
    Public Function [Get](where As Expression(Of Func(Of T, Boolean))) As T
        Return Nothing ' dbset.Where(where).FirstOrDefault(Of T)()
    End Function
    Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
        target = value
        Return value
    End Function
End Class

有没有人知道我应该改变什么来让它工作,让我的UserRepository在实现IUserRepository时使用RepositoryBase中的方法?

提前致谢

1 个答案:

答案 0 :(得分:0)

VB.Net没有直接投射,与c#

相反

最好的方法是重新创建所有方法,并始终调用mybase.method

例如:

   Public Overrides Sub Update(entity As Domain.Slave.Klant) Implements Infrastructure.IRepository(Of Domain.Slave.Klant).Update
        MyBase.Update(entity)
    End Sub

因此,这在C#中不是必需的,但它在VB.Net中:)