|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
General Java Integrating and Mapping a Web Application MVC Pattern
Integrating and Mapping a Web Application MVC Pattern
Oct. 1, 2001 12:00 AM
As Java technology has matured over the last few years so have we. We've learned that building complex enterprise applications that respond to change requires more than standardized APIs and virtual machines. Fortunately, we're now starting to see the widespread adoption of best practices, patterns, and even frameworks with templates and prebuilt components. This article looks at the MVC design pattern and reviews its implementation in Struts, a presentation-tier application framework, as well as recognizing analogies of MVC to a well-formed, EJB-tier framework that Struts can be integrated with.
MVC for J2EE When we integrate with a business tier, the recommended pattern is to access a Session Façade or Business Service - analogous to an MVC view. The model could be thought of as our data access objects or entity beans. The model can be queried by the view, but updates occur through the controller, which would then represent our business logic. When we consider the applicability of the MVC pattern to the business tier, it enables us to recognize the separation of responsibilities between the interface, model, and business logic. It demarcates responsibilities across our infrastructure components. We see, for example, how to use different interfaces or views for the same model or controller.
Struts Implementation of MVC Figure 1 illustrates the interaction between the key Struts components as they relate to editing user information. Figure 2 illustrates how each major component of Struts maps into the MVC.
The Model
The View Listing 2 provides an example of a JSP page with Struts tags.
The Controller The developer extends the Action class to implement logical units of work that will be invoked by the ActionServlet upon receiving a request. Saving user information may be part of the SaveUserAction (see Listing 3). UserDelegate is a class based on the J2EE Business Delegate design pattern. It hides the implementation of the business-tier's view of the model and will be explained in detail later in this article. The important thing to note is how the Struts model interacts with the business-tier's view - the action is the bridge that joins the two. The ActionServlet is made aware of the URI to Action mappings via an XML configuration file. This file describes request URIs and how the ActionServlet should dispatch them. A typical mapping is provided in Listing 4. When the ActionServlet receives the request such as http://my- app/SaveUser.do from the Edit- User.jsp, it understands that its mapping corresponds to the path attri- bute defined in the XML configuration file, and that it should invoke the perform method of the SaveUserAction class. The attribute "name" defines the ActionForm (model) that the action will receive and the scope attribute defines where the form will be found (request, session, context). It also defines URI mappings that the Action will use to determine where to forward. By keeping the application mappings external, the Struts framework can be easily modified without recompiling classes. Online Business Systems has used the Struts application framework on many successful projects including Viewtrak.com, an integrated food source management tool that tracks production of cattle from producer to consumer. The architect, Curtis Linton of Online Business Systems, used Struts as the Web application framework and the Versata Logic Suite for business logic development and management. Example code from this project has been used in this article, and updated to current versions.
Business-Tier Pattern Framework First, to develop the business components we should consider a declarative approach, a business rules approach. Barbara von Halle in her book, Building Business Rule Systems, states, "It is no longer desirable to bury rules in specifications and program code where they are locked away, requiring costly intervention and overhead to effect change." Second, we need to pay careful attention to J2EE best practices to avoid those performance pitfalls we've read about: too many fine-grained entity beans, caching, use of CMP, and so on.
Business-Tier Design Pattern Framework What follows is a discussion of this pattern framework in terms of the core J2EE design patterns. Using this framework, we'll also look at how products within the Versata Logic Suite leverage and implement these patterns and how they accelerate and simplify business logic development and maintenance. Within the Versata Logic Suite, business components are built with the Versata Logic Studio from business-logic specifications, especially business rules. Java artifacts, such as value objects, value object assemblers, business objects (entity and session beans), and data access objects, are produced as well as deployment artifacts, such as database schemas, deployment descriptors, and test data scripts. The business components may be accessed as regular Enterprise JavaBeans through the Versata Transaction Logic Engine or through the Versata Logic Server that includes the framework that follows the patterns we describe below. The Versata Logic Server also includes the optional Versata Presentation Engine, which supports the model-based development of HTML servlet (presentation tier) and Java (client tier) Web applications.
Business Delegate The Business Delegate pattern represents the Model of our presentation-tier MVC pattern.
Business Service As an interface into our business logic we would consider Business Service one of the View components of our MVC-like pattern.
Value Objects The Versata Suite enables value objects to be created declaratively through the Versata Logic Studio, allowing attri- butes to be selected from objects in the entity object model, and creating composite objects by joining across object relationships. Value objects are also created for each entity object by default. These value objects are also updatable, encapsulating behavior that allows them to interact directly with the Business Service, such as update, create, delete, and refresh. This is also true for composite objects - updates may be distributed across different business objects. They're also metadata value objects accessed somewhat like javax.sql.RowSet objects. Considering the MVC pattern, value objects represent the view. This suggests to us that they should be model-independent, not encapsulate business logic. We should look to create value objects that represent how a system presents itself to the outside world as opposed to necessarily re-presenting internal models.
Value Object Assembler Would this component be a view or a controller? Value Object Assemblers typically are involved only in query operations, so they would be equivalent to a view component. If we were more faithful to the MVC pattern, the view objects would access the model directly for querying state, only accessing the controller for updates. In fact, we sometimes see this pattern as a performance optimization, especially for accessing large sets of data. The value object will pull data straight from the data access object or JDBC, alleviating the need to instantiate an entity bean for each object instance. Our implementation uses this optimization transparently. The Versata Value Object Assembler instantiates only business objects for query operations when it has to. Since it knows attributes are persisted or calculated, it infers whether it can retrieve data straight back from the data access object or whether business logic needs to be executed.
Data Access Object You can write data access objects or use tools. A good example is IBM's VisualAge for Java (VAJ). VAJ's Enterprise Access Builder allows you to create data access objects for sources ranging from DB2 to CICS. The Versata Logic Server has a powerful implementation of the data access layer. Access objects are created at runtime based on object metadata, runtime configuration properties, and transaction requirements. The administrator can change the data source at runtime and tune options, such as whether to use prepared statements, the prepared statement cache-size, connection pooling, and lock isolation levels, giving a great deal of flexibility and high performance without requiring hand-coded BMP. Data access objects clearly represent the Model of our MVC-like business-tier pattern.
Business Objects The business objects operate within the EJB container of the J2EE application server and use the services for coordinated transaction management and persistence, for example. The Versata Logic Suite supports the automated creation of business objects from specifications. The system sequences, optimizes, and translates these specifications or what we call business rules into the business objects - the Java components. Typically some custom Java coding is required, which might be entered directly into business rules or used to subclass or add code to Java components. We typically see about 98-99% of the Java component built directly from rules. Looking at the MVC patterns, would business objects be akin to the model or the controller? You could argue that they're part of the model, the intrinsic representation of what the business is. In a way though that's what the whole business tier represents. We prefer thinking about business objects as the controller. Recall that for the presentation tier, the controller provides the presentation logic. For the business tier, the business objects handle business logic, providing a clean separation from the data (the model) and from business services (the view).
Declarative Business Logic An example of declarative programming is the EJB deployment descriptor, specifying what fields should have their persistence managed by the container. EJB 2.0 goes further by adding declarative support for container-managed relationships; something you have to manage programmatically today. We've also seen the Struts action mappings and view definitions as declarative approaches.
Automation of Business Rules Declaratively we might express this logic as:
Constraints are another type of business rule; they represent the policies under which a business can operate. Figure 4 shows constraints for the Customer object. In business rules terminology we call these calculations derivation rules - the attribute definitions are derived from other attributes. Figure 5 shows several derivation rules for the Account business object, providing detail from the Total-Billed sum rule. Additional derivation types can be seen in the grid: a replicate, a calculation, a default, two sums, and a count.
Business Rules Paradigm Shift After all, they're business rules. Some of the advantages of a rules approach are:
The Model-View-Controller pattern continues to offer a powerful abstraction to the J2EE architect, creating well defined but loosely coupled components, enabling change. Although typically applied at the presentation tier, the MVC pattern can also guide us within the business tier, providing a clear delineation of work to developers and infrastructure components. Through the discussion of the Struts framework and the Versata Logic Suite for business logic development we also saw how change is enabled through systems driven from specifications, not code - moving toward what not how.
References
Articles/ebeans/sevenrules/. May. Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||