- Terminology
- Why Was the Online Community Built the Way It Was?
- The Overall Logical Structure
- The Database
- The Modules
- How the Code Files Are Organized
- Search
- Security
- Moving On
The Overall Logical Structure
The structure of the application is shown in Figure 3.1.
Figure 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:
ModuleControlBase class for all controls that display information from a module.
ModuleInstanceControlBase class for controls that display a complete module instance.
ModuleItemControlBase class for controls that display a particular item from a module (for example, a news story).
SectionItemControlBase 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.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 objectsthey 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.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 itselfit 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 uswe 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.