Home > Articles

Exploring the Code

This chapter is from the book

This chapter is from the book

The Overall Logical Structure

The structure of the application is shown in Figure 3.1.

Figure 3.1Figure 3.1 The overall structure of the online community application.

As you can see, the application is constructed in layers. Let's take a look at each of the layers in the application and briefly describe what they do.

ASP.NET Pages

At the top of the tree are the ASP.NET pages users request when viewing the site. There are actually very few of these; most of the users' requests go to default.aspx. Other pages have only been used when a different layout is required than that provided by default.aspx, or where it is thought that a different layout might be required in the future.

All the ASP.NET pages provide very little other than a basic layout. They simply act as containers for controls, which do the bulk of the work of displaying the online community.

Controls

Most of the work of actually displaying the community is done by ASP.NET user controls. User controls were used rather than server controls because they make it very easy to make changes to their appearance. (Their HTML code is separated from the code that provides their functionality.) This means that a Web developer who is not quite so good with code can make changes to the site appearance, whereas those with programming skills can concentrate on the features of the site.

Server controls are great for building highly reusable controls, but we want to get our community up quickly and also maintain as much flexibility as possible in how it looks.

There are a few different types of user controls in the online community. Some of the controls are standard user controls, whereas others are specialized to the community application. Where several controls need to provide the same features, base classes have been created. Each control inherits from the base class that defines the features it needs.

Inheritance was used to create sets of controls with common features:

  • ModuleControl—Base class for all controls that display information from a module.

  • ModuleInstanceControl—Base class for controls that display a complete module instance.

  • ModuleItemControl—Base class for controls that display a particular item from a module (for example, a news story).

  • SectionItemControl—Base class for module views.

The biggest potential causes of confusion here are ModuleInstanceControl and SectionItemControl. Controls that derive from ModuleInstanceControl provide the main display for a module. There will be one of these controls for each module in the application. Controls that derive from SectionItemControl are the views of the modules. There may be several of them per module, each selecting different data from the module and displaying it in a different way.

Business Services

The ASP.NET pages and controls get data to display by making requests to business service classes. There is one business service class that provides "core" services that are used by the whole application and one business service class for each module.

Persistent Objects

These objects are not shown in Figure 3.1. They are, however, very important. The persistent objects are used to carry information between the layers of the application. For example, when information about a member is required by a page or control, a Member persistent object is requested from the core business service class. The core business service class, in turn, requests the Member object from the persistence service, which handles data access. This process is shown in Figure 3.2.

Figure 3.2Figure 3.2 Requesting and passing a Member persistent object between layers.

The persistent objects have relationships between them, so after we have requested one object, we can access other related objects without having to make requests to other layers of the application. So, for example, after we have a Member object in the presentation layer, we can access all of the module instances created by that member directly. Doing this might involve further requests to the database to get the data we need, but this is handled transparently by the persistence service.

An important point to note is that the presentation layer pages and controls never directly make changes to persistent objects—they always make requests to the business service layer for changes to be made. We will discuss why this is important in the next section.

The core persistent objects and the relationships between them are shown in Figure 3.3.

Each persistent object class represents a particular entity within the online community application. You might want to refer to the "Terminology" section at the beginning of this chapter for explanations of the concepts that the persistent objects represent.

Figure 3.3Figure 3.3 Relationships between the persistent objects.

The stars on Figure 3.3 indicate the type of relationship that is involved. All of the relationships are one-to-many, with the stars indicating the "many" end of the relationship. For example, each member can have many sections. Each ModuleInstance object can be connected to many SectionItem objects, but each SectionItem object only links to a single ModuleInstance.

We will now look at each of the persistent objects and the properties they provide.

In addition to the properties shown for each persistent object, they each have a primary key. A primary key is a value that is unique for each instance of a particular persistent object. If you are familiar with relational databases, you know that primary keys are used to uniquely identify entities within the data. In fact, a primary key column is used in each database table that stores the data for a persistent object. The same primary key value is stored in the persistent objects themselves.

Member

The Member object holds data for a single member of the community. It has the following properties:

  • Username (String)—The name the member uses to identify himself.

  • Password (Write-only String)—The member's password. This is set to write-only because we don't intend for any code to have access to the member's password after it is set.

  • IntroText (String)—Some text that is displayed on the member's personal page and also in the list of members.

  • DateJoined (DateTime)—The date the member registered with the community.

  • LastAccess (DateTime)—The last date and time the member used the community application.

  • Email (String)—The member's email address.

  • PublicEmail (Boolean)—A value that determines whether to display the member's email address to other users of the community.

  • MemberPageSection (Section)—A Section object for the section that should be displayed when users view the member's personal page.

  • Sections (IList)—A collection of Section objects for all the sections the member has created. This includes the section that is referred to by MemberPageSection.

  • NonMemberPageSections (IList)—A collection containing all the member's sections except for her MemberPageSection.

  • ModuleInstances (IList)—A collection containing all the module instances the member has created.

Note that some of the properties have type IList. This means they guarantee that they will return an object of a class that implements the IList interface, but they will not guarantee exactly which collection class that will be. Code that uses these properties must only use the features required by IList when using these objects.

Fortunately, the code can assume that the IList objects contain objects of a particular type (because the persistence service will fill the collections with appropriate objects), and IList provides the means to access those objects. In particular, IList enables us to use the For Each statement to loop through all of the objects in the collection.

CommunityModule

This persistent object holds information about modules that are used to add features to the application. Note that the persistent object does not provide the features itself—it simply holds information about the module the core community code needs to use it.

It has the following properties:

  • Name (String)—The name of the module. The name is used as an identifier so that the code for a module can access its persistent object. It is also used as the name of the folder in which the module's code is kept.

  • Description (String)—A text description of the module. It's not currently used for anything, but it's useful for documenting the modules.

  • AllowGlobal (Boolean)—Specifies whether a global instance should be allowed for the module.

  • AllowMember (Boolean)—Specifies whether members should be allowed to create instances of the module.

  • ModuleViews (IList)—A collection of ModuleView objects for all of the views provided by the module.

  • ModuleInstances (IList)—A collection of ModuleInstance objects for all of the instances that exist for this module.

  • ServiceClassName (String)—The name of a class that inherits from ModuleBase that contains the business service functionality for the module. This class should be defined in a source code file in the Module folder.

It is worth noting that all of the properties of CommunityModule are read-only. When new modules are added to the system, their details are added directly to the database by an administrator, so there is no need to allow write access to the properties in code.

ModuleView

Each ModuleView object contains information about a view provided by a particular module. This system enables us to define a number of different ways in which the data held by module instances can be displayed to users.

As with CommunityModule, the ModuleView persistence object does not contain the implementation of the view it represents but rather holds the information the application needs to access the implementation.

ModuleView objects have the following properties:

  • Name (String)—The name of the view.

  • Description (String)—A text description of the view.

  • AllowMember (Boolean)—Defines whether members are allowed to create views of this type. (We might want to reserve some views for use with global module instances.)

  • ControlName (String)—The name of the ASP.NET user control that implements the view. When the view is displayed, this control will be found in the Views subfolder of the Module folder and will be loaded into the page.

  • CommunityModule (CommunityModule)—The persistent object for the module to which this view belongs.

  • SectionItems (IList)—A collection of all the SectionItem objects that use this view.

Like those of CommunityModule, the properties of ModuleView are all read-only.

ModuleInstance

This persistent object represents a particular instance of a module that has been created by a member. It has the following properties:

  • Name (String)—The name the member has given the module instance.

  • CommunityModule (CommunityModule)—The module in which the instance is an example.

  • Member (Member)—The member who created the instance.

  • SectionItems (IList)—A collection of all the section items that are based on this module instance.

  • ShowInGlobal (Boolean)—Enables the member to define whether the data from this module instance should be included in the global instance of the module and in the community-wide search system.

  • LastUpdated (DateTime)—The last time that any data was changed in the module instance.

SectionItem

The section items bring together module instances, sections, and module views to define which module instances should be displayed where, and how they should be displayed.

They have the following properties:

  • Section (Section)—The section to which the section item belongs.

  • ModuleInstance (ModuleInstance)—The module instance the section item should display. This property is set to nothing if the section item should display the global instance of a module.

  • ModuleView (ModuleView)—The view that should be used to display the module instance.

  • IsGlobal (Boolean)—Returns true if there is no ModuleInstance and thus the section item should display the global instance of the module to which its view belongs.

Section

Section objects are created by members to group together their information. Each section can contain a number of section items, each displaying different module instances.

Section objects have the following properties:

  • Member (Member)—The member who created the section.

  • Name (String)—The name the member has given to the section. This will be used in the navigation.

  • Description (String)—A description of the section. (This is not currently used.)

  • LastUpdated (DateTime)—The last time any of the module instances referred to by section items in the section were updated.

  • SectionItems (IList)—A collection containing the section items in the section.

  • IsGlobal (Boolean)—Returns true if there is no member defined for the section. Global sections are used for community-wide content (especially global module instances).

There are some other persistent objects in the application. These are specific to modules. For example, the News module has a NewsItem persistent object that represents a single article, whereas the ImageGallery module has a GalleryImage persistent object that represents a single image. These persistent objects typically have a one-way relationship with a ModuleInstance persistent object; each module-specific object knows to which ModuleInstance it belongs.

We will look at the module-specific persistent objects later in this book when we dig into the code for the modules.

The Persistence Service

So, we know that the persistent objects are used to carry information between different parts of the application. The big question is, where do the persistent objects come from?

The data behind the persistent objects is ultimately stored in the database, so when we were designing the application, we needed a way to get the data from the database (and also a way to change the data). We could have chosen to write our own data access code to extract the data from the database and then used it to create the objects. Instead, we opted to use a system that had already been created to do just this job.

The persistence service that was chosen for the application is called OJB.NET. OJB.NET is an object/relational mapping service. This means that it handles all the hard work of storing objects in a relational database, enabling us to simply request the objects we want and use them. The really great thing about OJB.NET is that it also handles the work of updating the database when we make changes to persistent objects. We just treat the objects as objects, and the database is updated automatically.

TIP

If you want to learn more about OJB.NET directly, you can download the full source code for it from http://Ojb-Net.Sourceforge.net/. The code is not for the faint-hearted, but it is well worth looking at because it uses a wide variety of advanced .NET programming techniques.

Later in this chapter, we will see just how much the use of a persistence service simplifies our data access. We will also look at how this approach can improve performance in some ways.

There is one very important point to bear in mind, though. Remember how, in the previous section, I said that we always have to use the business service layer rather than the presentation layer to make changes to persistent objects? Well, the reason for this is that any code that changes the persistent objects must be transactional.

TIP

Terminology Transactional means that sets of operations are carried out as transactions. Each transaction is treated as a single whole rather than a series of separate operations. After a transaction is defined, the whole set of operations will be completed without interference from other operations, or none of them will be completed.

By making changes to persistent objects in transactions, we know that our data will not become corrupted by part operations taking place and that multiple, simultaneous page requests will not cause clashes with each other.

All the business service classes are transactional (we will see how this is achieved later), so making changes to persistent objects within their code is safe. The presentation layer code is not transactional, so making changes to persistent objects there is not safe.

When our application makes a call to a business service class, a new transaction is created. No changes are persisted to the database until all of the operations of the business service class are completed. If anything fails, no changes are made to the database. If everything succeeds, OJB.NET makes the required changes.

Another feature of OJB.NET is that it caches persistent objects. This means that after a specific object has been retrieved from the database, it is stored in memory. If that same object is needed again, it can be used from memory rather than retrieving it from the database.

There are limits to the caching that OJB.NET provides. The main limitation is that caching only works when we retrieve a specific object by its primary key. If we use a query to retrieve a set of objects, we have to make a query to the database to determine which objects are selected in the query; queries use SQL so that the database does the work of the query. However, OJB.NET will only extract what it needs to from the database; if the objects exist in the cache, the full data of the object will come from there rather than from the database.

The final persistence service feature we will look at for now is lazy loading. This means that when we retrieve a persistent object that contains a collection of other persistent objects, we do not retrieve all of the other objects immediately. Instead, OJB.NET waits until we first access the collection and retrieves them at that stage. The great thing about the way this works is that it is totally transparent to us—we don't have to worry about it at all. We just access our objects and let OJB.NET do the work.

We will be looking in more detail at how we use OJB.NET, but for now it is enough to understand that it enables us to request, use, update, create, and delete persistent objects without having to worry about how it actually communicates with the database.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020