【问题标题】:ASP.NET MVC - Is a ViewModel an appropriate place to do minor calculations?ASP.NET MVC - ViewModel 是否适合进行少量计算?
【发布时间】:2010-11-07 19:52:47
【问题描述】:

我正在开发一个 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

【问题讨论】:

  • 附注。当人们在没有真正考虑清楚的情况下编辑标签时,我讨厌它。我的示例是在 VB.NET BUT 中,这并不意味着我的问题与 VB.NET 有任何关系。这个问题是关于原则的,但是添加了一个 VB.NET 标签,它将阻止合格(非 VB)的人查看这个问题。因此,请出于对所有神圣事物的热爱,如果问题不依赖于特定语言的答案,请不要编辑标签以包含代码语言!!!

标签: asp.net-mvc viewmodel


【解决方案1】:

如果这些计算涉及给定视图的格式,那么它就是正确的位置。看来您正在这样做:格式化视图没问题(抱歉,如果我遗漏了什么,我的 VB.NET 代码阅读技巧开始让我难以理解 :-))。另一方面,如果它是一些领域逻辑,它可能更适合模型,以便可以重用。

【讨论】:

  • 我正在做的大部分事情就是……格式化。然而,在我的事件 ViewModel 中,我还将我所有的“纬度”/“经度”项目添加到一个变量中,然后除以记录数......这样我就可以动态地获取地图的中心。查看第一个代码块 (stackoverflow.com/questions/4115180/…) - 我将该代码移至 ViewModel。
  • @rockin 这对我来说听起来不够小。
【解决方案2】:

虽然我不同意 Darin,但还有另一种方法。

除了将简单的逻辑放入 ViewModel 中,您还可以将该逻辑放入用于将域模型对象转换为 dto 或 viewmodel 对象的任何层中。让我们称它为您的映射层。这使您的视图模型真正愚蠢和灵活,同时将所有自定义视图转换逻辑保持在单独的位置。

使用诸如 AutoMapper 之类的工具可以很容易地做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    相关资源
    最近更新 更多