oop advice needed

Discussion in 'ASP .Net' started by wapsiii, Oct 11, 2005.

  1. wapsiii

    wapsiii Guest

    On a asp.net project I'm using generated classes to access the
    database. I'm developing business logic classes to handle all business
    logic and pass on info to the data access classes. On the code behind
    pages of the asp.net pages I simply get the values from the postback
    and set the properties of the business classes. Before I pass the
    values on the the data access layer to persist the changes I'd like to
    check if the required properties have been set correctly.

    I'm unsure how best to implement these kinds of checks. Should I
    simply add a function to the business layer class called something

    function isReadyToPersist() as boolean

    and do my checks here? Something like:

    if me.PersonId = 0 then
    return false
    end if


    or is there a better way?
    wapsiii, Oct 11, 2005
    1. Advertisements

  2. Yes, you could make a check function to make sure the data is correct at the
    end of processing, although it might be best to do it in several layers:
    1) Use form validation controls to make sure the data is in the correct
    format before it gets anywhere near the business objects, and then
    2) Make sure the data is correct as it's being entered into the objects,
    e.g. if you have a setPersonID function to change the value of an
    encapsulated variable called personID then you could put the logic for
    testing the input into the setPersonID function and return false if there
    was a problem. Then you could easily send an error message back to the
    client if any of the set functions failed.
    Leon Mayne [MVP], Oct 11, 2005
    1. Advertisements

  3. Properties are perfect for this sort of thing. Example:

    private string _FirstName;
    public string Firstname
    get { return _FirstName; }
    if (value == null || value.Length > 128)
    throw new ArgumentOutOfRangeException("FirstName");
    _FirstName = value;


    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Ambiguity has a certain quality to it.
    Kevin Spencer, Oct 11, 2005
  4. wapsiii

    Don Nablo Guest

    The best way requires a value judgement. But I can give you an example I
    have used in numerous projects.
    The main idea of this process is that all your objects need to handle, in a
    consistent manner, errors detected.
    All objects need to respond at a minimum common set of method names. For my
    applications it works like this:

    First all business objects implement 4 basic methods:
    public function getItem(ses as dbConnection, key as String ) as Boolean
    public function updateItem(ses as dbConnection) as Boolean
    public function deleteItem(ses as dbConnection, key as string ) as Boolean
    public function addItem(ses as dbConnection, key as string) as Boolean

    The objects then need to implement a propertey which is read only which
    returns any error messages detected in object operations.
    I use LastOperationStatus as the property name.

    Internally in each business object you would define a method to check the
    values of the object

    Private Function IsValid() as boolean

    Each of the , getItem, AddItem, DeleteItem, UpdateItem methods would call
    IsValid as the first step in the process

    LastOperationStatus would contain the error message of the operation failed.

    For example UpdateItem might be implemented as:

    Public Function UpdateItem(ses as dbconnection) as boolean.
    if not Isvalid then Return False 'isValid is responsible for loading the
    error message in LastOperation status
    ... other code to save data
    End function

    The object IsValid function may look like this:

    Public Function IsValid() as boolean
    dim msg as string
    if trim(me.FirstName) = "" then
    msg = msg & vbcrlf & "The first name cannot be blank"
    end if

    'you can exit here or build up a list of all the errors if you like.
    if msg <> "" then
    LastOperationStatus = Msg
    return False
    return true
    end if
    end function

    You also need to validate User input before setting Object Properties....
    But how that is done is another subject.

    This is intended to outline how I implement the business objects to check
    Don Nablo, Oct 12, 2005
  5. wapsiii

    wapsiii Guest

    thanks, I'm slowy getting the hang of oop. I can see that I'm working
    along the same lines you outline, but not as consistently as you
    outline. Its really a great help to read you comments.

    Do you create a an interface with getItem, updateItem, deleteItem and
    addItem and have your business classes implement this interface? Or
    you you inherit from an abstract class?

    Regarding error/exception handling I have two readonly properties
    (HasError boolean and ErrorMessage String). I guess HasError is really
    not needed! I'm not sure if I should always have the business class
    catch errors and only have the calling class check ErrorMessage or if
    I should throw an error in the business class and have the asp.net
    pages catch the error!

    wapsiii, Oct 12, 2005
  6. wapsiii

    wapsiii Guest

    I wonder why you have a key param in you public function addItem? What
    key do you pass to this function?
    wapsiii, Oct 12, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.