首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC -- ViewModel是否适合进行细微的计算?

MVC -- ViewModel是否适合进行细微的计算?
EN

Stack Overflow用户
提问于 2010-11-08 03:52:47
回答 2查看 735关注 0票数 2

我正在UserViewModel上工作,我想知道是否适合在VM中进行细微的计算,或者我是否需要进一步分离它并在其他地方进行计算。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-08 03:55:37

如果这些计算涉及给定视图的格式,那么它就是确切的位置。看起来您正在做这件事:格式化视图,这是可以的(如果我错过了什么,很抱歉,我的VB.NET代码阅读技能开始让我摸不着头脑了:-))。另一方面,如果它是一些领域逻辑,那么它可能更适合模型,这样就可以重用它。

票数 5
EN

Stack Overflow用户

发布于 2010-11-08 04:14:43

虽然我不反对Darin的观点,但还有另一种方法。

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

使用像AutoMapper这样的工具可以很容易地做到这一点。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4119388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档