简体   繁体   English

调用业务层方法

[英]Call Business Layer method

I am quite new to multi-layered architecture and asp.net mvc. 我是多层架构和asp.net mvc的新手。 I would like to know which is the good practice to call the business layer method. 我想知道调用业务层方法的好习惯。

1) Calling the business layer method from the controller, populate a view model and pass the view model to the view. 1)从控制器调用业务层方法,填充视图模型并将视图模型传递给视图。

2) Calling the business layer method directly from the view. 2)直接从视图中调用业务层方法。

Please let me know the advantages and disadvantages in both methods. 请让我知道这两种方法的优缺点。 It would be grateful if explained "In which scenarios the either methods are used" 如果解释“在哪种情况下使用任何一种方法”,将不胜感激

This is bordering on an opinion seeking question but I think it's worth an explanation, albeit you can find this with enough research. 这是一个寻求意见的问题,但我认为值得一个解释,尽管你可以通过足够的研究找到它。

Option 1 is the way to go, that much is probably +80% agreed to. 选项1是要走的路,可能会达到+ 80%的同意。

Anymore and you run into the 'it depends' answer. 再过一次,你会遇到'依赖'的答案。 It depends on how much you need to break things out. 这取决于你需要多少才能解决问题。 Academically you should be loosely coupled with high cohesion. 在学术上你应该松散地加上高凝聚力。 All well and good but in the real world there is rarely an absolute because perfect architecture is more a matter of 'just in time' design than academics, else you'll never get things into production. 一切都很好,但在现实世界中很少有绝对的,因为完美的建筑更多的是“及时”设计而不是学术,否则你永远不会把东西投入生产。 So you as the developer have to make the judgment call as to where you compromise (and you will). 所以你作为开发者必须判断你妥协的地方(你会)。

So here's where it gets opinion based: 所以这里有基于意见的地方:

a) Loose coupling with high cohesion means better testability / maintainability / scalability at the expense of greater abstraction, ie more development time up front. a)具有高内聚力的松散耦合意味着更好的可测试性/可维护性/可扩展性,代价是更大的抽象,即更多的开发时间。

b) The reverse means less all the goods, but also less development time up front, aka 'just get it out the door and stop telling me (your boss) about good architecture damnit!' b)相反意味着减少所有商品,但前期开发时间也减少了,也就是说“只是把它拿出来,停止告诉我(你的老板)关于好建筑的死讯!”

A good developer is always striving for a). 一个优秀的开发人员总是在努力争取a)。 Problem is most of us have to balance that with making progress so we tend to slide towards b) (begrudgingly) 问题是我们大多数人必须在取得进展的同时保持平衡,因此我们倾向于向b)(不情愿)

So for the sake of further discussion (if you're still reading)...An example of a), and there are plenty of other right a's, this is just one flavor: 因此,为了进一步讨论(如果你还在阅读)... a)的一个例子,还有很多其他的权利,这只是一种风格:

Your view model is suppose to contain all the things necessary to render the UI of flavor, minimal data needed for view + functions called directly from UI. 您的视图模型假设包含呈现flavor的UI所需的所有内容,直接从UI调用的view +函数所需的最少数据。 It should not represent the business logic pertaining to the domain models. 它不应代表与域模型相关的业务逻辑。 Domain models should represent the data necessary to, well, represent the domain they encompass. 域模型应该代表必要的数据,以代表它们所包含的域。 Business logic layer, referring to that here as the service layer, should be the functions that operate on domains. 业务逻辑层(在此处称为服务层)应该是在域上运行的功能。

Ze example: 泽的例子:

Lets say we have a user who has multiple accounts. 假设我们有一个拥有多个帐户的用户。 There would be a User class and Account class. 会有一个User类和Account类。 These are combined in a UserAccounts class (a User property and Accounts property (array). 它们组合在UserAccounts类(User属性和Accounts属性(数组))中。

Depending on how you decide to domain there is either a UserService class or a UserService + AccountService + UserAccountService class combo. 根据您决定使用域的方式,可以使用UserService类或UserService + AccountService + UserAccountService类组合。

Then let's assume you have user (UserView class) and user detail (UserDetailView class) views. 然后让我们假设您有用户(UserView类)和用户详细信息(UserDetailView类)视图。 UserView is the User class + an aggregate representation of accounts (but not all details) + UI direct call functions. UserView是User类+帐户的聚合表示(但不是所有细节)+ UI直接调用函数。 The UserDetailView represents the User + details of each account + UI direct call functions. UserDetailView表示每个帐户的用户+详细信息+ UI直接调用功能。

The User controller has two functions (plenty more for sure but just these for explanation), getUsers and getUserDetail. 用户控制器有两个功能(更多肯定,但只是这些解释),getUsers和getUserDetail。

  • GetUsers calls the user serivce which returns an array of UserAccounts objects which is then passed through a model builder that returns a array of UserView objects which the controller then passes to the 'summary' view. GetUsers调用用户serivce,它返回一个UserAccounts对象数组,然后通过模型构建器传递,该构建器返回一个UserView对象数组,然后控制器将其传递给“summary”视图。
  • GetUserDetail calls the user service which returns a UserAccounts object which is then passed through a model builder that returns a UserDetailView object which the controller then passes to the 'detailed' view. GetUserDetail调用返回UserAccounts对象的用户服务,然后该对象通过模型构建器传递,该构建器返回UserDetailView对象,然后控制器将其传递给“详细”视图。

With MVC your processing (like calling and populating a model) will fall within the model itself. 使用MVC,您的处理(如调用和填充模型)将属于模型本身。 The controller needs to be as "dumb" as possible, essentially just there to say "Okay I need THIS model and send it to THAT view" 控制器需要尽可能“愚蠢”,基本上只是说“好吧我需要这个模型并将其发送到那个视图”

Here is a VB.NET version of one of my models (I use a customized version of StackOverflow's Dapper ORM as my Database Layer. This is an unfinished model btw, and the population of the user info isn't contained in it yet): 这是我的一个模型的VB.NET版本(我使用自定义版本的StackOverflow的Dapper ORM作为我的数据库层。这是一个未完成的模型顺便说一句,用户信息的总体还没有包含在其中):

Public Class UserAccount    
    Public Property Employee_ID() As Integer
    Public Property Email_Address() As String
    Public Property Location_ID() As String
    Public Property Department_ID() As String
    Public Property Company_ID() As String
    Public Property Password_Expiry() As DateTime
    Public Property Win_User_Name() As String
    Public Property First_Name() As String
    Public Property Last_Name() As String
    Public Property Message_Number() As Integer
    Public Property Message_Text() As String
    Public Property Password() As String

    Public Overrides Function ToString() As String
        Dim serializer As JavaScriptSerializer = New JavaScriptSerializer
        Dim result As String = serializer.Serialize(Me)
        Return result
    End Function

    Public Function FromString(str As String) As UserAccount
        Dim serializer As JavaScriptSerializer = New JavaScriptSerializer
        Return serializer.Deserialize(Of UserAccount)(str)
    End Function

    Public Function GetUserInfo() As UserAccount 'Returns user info as a strongly typed object from what is saved in the cookie
        Dim user As UserAccount
        Dim cookie As HttpCookie = DirectCast(System.Web.HttpContext.Current.Request.Cookies(FormsAuthentication.FormsCookieName), HttpCookie)
        Dim ticket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(cookie.Value)

        user = Me.FromString(ticket.UserData)

        Return user
    End Function

    Public Shared Function Encrypt(password As String, Win_User_Name As String)

        Dim _password As New StringBuilder
        Try

           'encryption stuff happens here
        Catch er As Exception
            Return ""
        End Try

        Return _password.ToString

    End Function

End Class

Now all the controller needs to do is instantiate this model and give the data to the view 现在,控制器需要做的就是实例化该模型并将数据提供给视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM