Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default CFCs and OO design

    Howdy folks,

    I am new to using CFC's (and to be honest, new to OO in general), and I'm
    trying to use them for the first time. Let me explain my problem:

    I'm writing a program to staff a temp employee for a specific shift on a
    specific date. Requirements are that the employee must be able to work on that
    day, must be active in the database, have the appropriate skill set, be willing
    to work in the appropriate region, etc.. I then populate a dropdown list with
    all the employees who match the criteria so the user can choose whom to staff.

    In the past, I've written a bunch of CFML code that retrieved all the
    employees for a certain market (and other restrictions), and then I proceeded
    to build an array of structures where each element of the array was an employee
    and the structure members of that array element was all the vital information
    about that employee. I would then loop over the structure looking for employees
    who matched more closely the criteria I was looking for.

    Now, I'm trying to do the same sort of thing with CFC's only slightly more
    complex and a bit more granular. I created an employee class with three
    methods: getEmployeeSkills, getEmployeeAddress, and getEmployeePhone. I have a
    class called "nurse" since a nurse is a type of employee. I start by creating a
    nurse object from my nurse class which inherits the methods from the employee
    class. My nurse object returns an array each element of which contains a
    structure that represents a nurse. Some of the structure members contain a
    simple piece of data (like first name, last name) and others contain more
    complex data. For instance, the addresses structure member is an object which
    is created from the getEmployeeAddress method of the employee class, and it
    returns another array each element of which is a structure containing the
    different pieces of an address: address line 1, address line 2, address
    type(home, billing, work, etc.), City, State, Zip.

    This is all started by a simple CMFL page that contains a line to create the
    nurse object and another line to dump the nurse object to screen:

    <CFSet nurse = CreateObject('component', 'cfcs.nurse').init()>
    <CFDump var="#nurse#">

    this whole process takes just over a minute. That's just too long. So I'm
    trying to find out if I'm missing something in the way that I'm approaching
    this OO concept or the concept of CFCs. My hope was to be able to access these
    employees in a manner such as: nurse[1].addresses[1].city

    Am I going about this in even remotely the right way? Any advice would be
    appreciated.

    Thanks.
    -Chris

    g1zm0guy Guest

  2. Similar Questions and Discussions

    1. CFCs and RemoteObject
      I currently have an application that is calling a CFC, via RemoteObject, and the results are populating a datagrid. How can I pass an argument back...
    2. cftransaction across cfcs
      Hi there, There seems to be a REALLY irritating 'feature' of Coldfusion MX, to do with using cftransaction across function call boundaries, which...
    3. CFCs & Dot Notation
      Here is what I have. A CFC with filename amCurPatientCount.cfc which is located in the "components" folder under the webroot. I have been able to...
    4. CFCs and MX7
      I have a site with a photo album that utilizes CFCs and it worked great until my ISP switched to MX7. When they asked me if I thought I would have...
    5. Is cfinclude'ing from CFCs bad?
      Is it bad to CFINCLUDE a cfm file from a ColdFusion Component? We have run into some strange problems, which we believe are related to a shortage...
  3. #2

    Default Re: CFCs and OO design

    Have you considered using a query or query of queries to filter down the inital list of possibles. It might be faster.
    Symantec Guest

  4. #3

    Default Re: CFCs and OO design

    I'll jump in with a aouple of general comments and suggestions.

    g1zm0guy wrote:
    > Now, I'm trying to do the same sort of thing with CFC's only slightly more
    > complex and a bit more granular. I created an employee class with three
    > methods: getEmployeeSkills, getEmployeeAddress, and getEmployeePhone. I have a
    > class called "nurse" since a nurse is a type of employee.
    Are you sure you want to have a completely separate nurse class? Maybe
    you need this for some reason, but depending on what you're doing with
    this you might consider that a nurse is really a "role" of an employee
    rather than a truly independent class. I only mention that because if
    you start doing this for every type of employee you'll likely have a lot
    of unnecessary object proliferation.
    > I start by creating a
    > nurse object from my nurse class which inherits the methods from the employee
    > class. My nurse object returns an array each element of which contains a
    > structure that represents a nurse. Some of the structure members contain a
    > simple piece of data (like first name, last name) and others contain more
    > complex data. For instance, the addresses structure member is an object which
    > is created from the getEmployeeAddress method of the employee class, and it
    > returns another array each element of which is a structure containing the
    > different pieces of an address: address line 1, address line 2, address
    > type(home, billing, work, etc.), City, State, Zip.
    >
    > This is all started by a simple CMFL page that contains a line to create the
    > nurse object and another line to dump the nurse object to screen:
    >
    > <CFSet nurse = CreateObject('component', 'cfcs.nurse').init()>
    > <CFDump var="#nurse#">
    >
    > this whole process takes just over a minute. That's just too long. So I'm
    > trying to find out if I'm missing something in the way that I'm approaching
    > this OO concept or the concept of CFCs. My hope was to be able to access these
    > employees in a manner such as: nurse[1].addresses[1].city
    Are you saying it takes over a minute to create a *single* nurse object?
    If that's the case you'd really have to elaborate on what you're doing
    or post some additional code so we can better try and evaluate what's
    going on here.

    If I'm understanding what you're doing, it sounds like you're creating
    an object to hold arrays of structures about all nurses. I guess my
    question would be why? Whenever you do something like "give me all
    nurses" and you create an object to hold the results you're really
    reinventing the wheel--that's what the query object is for. No need to
    make your own container for this type of data in most cases, and it will
    definitely be a heavyweight process compared to the query object.

    To use your nurse object as an example, I've seen people do a basic
    search that hits the database, and then they'll take their query object,
    loop over it and instantiate an object for each database record, shove
    that into an array, and then use that array of objects to output basic
    information about each nurse like name, etc. Don't do that! :-) It's
    a very expensive process and you don't gain anything in the end over
    using the query object directly.

    Don't get me wrong, I do pretty much all OO these days, but it's always
    good to use CF's built-in objects for their intended purpose,
    particularly where the query object is concerned.

    Matt

    --
    Matt Woodward
    Team Macromedia - ColdFusion
    mpwoodward *TMM* Guest

  5. #4

    Default Re: CFCs and OO design

    Yes, I tried that, but the Query took quite a while to run. Unfortunately, I'm
    in an environment where it is not possible for me to add indices to my tables.
    I'm dealing with old FoxPro 2.6 tables and the indexes get rebuilt nightly by a
    process that I cannot control (owned by another vendor).

    I was talking to someone else about this, and I think I may just do what he
    called "LazyLoading". I'm going to narrow down my list of employees as much as
    I can, and then get the granular detail only when I need it. Perhaps that will
    be faster. I don't know, I'm still trying to get my mind around it a bit.

    Any other comments or advice are welcome. I'm trying to get over my learning
    curve. :-)

    Thanks.

    g1zm0guy Guest

  6. #5

    Default Re: CFCs and OO design

    Hi there,

    CFC's are pretty slow to instantiate especially when compared to structures.
    So the simple answer is don't use them. The more complex answer is don't use
    them for small grained objects where you would need to instantiate a lot of
    them. Instead use them for larger grained objects or when you need to
    instantiate less of them (one or two)

    For example

    EmployeeDataGateway - Contains all data-acces code (including sql) for your
    employee table. Return query or array of structs. An array of Employee CFC's
    would kill performance. I generally provide select (returns details for 1
    employee), insert, update, delete and find(returns details for filtered group
    of employees)

    EmployeeCollection - Encapsulates filtered list of employees and business
    logic that applies to the collection as a whole. I generally use this when I
    want to provide a list of employyes to choose from

    EmployeeEntity - Encapsulate a single employee and associated business logic
    that applies only to the employee. I generally use this once I've selected an
    employee from a list.

    There is probably a better way to do this but it seems to be working well for
    me and impact on performance is negligible,

    Hope this helps,

    Peter (aka lad4bear)

    lad4bear Guest

  7. #6

    Default Re: CFCs and OO design

    Matt,
    > Are you sure you want to have a completely separate nurse class?
    I don't really know. I was working from an example that I found in an article
    I found on Hal Helm's website. His basic example was that you have a class
    called "Car" and then another one called called "Sports Car" that extended
    "Car".
    (see his article here:
    [url]http://www.halhelms.com/index.cfm?fuseaction=newsletters.show&issue=060204 _inter[/url]
    faces1)

    So, I'm not sure I'm doing it right, but at least that part made a certain
    amount of sense.
    > If I'm understanding what you're doing, it sounds like you're creating
    > an object to hold arrays of structures about all nurses. I guess my
    > question would be why? Whenever you do something like "give me all
    > nurses" and you create an object to hold the results you're really
    > reinventing the wheel--that's what the query object is for. No need to
    > make your own container for this type of data in most cases, and it will
    > definitely be a heavyweight process compared to the query object.
    Matt, I think that the general reason used structures in my original code and
    am trying to do something similar now with objects is because, there are times
    when I will have to cross data sources. An employees data (in my specific
    instance) is not organized very well. Some of the data is in old FoxPro tables
    and some of it is in AS400 DB2 tables. Eventually, when I'm completely done
    everything will be organized nicely and on a single database (probably the DB2
    stuff), but getting information from a few queries on different data sources
    and then storing the information in a structure was not only an easy way for me
    to understand what I had, but also an easy way to access the data later on.

    Maybe there's a better way. I don't know.
    > To use your nurse object as an example, I've seen people do a basic
    > search that hits the database, and then they'll take their query object,
    > loop over it and instantiate an object for each database record, shove
    > that into an array, and then use that array of objects to output basic
    > information about each nurse like name, etc. Don't do that! :-) It's
    > a very expensive process and you don't gain anything in the end over
    > using the query object directly.
    Yeah, I definitely don't want to do anything this drastic! :-P A part of the
    idea was that I wanted to be able to access an employee's information by saying
    something like:

    janitor[1].skills.mopping
    or
    assistant[1].skills.typing = 120 // set the first administrative
    assistant's typing skill = 120 wpm.

    That sort of thing. I was thinking about how JavaScript treats an object like
    the elements array, and sort of trying to mimic that. Don't know if that's the
    thing to do.

    g1zm0guy Guest

  8. #7

    Default Re: CFCs and OO design

    Peter,
    > EmployeeDataGateway - Contains all data-acces code (including sql) for your
    > employee table. Return query or array of structs. An array of Employee CFC's
    > would kill performance. I generally provide select (returns details for 1
    employee),
    > insert, update, delete and find (returns details for filtered group of
    employees)

    By "find" here are you talking about a method that you create that your
    EmployeeDataGateway class contains? (assuming, of course, that it is a class).
    >
    > EmployeeCollection - Encapsulates filtered list of employees and business
    > logic that applies to the collection as a whole. I generally use this when
    I want
    > to provide a list of employees to choose from
    > EmployeeEntity - Encapsulate a single employee and associated business
    > logic that applies only to the employee. I generally use this once I've
    selected
    > an employee from a list.
    I'm interested in this idea. It sounds a bit like what I'm trying to do. Could
    you perhaps elaborate for me a bit?

    g1zm0guy Guest

  9. #8

    Default Re: CFCs and OO design

    g1zm0guy wrote:
    > Matt,
    >
    > > Are you sure you want to have a completely separate nurse class?
    >
    > I don't really know. I was working from an example that I found in an article
    > I found on Hal Helm's website. His basic example was that you have a class
    > called "Car" and then another one called called "Sports Car" that extended
    > "Car".
    > (see his article here:
    > [url]http://www.halhelms.com/index.cfm?fuseaction=newsletters.show&issue=060204 _inter[/url]
    > faces1)
    >
    > So, I'm not sure I'm doing it right, but at least that part made a certain
    > amount of sense.
    Right, it may or may not make sense--so I'm really just asking the
    question more than telling you that you don't need to do this. Hal's
    also written articles about using roles to avoid unnecessary object
    proliferation, so it just depends on the situation. If there isn't
    anything particularly unique to the nurse employee as opposed to the
    generic employee object (i.e. if you can represent skills in some
    generic way and don't need an actual object attribute for nurse-specific
    skills) you might be better off with a generic employee object and a tpe
    or role attribute to cover whether it's a nurse or a programmer.
    > Matt, I think that the general reason used structures in my original code and
    > am trying to do something similar now with objects is because, there are times
    > when I will have to cross data sources.
    Sure, but you still might be better off combining the data into a single
    query object. Again *might* being the operative word. I only bring
    that up because using the objects that are native to CF and designed for
    this specific purpose will likely perform better even if it takes a bit
    of finagling to get them created in the first place.

    Just some additional thoughts--in the end you have to do what makes the
    most sense given your situation.

    Matt
    --
    Matt Woodward
    Team Macromedia - ColdFusion
    mpwoodward *TMM* Guest

  10. #9

    Default Re: CFCs and OO design

    Matt,

    I've thought about this "role" concept, and looked at my data. I don't think
    there's anything specific to a nurse that couldn't also be applied to an
    accountant, or a valet parking attendant, or whatever. So, I probably will
    scrapp the separate class thing. I appreciate the input you've given me. I'm
    really new to object orientation (much less oo in cf), and I've enjoyed your
    help. Any other tips you may have for me, feel free to pass on the knowledge!
    :-)

    Thanks
    Chris

    g1zm0guy Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139