1
的方法,這是在C#Repository模式通過添加ADD1實現方法,但應使用基類
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中實現的方法(add,remove,...)
這是我的基類,這裏的問題可能是
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使用方法RepositoryBase而實施IUserRepository?
在此先感謝