开发者

Repository Pattern implements methods by adding Add1 but should use methods of the baseclass

开发者 https://www.devze.com 2023-03-31 23:49 出处:网络
This is the original code in c# public class CategoryRepository: RepositoryBase<Category>, ICategoryRepository

This is the original code in c#

public class CategoryRepository: RepositoryBase<Category>, ICategoryRepository
    {
    public CategoryRepository(IDatabaseFactory databaseFactory)
        : base(databaseFactory)
        {
        }           
    }
public interface ICategoryRepository : IRepository<Category>
{
}

And i had to change it in VB.Net and to change it to my current situation (other object, but the principal is the same), so it became like this:

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

The problem is, it still has to implement IUserRepository, which is not possible, because i would have to rewrite my code, as it wants to add the methods (add, remove, ... that are already implemented in the RepositoryBase)

This is my base class, where the problem could be

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, Data开发者_C百科baseFactory.[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

Does anyone has an idea what i should change to let it work and let my UserRepository use the methods in RepositoryBase while implementing the IUserRepository?

Thanks in advance


VB.Net doesn't have direct casting, in contrairy to c#

The best way is to recreate all the methods and always call mybase.method

For example:

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

So, this isn't required in C#, but it is in VB.Net :)

0

精彩评论

暂无评论...
验证码 换一张
取 消