Defining the address service

Let's repeat the process for the second of our packages used within the application, the user package.

Create a new file called AddressService.cfc within the com/packtApp/oop/dataAccess directory within your application, and add the following code to the file:

<cfcomponent displayname="AddressSVC" output="false" hint="I am the AddressSVC Class used to interact with the Address package.">
<!--- Pseudo-constructor --->
<cfset variables.instance = {
addressDAO = '',
addressGW = ''
} />
<cffunction name="init" access="public" output="false" returntype="any" hint="I am the constructor method for the AddressSVC Class.">
<cfargument name="datasource" required="true" type="com.packtApp.oop.beans.Datasource" hint="I am the datasource object." />
<!--- Set the initial values of the Bean --->
<cfscript>
// instantiate the Data Access Object
variables.instance.addressDAO = createObject( 'component', 'AddressDAO').init(arguments.datasource);
// instantiate the Gateway
variables.instance.addressGW = createObject(
'component', 'AddressGateway').init(arguments.datasource);
</cfscript>
<cfreturn this />
</cffunction>
<!--- CRUD METHODS --->
<!--- GATEWAY METHODS --->
</cfcomponent>

Listing 7.10 - com/packtApp/oop/dataAccess/AddressService.cfc

As in the previous example, we first define the basic structure for the ColdFusion component, and add the constructor method to instantiate the object. We also send through the datasource name as a required parameter, and create the user package Data Access Object and Gateway object, again storing them in the variables.instance structure within the service layer.

Notice in Listing 7.10, we have already defined the path to the coreUtils.cfc within the extends attribute, thereby inheriting the functions from the abstract class for use within this object.

We next need to add the DAO methods into the service object to interact with the functions in the users Data Access Object.

<cffunction name="save" access="public" output="false" hint="I save or update an Address into the database.">
<cfargument name="address" required="true" type="com.packtApp.oop.beans.Address" hint="I am the Address bean." />
<cfreturn variables.instance.addressDAO.saveAddress(arguments.address) />
</cffunction>
<cffunction name="read" access="public" output="false" hint="I obtain details for a specific address from the database.">
<cfargument name="ID" required="true" type="numeric" hint="I am the ID of the address you wish to search for." />
<cfreturn variables.instance.addressDAO.getAddressByID(arguments.ID) />
</cffunction>
<cffunction name="delete" access="public" output="false" hint="I delete a specific Address the database.">
<cfargument name="ID" required="true" type="String" hint="I am the ID of the address you wish to delete." />
<cfreturn variables.instance.addressDAO.deleteAddressByID(arguments.ID) />
</cffunction>

Listing 7.11 - AddressService.cfc (CRUD method callers)

Finally, we need to add the Gateway method callers into the AddressService component to interact with the Gateway object and run the multiple record data transactions against the database, as in Listing 7.12.

<!--- GATEWAY METHODS --->
<cffunction name="getAllAddresses" access="public" output="false" hint="I run a query of all users within the database table.">
<!--- Call the query method and return the query object. --->
<cfreturn variables.instance.addressGW.getAllAddresses() />
</cffunction>
<cffunction name="filterByUserID" access="public" output="false" hint="I run a query of all addresses within the database table based upon a required filter.">
<cfargument name="userID" required="true" type="string" hint="I am the userID to filter." />
<!--- Create and populate a structure object containing the filter to pass through. --->
<cfset var stuFilter = {
userID = arguments.userID
} />
<!--- Send the structure into the query method and return the query object. --->
<cfreturn variables.instance.addressGW.filterAllAddresses(stuFilter) />
</cffunction>

Listing 7.12 - AddressService.cfc (Gateway method callers)

With the addition of the code in Listings 7.11 and 7.12 added to the basic structure for the AddressService.cfc, your service layer for the address packages is complete.

Defining the address service

onApplicationStart (revisited)

Now that we have written our service layer objects, which interact with the DAO and Gateway for each package on our behalf, we can now amend the onApplicationStart() method within the Application.cfc file to build our objects.

From the four objects we were creating at the beginning of this chapter, we have streamlined the process and are now instantiating just two objects within the onApplicationStart() method; the User and Address service layers respectively.

Not only do these files provide a cleaner, simpler API with which to deal with the underlying code, but this also streamlines the process in which we create the objects. The Gateway and DAO, for each package, are now created within the init() method of each service object.

<cffunction name="onApplicationStart" output="false">
<!--- Instantiate the Datasource object. --->
<cfset var objDatasource = createObject('component', 'com.packtApp.oop.beans.Datasource').init( DSName='CFOOP', username='< your datasource username >', password='< your datasource password >') />
<!--- Instantiate and persist the User Service in the application scope.--->
<cfset Application.userSVC = createObject('component', 'com.packtApp.oop.dataAccess.UserService').init( datasource = objDatasource) />
<!--- Instantiate and persist the Address Service in the application scope.--->
<cfset Application.addressSVC = createObject('component', 'com.packtApp.oop.dataAccess.AddressService').init( datasource = objDatasource) />
</cffunction>

Listing 7.13 - Application.cfc (after services)

Now that we have amended the Application.cfc component to instantiate the two Service objects instead of the four data-related objects, our revised UML diagram would look a little like this:

onApplicationStart (revisited)

As we can see from the diagram, the Application.cfc merely needs to instantiate the service layers/facade objects, which in turn will create the required objects to access the underlying information from the data providers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.236