gpt4 book ai didi

asp.net-mvc - ASP.NET MVC - ViewModel 是否适合进行较小的计算?

转载 作者:行者123 更新时间:2023-12-01 14:41:38 25 4
gpt4 key购买 nike

我正在研究 UserViewModel,我想知道在 VM 中进行少量计算是否合适,或者我是否需要进一步分离它并在其他地方计算。

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

最佳答案

如果这些计算涉及给定 View 的格式,那么它就是确切的位置。看起来你正在做这个:格式化 View 是好的(抱歉,如果我错过了什么,我的 VB.NET 代码阅读技能开始让我失望:-))。另一方面,如果它是一些域逻辑,它可能更适合该模型,以便可以重用。

关于asp.net-mvc - ASP.NET MVC - ViewModel 是否适合进行较小的计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4119388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com