Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Mastering UML with Rational Rose 2002.pdf
Скачиваний:
137
Добавлен:
02.05.2014
Размер:
9.68 Mб
Скачать

Chapter 18: Rose Data Modeler

So far, we've focused on modeling the application itself—creating the Use Case diagrams, Interaction diagrams, Class diagrams, and other artifacts needed to really understand how the system works. An essential element to nearly every system, however, is some form of persistent storage, typically a database.

Using Rose, you can model not only the application, but also the database or databases that support the application. Rose 2001, 2001A, and 2002 support the data−modeling notation that has been incorporated into UML.

Comparing object models and data models

Creating a data model

Adding logic to a data model

Modeling databases, schemas, tables, fields, stored procedures, triggers, and more

Modeling primary keys, foreign keys, and entity relationships

Modeling views

Generating an object model from a data model

Generating a data model from an object model

Creating the database from a data model

Reverse engineering a database into a data model

Object Models and Data Models

An object model is used for all of the pieces of the application that we have discussed so far—the classes, attributes, operations, relationships, components, and other constructs—except for the data. The primary emphasis of an object model is on memory—what objects will be created in memory, how will these objects communicate, and what is each object responsible for doing? The focus of the data model is, as the name implies, the database rather than the application.

While object modeling is concerned with memory efficiency, data modeling is more concerned with database efficiency. Table 18.1 lists some of the differences in perspective between the data model and the object

610

Chapter 18: Rose Data Modeler

model.

Table 18.1: Concerns in Data Modeling and Object Modeling

Object Model

Data Model

How can I design the classes to be memory efficient?

How can I design the database to be storage efficient?

What objects need relationships in the object model?

What tables need relationships in the data model?

How can I structure the data on the user interface to

How can I structure the data to speed access times?

make the most sense to the end user?

 

How can I package the data with behavior to create

How can I normalize the data?

classes?

 

What data will be used throughout the application,

What data will be retrieved frequently?

and what data will be used in only one area?

 

How can I use generalizations or other design

How can I incorporate the concept of inheritance into

strategies to reuse code?

my data model if my DBMS doesn't directly support

 

inheritance?

There is a definite disparity between the data model and the object model. The primary reason for this is the nature of the models themselves; objects are, by definition, focused on behavior and data, while the data model is focused on data. The object model, in most languages, supports inheritance, while the data model does not. Data types in programming languages and database management system (DBMS) packages are different. Join tables do not need to be included in the object model as a general rule (although association classes are sometimes needed). Two classes need to have a relationship if one needs to access attributes or operations of the other; two tables need to have a relationship if there is a logical connection between the data in the two tables. Two entity classes may have a relationship in the object model, but their tables may not be related in the data model.

To account for these natural differences, Rose supports the creation of both an object model and a separate data model. You can create both of these models in a single Rose file so that you have a complete understanding of your application and its database in one place.

So, which comes first: the data model or the object model? In many cases, the two models are developed concurrently. In the Inception phase, the team can develop both a rough data model and a rough object model, or a domain model. As Elaboration and Construction progress, the team can fill in the details of both models. Many of the entity classes from the Class diagrams will become database tables. There is not, however, a one−to−one correspondence. Because of the differences in perspective between the two models, a single

entity class may become two or more database tables. Conversely, a single database table may be supported by two or more classes in the application.

Many projects, especially maintenance projects, begin with some sort of existing data model. Using Rose, you can reverse engineer the existing data model, and you can even automatically generate an object model from it. If you have an object model but no data model, you can automatically generate a data model from your object model.

611

Chapter 18: Rose Data Modeler

Creating a Data Model

In Rose, the Data Model includes constructs in both the Logical view and the Component view. In the Logical view, you can create schemas, which in turn contain stored procedures. You can also create tables, which contain fields, constraints, triggers, primary keys, indexes, and relationships. Finally, you can create domains and domain packages.

In the Component view, you can model the databases themselves. Each database is modeled as a component with a <<database>> stereotype. Rose 2001A and 2002 support DB2, SQL Server, Sybase, Oracle, or ANSI SQL.

The primary steps in the creation of a data model are:

1.

Create a database.

2.

Add a schema to hold the data model and assign the schema to the database.

3.

Create domain packages and domains.

4.

Add tables to each schema.

5.

Add details to the tables (fields, constraints, triggers, indexes, primary key).

6.

Add relationships between the tables and add foreign keys.

7.

Create views.

8.

Create an object model from your data model.

9.

Generate the database.

10.

Keep the database synchronized with the model through the Update feature.

It isn't necessary to follow all of the steps in this order, but creating the database and schema first sets the DBMS that will be used. When you create tables, fields, and other data−modeling elements, the appropriate data types will then be available. In the remainder of this chapter, we will discuss each of these steps. Before we do, however, let's look at what logic might be incorporated into the data model.

612

Chapter 18: Rose Data Modeler

Logic in a Data Model

Database−management systems are becoming more sophisticated every year. It's becoming easier to add logic to the database—so much so that it can be easy to become confused about what logic should go in the

database and what logic should go in the application.

There is no simple way to determine what logic should go where, and a complete analysis of database design principles is outside the scope of this book, but here are some points to consider:

General object−oriented practices suggest keeping at least some of the business logic in an application layer rather than in the database.

In general, only logic related to the data itself should be housed in the database. This would include items such as required fields, valid values for fields, and field lengths.

Many business rules can be enforced directly in the database through the use of constraints. Although the database is an appropriate location for this type of logic, the application must gather information from the end user, pass it through the business layer, and then across a network connection, which may be slow, before the data is validated. Keeping this logic in the business layer can sometimes help reduce unnecessary network traffic.

However, if a number of areas within the application, or even a number of different applications, need to use the same constraint, placing the logic in the database can help ensure that the rule is applied consistently.

Some of the system logic can be carried out directly in the database through the use of stored procedures. There are advantages to this approach; functionality that is very data−intensive might be more appropriate as a stored procedure. If the functionality is strictly data manipulation, programming it as a stored procedure might be significantly faster than loading all the records into memory, having the application do the processing, and then storing the results back to the database.

However, there are some disadvantages to this strategy as well. Using stored procedures to implement any business logic inherently divides the business logic across at least two layers: the business logic layer and the database layer. When business logic changes, you may need to update both of these layers. You also run the risk of duplicate logic across the layers or, even worse, contradictory business logic across the two layers.

Too many stored procedures can also cause difficulties in migrating from one DBMS to another. Many database management packages have slightly different syntax, and migrating from one to another may necessitate rewriting of the stored procedures.

Again, there isn't necessarily an easy way to distinguish between the logic that should reside in the database and the logic that should reside in the application. Once you have decided to place logic in the database, you can model that logic by modeling stored procedures, constraints, and triggers in Rose. First, however, you must create a database and schema.

613