Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC-ViewModel是进行小计算的合适位置吗?_Asp.net Mvc_Viewmodel - Fatal编程技术网

Asp.net mvc ASP.NET MVC-ViewModel是进行小计算的合适位置吗?

Asp.net mvc ASP.NET MVC-ViewModel是进行小计算的合适位置吗?,asp.net-mvc,viewmodel,Asp.net Mvc,Viewmodel,我正在开发一个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 Pr

我正在开发一个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代码阅读技能开始让我难以捉摸:-))。另一方面,如果它是一些领域逻辑,那么它可能更适合模型,以便可以重用。

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

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


使用诸如AutoMapper之类的工具,这一点非常容易做到。

我正在做的大部分工作就是。。。格式化。然而,在我的events ViewModel中,我还将所有我的“纬度”/“经度”项添加到一个变量中,然后除以记录数。。。这样我可以动态地得到地图的中心。请参阅第一个代码块()-我将该代码移到了ViewModel中。@Rocking对我来说听起来不够小。在旁注上。我讨厌人们在没有仔细考虑的情况下编辑标签。我的例子是在VB.NET中,但这并不意味着我的问题与VB.NET有关。这个问题是关于原则的,但是添加了一个VB.NET标记,它会阻止合格(非VB)的人看这个问题。因此,为了所有神圣的事物,如果问题不依赖于特定语言的答案,请不要编辑标签以包含代码语言!!!