ASP.NET MVC - ViewModel 是否适合进行少量计算?
我正在研究 UserViewModel,我想知道是否适合在虚拟机中进行少量计算,或者我是否需要将其进一步分离并在其他地方计算。
Public Class UserViewModel
Public Property UserName As String
Public Property Email As String
Public Property Website As String
Public Property ID As Integer
Public Property OpenIds As List(Of OpenID)
Public Property UserAge As String
Public Property About As String
Public Property Slug As String
Public Property LastSeen As String
Public Property Region As String
Public Property MemberSince As String
Public Property Reputation As String
Public Property isUserMatch As Boolean = False
Private MarkDownSharp As MarkdownSharp.Markdown
Public Sub New(ByVal user As User)
Dim currentuser As Authentication.AuthUserData = Authentication.CustomAuthentication.RetrieveAuthUser
MarkDownSharp = New MarkdownSharp.Markdown
With MarkDownSharp
.AutoHyperlink = False
.AutoNewLines = True
.EncodeProblemUrlCharacters = True
.LinkEmails = True
.StrictBoldItalic = True
End With
_UserName = If(Not user.UserName Is Nothing, user.UserName, "User" & user.ID.ToString)
_Email = user.Email
_Website = user.WebSite
_ID = user.ID
_OpenIds = user.OpenIDs.ToList
''# Converts the users birthdate to an age representation
''# IE: 29
_UserAge = user.BirthDate.ToAge
''# Because some people can be real ass holes and try to submit bad
''# data (scripts and shitè) we have to modify the "About" content in
''# order to sanitize it. At the same time, we transform the Markdown
''# into valid HTML. The raw input is stored without sanitization in
''# the database. this could mean Javascript injection, etc, so the
''# output ALWAYS needs to be sanitized.
_About = Trim(Utilities.HtmlSanitizer.Sanitize(MarkDownSharp.Transform(user.About)))
''# Removes spaces from Usernames in order to properly display the
''# username in the address bar
_Slug = Replace(user.UserName, " ", "-")
''# Returns a boolean result if the current logged in user matches the
''# details view of the user in question. This is done so that we can
''# show the edit button to logged in users.
_isUserMatch = If(currentuser.ID = user.ID, True, False)
''# Grabs the users registration data and formats it to a time span
''# The "timeago-nosuffix" CssClass is there to remove the "ago"
''# suffix from the "member for" string. Cuz when you think about
''# it... "Member for 5 days ago" just sounds stupid.
_MemberSince = user.MemberSince.ToTimeSpan("timeago-nosuffix")
''# Grabs the users last activity and formats it to a time span
_LastSeen = user.ActivityLogs.Reverse.FirstOrDefault.ActivityDate.ToTimeSpan("timeago", "ago")
''# Formats the users reputation to a comma Deliminated number
''# IE: 19,000 or 123k
_Reputation = user.Reputation.ToShortHandNumber
''# Get the name of the users current Region.
_Region = user.Region.Region.FirstOrDefault
End Sub
End Class
I'm working on a UserViewModel, and I'm wondering if it's appropriate to do minor calculations in the VM, or if I need to separate it further and calculate elsewhere.
Public Class UserViewModel
Public Property UserName As String
Public Property Email As String
Public Property Website As String
Public Property ID As Integer
Public Property OpenIds As List(Of OpenID)
Public Property UserAge As String
Public Property About As String
Public Property Slug As String
Public Property LastSeen As String
Public Property Region As String
Public Property MemberSince As String
Public Property Reputation As String
Public Property isUserMatch As Boolean = False
Private MarkDownSharp As MarkdownSharp.Markdown
Public Sub New(ByVal user As User)
Dim currentuser As Authentication.AuthUserData = Authentication.CustomAuthentication.RetrieveAuthUser
MarkDownSharp = New MarkdownSharp.Markdown
With MarkDownSharp
.AutoHyperlink = False
.AutoNewLines = True
.EncodeProblemUrlCharacters = True
.LinkEmails = True
.StrictBoldItalic = True
End With
_UserName = If(Not user.UserName Is Nothing, user.UserName, "User" & user.ID.ToString)
_Email = user.Email
_Website = user.WebSite
_ID = user.ID
_OpenIds = user.OpenIDs.ToList
''# Converts the users birthdate to an age representation
''# IE: 29
_UserAge = user.BirthDate.ToAge
''# Because some people can be real ass holes and try to submit bad
''# data (scripts and shitè) we have to modify the "About" content in
''# order to sanitize it. At the same time, we transform the Markdown
''# into valid HTML. The raw input is stored without sanitization in
''# the database. this could mean Javascript injection, etc, so the
''# output ALWAYS needs to be sanitized.
_About = Trim(Utilities.HtmlSanitizer.Sanitize(MarkDownSharp.Transform(user.About)))
''# Removes spaces from Usernames in order to properly display the
''# username in the address bar
_Slug = Replace(user.UserName, " ", "-")
''# Returns a boolean result if the current logged in user matches the
''# details view of the user in question. This is done so that we can
''# show the edit button to logged in users.
_isUserMatch = If(currentuser.ID = user.ID, True, False)
''# Grabs the users registration data and formats it to a time span
''# The "timeago-nosuffix" CssClass is there to remove the "ago"
''# suffix from the "member for" string. Cuz when you think about
''# it... "Member for 5 days ago" just sounds stupid.
_MemberSince = user.MemberSince.ToTimeSpan("timeago-nosuffix")
''# Grabs the users last activity and formats it to a time span
_LastSeen = user.ActivityLogs.Reverse.FirstOrDefault.ActivityDate.ToTimeSpan("timeago", "ago")
''# Formats the users reputation to a comma Deliminated number
''# IE: 19,000 or 123k
_Reputation = user.Reputation.ToShortHandNumber
''# Get the name of the users current Region.
_Region = user.Region.Region.FirstOrDefault
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这些计算涉及给定视图的格式,那么它就是确切的位置。看起来你正在做的就是这样:格式化视图,这是可以的(抱歉,如果我错过了一些东西,我的 VB.NET 代码阅读技能开始让我困惑:-))。另一方面,如果它是一些领域逻辑,它可能更适合该模型,以便可以重用。
If those calculations concern formatting for the given view then it is the exact place. It seems you are doing exactly this: formatting for the view which is OK (sorry if I've missed something, my VB.NET code reading skills start to elude me :-)). If on the other hand it is some domain logic it is probably better suited to the model so that it can be reused.
虽然我不同意达林的观点,但还有另一种方法。
您还可以将该逻辑放入用于将域模型对象转换为 dto 或 viewmodel 对象的任何层中,而不是将简单的逻辑放入 ViewModel 中。我们将其称为“映射层”。这使您的视图模型变得非常愚蠢和灵活,同时将所有自定义视图转换逻辑保留在单独的位置。
使用 AutoMapper 等工具可以轻松做到这一点。
While I don't disagree with Darin there is another approach.
Instead of putting simple logic into your ViewModels you could also put that logic in whatever layer you use to transform domain model objects into dto or viewmodel objects. Lets call this your Mapping layer. This keeps your viewmodels really dumb and flexible while keeping all of the custom view transformation logic in a separate place.
Using a tool such as AutoMapper makes this really easy to do.