Я пытаюсь следовать учебнику , найденному здесь , для реализации сервисного уровня в моем приложении MVC. Чего я не могу понять, так это связать все это.
вот что у меня есть.
IUserRepository.vb
Namespace Data
Public Interface IUserRepository
Sub AddUser(ByVal openid As String)
Sub UpdateUser(ByVal id As Integer, ByVal about As String, ByVal birthdate As DateTime, ByVal openid As String, ByVal regionid As Integer, ByVal username As String, ByVal website As String)
Sub UpdateUserReputation(ByVal id As Integer, ByVal AmountOfReputation As Integer)
Sub DeleteUser(ByVal id As Integer)
Function GetAllUsers() As IList(Of User)
Function GetUserByID(ByVal id As Integer) As User
Function GetUserByOpenID(ByVal openid As String) As User
End Interface
End Namespace
UserRepository.vb
Namespace Data
Public Class UserRepository : Implements IUserRepository
Private dc As DataDataContext
Public Sub New()
dc = New DataDataContext
End Sub
#Region "IUserRepository Members"
Public Sub AddUser(ByVal openid As String) Implements IUserRepository.AddUser
Dim user = New User
user.LastSeen = DateTime.Now
user.MemberSince = DateTime.Now
user.OpenID = openid
user.Reputation = 0
user.UserName = String.Empty
dc.Users.InsertOnSubmit(user)
dc.SubmitChanges()
End Sub
Public Sub UpdateUser(ByVal id As Integer, ByVal about As String, ByVal birthdate As Date, ByVal openid As String, ByVal regionid As Integer, ByVal username As String, ByVal website As String) Implements IUserRepository.UpdateUser
Dim user = (From u In dc.Users
Where u.ID = id
Select u).Single
user.About = about
user.BirthDate = birthdate
user.LastSeen = DateTime.Now
user.OpenID = openid
user.RegionID = regionid
user.UserName = username
user.WebSite = website
dc.SubmitChanges()
End Sub
Public Sub UpdateUserReputation(ByVal id As Integer, ByVal AmountOfReputation As Integer) Implements IUserRepository.UpdateUserReputation
Dim user = (From u In dc.Users
Where u.ID = id
Select u).FirstOrDefault
''# Simply take the current reputation from the select statement
''# and add the proper "AmountOfReputation"
user.Reputation = user.Reputation + AmountOfReputation
dc.SubmitChanges()
End Sub
Public Sub DeleteUser(ByVal id As Integer) Implements IUserRepository.DeleteUser
Dim user = (From u In dc.Users
Where u.ID = id
Select u).FirstOrDefault
dc.Users.DeleteOnSubmit(user)
dc.SubmitChanges()
End Sub
Public Function GetAllUsers() As System.Collections.Generic.IList(Of User) Implements IUserRepository.GetAllUsers
Dim users = From u In dc.Users
Select u
Return users.ToList
End Function
Public Function GetUserByID(ByVal id As Integer) As User Implements IUserRepository.GetUserByID
Dim user = (From u In dc.Users
Where u.ID = id
Select u).FirstOrDefault
Return user
End Function
Public Function GetUserByOpenID(ByVal openid As String) As User Implements IUserRepository.GetUserByOpenID
Dim user = (From u In dc.Users
Where u.OpenID = openid
Select u).FirstOrDefault
Return user
End Function
#End Region
End Class
End Namespace
IUserService.vb
Namespace Data
Interface IUserService
End Interface
End Namespace
UserService.vb
Namespace Data
Public Class UserService : Implements IUserService
Private _ValidationDictionary As IValidationDictionary
Private _repository As IUserRepository
Public Sub New(ByVal validationDictionary As IValidationDictionary, ByVal repository As IUserRepository)
_ValidationDictionary = validationDictionary
_repository = repository
End Sub
Protected Function ValidateUser(ByVal UserToValidate As User) As Boolean
Dim isValid As Boolean = True
If UserToValidate.OpenID.Trim().Length = 0 Then
_ValidationDictionary.AddError("OpenID", "OpenID is Required")
isValid = False
End If
If UserToValidate.MemberSince = Nothing Then
_ValidationDictionary.AddError("MemberSince", "MemberSince is Required")
isValid = False
End If
If UserToValidate.LastSeen = Nothing Then
_ValidationDictionary.AddError("LastSeen", "LastSeen is Required")
isValid = False
End If
If UserToValidate.Reputation = Nothing Then
_ValidationDictionary.AddError("Reputation", "Reputation is Required")
isValid = False
End If
Return isValid
End Function
End Class
End Namespace
Я также подключил IValidationDictionary.vb и ModelStateWrapper.vb , как описано в статье выше.
У меня проблема с реализацией в моем контроллере. Мой контроллер выглядит примерно так.
Public Class UsersController : Inherits BaseController
Private UserService As Data.IUserService
Public Sub New()
UserService = New Data.UserService(New Data.ModelStateWrapper(Me.ModelState), New Data.UserRepository)
End Sub
Public Sub New(ByVal service As Data.IUserService)
UserService = service
End Sub
....
End Class
однако в строке с надписью Public Sub New(ByVal service As Data.IUserService)
я получаю сообщение об ошибке
«service» не может предоставить тип «Data.IUserService» вне проекта через класс «UsersController»
Итак, мой вопрос - ДВА ЧАСТИ
- Как правильно реализовать Service Layer в моем приложении, используя концепции из этой статьи?
- Должен ли быть какой-либо контент в моем IUserService.vb ?