THREE Principal Layers

zhaozj2021-02-16  192

Presentation logic is about how to handle the interaction between the user and the software. This can be as simple as a command-line or text-based menu system, but these days it's more likely to be a rich-client graphics UI or an HTML -based browser UI. (In this book I use rich client to mean a Windows / Swing / fat-client UI, as opposed to an HTML browser.) The primary responsibilities of the presentation layer are to display information to the user and to interpret Commands from the user INTO Actions Upon the domain and data source.

TABLE 1.1. Three Principal Layers

Layer Responsibilities Presentation Provision of services, display of information (eg, in Windows or HTML, handling of user request (mouse clicks, keyboard hits), HTTP requests, command-line invocations, batch API) Domain Logic that is the real point of the System Data Source Communication with Databases, Messaging Systems, Transaction Managers, Other Packages

Data source logic is about communicating with other systems that carry out tasks on behalf of the application. These can be transaction monitors, other applications, messaging systems, and so forth. For most enterprise applications the biggest piece of data source logic is a database that Is Primarily Responsible for Storing Persistent Data.

The remaining piece is the domain logic, also referred to as business logic. This is the work that this application needs to do for the domain you're working with. It involves calculations based on inputs and stored data, validation of any data that comes In from The Presentation, And Figuring Out Exactly What Data Source Logic To Dispatch, Depending On Commands Received from The Presentation.

Sometimes the layers are arranged so that the domain layer completely hides the data source from the presentation. More often, however, the presentation accesses the data store directly. While this is less pure, it tends to work better in practice. The presentation may interpret a command from the user, use the data source to pull the relevant data out of the database, and then let the domain logic manipulate that data before presenting it on the glass.A single application can often have multiple packages of each of these three subject . areas An application designed to be manipulated not only by end users through a rich-client interface but also through a command line would have two presentations:. one for the rich-client interface and one for the command line Multiple data source components may be Present For DiffERENT DATABASES, but Would Be Particularly for Communication with existing packages. Even The Domain May Be Broken Into DistINCT AREAS Relatively Separate from Each Other. CE Rtain Data Source Packages May Only Be buying by Certain Domain package.

So far I've talked about a user. This naturally raises the question of what happens when there is no a human being driving the software. This could be something new and fashionable like a Web service or something mundane and useful like a batch process. in the latter case the user is the client program. At this point it becomes apparent that there is a lot of similarity between the presentation and data source layers in that they both are about connection to the outside world. This is the logic behind Alistair Cockburn's Hexagonal Architecture pattern [wiki], which visualizes any system as a core surrounded by interfaces to external systems. In Hexagonal Architecture everything external is fundamentally an outside interface, and thus it's a symmetrical view rather than my asymmetric layering scheme.I find this asymmetry useful , HOWEVER, BECAUSE I Think there is a good distinction to Be Made Between An Interface you provide as a service to others and your use of someone else's serv ice. Driving down to the core, this is the real distinction I make between presentation and data source. Presentation is an external interface for a service your system offers to someone else, whether it be a complex human or a simple remote program. Data source Is The Interface To Things That Are Providing a Service To You. I Find It Beneficial To Think About The Differently Because The Difference In Clients Alters The Way You Think About The Service.

Although we can identify the three common responsibility layers of presentation, domain, and data source for every enterprise application, how you separate them depends on how complex the application is. A simple script to pull data from a database and display it in a Web page may all be one procedure. I would still endeavor to separate the three layers, but in that case I might do it only by placing the behavior of each layer in separate subroutines. As the system gets more complex, I would break the three layers into separate classes. As complexity increased I would divide the classes into separate packages. My general advice is to choose the most appropriate form of separation for your problem but make sure you do some kind of separation stockade t least at the subroutine level.Together with the Separation, There's Also A Steady Rule About Dependencies: The Domain and Data Source Should Never Be Depend on The Presentation. That IS, There Should Be No Subroutine Call from The Domain OR DAT a source code into the presentation code. This rule makes it easier to substitute different presentations on the same foundation and makes it easier to modify the presentation without serious ramifications deeper down. The relationship between the domain and the data source is more complex and depends upon The Architectural Patterns Used for The Data Source.

One of the hardest parts of working with domain logic seems to be that people often find it difficult to recognize what is domain logic and what is other forms of logic. An informal test I like is to imagine adding a radically different layer to an application, such as a command-line interface to a Web application. If there's any functionality you have to duplicate in order to do this, that's a sign of where domain logic has leaked into the presentation. Similarly, do you have to duplicate logic to replace a relational database with an XML file? A good example of this is a system I was told about that contained a list of products in which all the products that sold over 10 percent more than they did the previous month were colored in red. To do this ......................... ..

The trouble is that that's putting domain logic into the presentation. To properly separate the layers you need a method in the domain layer to indicate if a product has improving sales. This method does the comparison between the two months and returns a Boolean value. The presentation layer then simply calls this Boolean method and, if true, highlights the product in red That way the process is broken into its two parts:. deciding whether there is something highlightable and choosing how to highlight.

转载请注明原文地址:https://www.9cbs.com/read-8672.html

New Post(0)