conditional object assignment

Discussion in 'ASP .Net' started by ThatsIT.net.au, Aug 19, 2009.

  1. I have an application where different types of users login. depending what
    role they are in they are assigned a diffrent user object, admin dataEntry
    and so on.

    I can do this using some if statments and then makiung a instance of the
    corect object. the propble with this is that i have to do this on every
    page. what i would like to do is load the same commonUserObject for each
    user, and in the class then assign the right object as a child object. i
    could then access the object like this


    dim oUser as commonUserObject = new commonUserObject
    oUser.childObject.someMethod

    This will do, but i was wondering if there was a more elegent way of doing
    this, can i some how make the commonUserObject morph into the right object.
    ThatsIT.net.au, Aug 19, 2009
    #1
    1. Advertising

  2. ThatsIT.net.au

    Stan Guest

    On 19 Aug, 14:34, "ThatsIT.net.au" <me@work> wrote:
    > I have an application where different types of users login. depending what
    > role they are in they are assigned a diffrent user object, admin dataEntry
    > and so on.
    >
    > I can do this using some if statments and then makiung a instance of the
    > corect object. the propble with this is that i have to do this on every
    > page. what i would like to do is load the same commonUserObject for each
    > user, and in the class then assign the right object as a child object. i
    > could then access the object like this
    >
    > dim oUser as commonUserObject = new commonUserObject
    > oUser.childObject.someMethod
    >
    > This will do, but i was wondering if there was a more elegent way of doing
    > this, can i some how make the commonUserObject morph into the right object.


    Sounds like a case for creating a class and adding it to the App_Code
    folder. The constructor could take the user role as a parameter and
    configure itself accordingly. That way you can re-use it throughout
    the whole site. Maintenance will be far easier because all the code
    for setting it up wil be contained in one place.
    Stan, Aug 19, 2009
    #2
    1. Advertising

  3. "ThatsIT.net.au" <me@work> wrote in
    news::

    > I have an application where different types of users login. depending
    > what role they are in they are assigned a diffrent user object, admin
    > dataEntry and so on.
    >
    > I can do this using some if statments and then makiung a instance of
    > the corect object. the propble with this is that i have to do this on
    > every page. what i would like to do is load the same commonUserObject
    > for each user, and in the class then assign the right object as a
    > child object. i could then access the object like this
    >
    >
    > dim oUser as commonUserObject = new commonUserObject
    > oUser.childObject.someMethod
    >
    > This will do, but i was wondering if there was a more elegent way of
    > doing this, can i some how make the commonUserObject morph into the
    > right object.



    Set up an object with the common fields. You can load this into session
    if you want to avoid going to the database all the time. For the more
    specific objects, have them adorn the main user object. If you want, you
    can lazy load the other object. This is where you are aiming for,
    essentially.

    Another method of doing this is to "subclass" the user object into more
    specific objects with additional features. On pages you only need the
    common bits, simply use it as a user object. On more specific pages,
    GetType() and then cast as the other type of object and use the extra
    bits.

    If you are talking configuration bits (an employee has an SSN, but other
    users do not), you can use the built in Profile bits and simply not
    assign the extra items for other users. This is very flexible, but note
    that the out of the box profile is a bit problematic if you get too
    creative (and cleaning up a fubar is a pain). I use custom profile
    providers for this reason.

    Peace and Grace,

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    *******************************************
    | Think outside the box! |
    *******************************************
    Gregory A. Beamer, Aug 19, 2009
    #3
  4. "Stan" <> wrote in message
    news:...
    > On 19 Aug, 14:34, "ThatsIT.net.au" <me@work> wrote:
    >> I have an application where different types of users login. depending
    >> what
    >> role they are in they are assigned a diffrent user object, admin
    >> dataEntry
    >> and so on.
    >>
    >> I can do this using some if statments and then makiung a instance of the
    >> corect object. the propble with this is that i have to do this on every
    >> page. what i would like to do is load the same commonUserObject for each
    >> user, and in the class then assign the right object as a child object. i
    >> could then access the object like this
    >>
    >> dim oUser as commonUserObject = new commonUserObject
    >> oUser.childObject.someMethod
    >>
    >> This will do, but i was wondering if there was a more elegent way of
    >> doing
    >> this, can i some how make the commonUserObject morph into the right
    >> object.

    >
    > Sounds like a case for creating a class and adding it to the App_Code
    > folder. The constructor could take the user role as a parameter and
    > configure itself accordingly. That way you can re-use it throughout
    > the whole site. Maintenance will be far easier because all the code
    > for setting it up wil be contained in one place.


    I think want you are sujesting is much what i have done so far, but I cant
    do this in the app_code foldee as the app will have many front ends built by
    whoever, what i want to do is handle all this in a lower layer.
    ThatsIT.net.au, Aug 20, 2009
    #4
  5. "Gregory A. Beamer" <> wrote in message
    news:Xns9C6C6C5103BF3gbworld@207.46.248.16...
    > "ThatsIT.net.au" <me@work> wrote in
    > news::
    >
    >> I have an application where different types of users login. depending
    >> what role they are in they are assigned a diffrent user object, admin
    >> dataEntry and so on.
    >>
    >> I can do this using some if statments and then makiung a instance of
    >> the corect object. the propble with this is that i have to do this on
    >> every page. what i would like to do is load the same commonUserObject
    >> for each user, and in the class then assign the right object as a
    >> child object. i could then access the object like this
    >>
    >>
    >> dim oUser as commonUserObject = new commonUserObject
    >> oUser.childObject.someMethod
    >>
    >> This will do, but i was wondering if there was a more elegent way of
    >> doing this, can i some how make the commonUserObject morph into the
    >> right object.

    >
    >
    > Set up an object with the common fields. You can load this into session
    > if you want to avoid going to the database all the time. For the more
    > specific objects, have them adorn the main user object. If you want, you
    > can lazy load the other object. This is where you are aiming for,
    > essentially.
    >
    > Another method of doing this is to "subclass" the user object into more
    > specific objects with additional features. On pages you only need the
    > common bits, simply use it as a user object. On more specific pages,
    > GetType() and then cast as the other type of object and use the extra
    > bits.
    >
    > If you are talking configuration bits (an employee has an SSN, but other
    > users do not), you can use the built in Profile bits and simply not
    > assign the extra items for other users. This is very flexible, but note
    > that the out of the box profile is a bit problematic if you get too
    > creative (and cleaning up a fubar is a pain). I use custom profile
    > providers for this reason.
    >
    > Peace and Grace,
    >



    I really dont want to do any casting or detection, well not in more than one
    spot that is.
    The site will have many front ends, so all the security and validation and
    such will be done on a middle layer. In between my business object layer and
    the front ends I have waht I am calling the user layer. here i have
    different user classes derived from a abstract class. that contatin all the
    methods allowed to that user. all common methods and properties are
    inhertied from the abstract class. i have done things this way becuse as
    long as the user has the right user object I need not worry about
    authorization as he is allowed to do or access anything in his class. The
    front end will only open one object the correct user object, then in return
    the user object will access the bisiness object layer as if it were the
    front end.

    how i am doing it is fine, but I just thought it would be a little better if
    i did not have to use the common.child syntax as it may be a little
    confusing for the front end developers.
    what i would like is to use a simple

    dim oUser as commonUserObject = new commonUserObject
    oUser.someMethod

    rather then

    dim oUser as commonUserObject = new commonUserObject
    oUser.childObject.someMethod

    But i can live with it

    Thanks




    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    >
    > Twitter: @gbworld
    > Blog: http://gregorybeamer.spaces.live.com
    >
    > *******************************************
    > | Think outside the box! |
    > *******************************************
    ThatsIT.net.au, Aug 20, 2009
    #5
  6. "ThatsIT.net.au" <me@work> wrote in
    news::


    > how i am doing it is fine, but I just thought it would be a little
    > better if i did not have to use the common.child syntax as it may be a
    > little confusing for the front end developers.
    > what i would like is to use a simple
    >
    > dim oUser as commonUserObject = new commonUserObject
    > oUser.someMethod
    >
    > rather then
    >
    > dim oUser as commonUserObject = new commonUserObject
    > oUser.childObject.someMethod


    I am rolling off the top of my head, so there is no filter here.

    Reflection - a bit too heavy probably
    Extension methods - fine, but have to handle when a method does not
    exist for an object type

    You might even try a combination of both, but while that will work, it
    makes things more complex than the child object, so ouch on that.

    The child object is certainly the safest, as a person trying a method on
    a child object that is incorrect will not compile.

    Peace and Grace,

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    *******************************************
    | Think outside the box! |
    *******************************************
    Gregory A. Beamer, Aug 20, 2009
    #6
  7. "Gregory A. Beamer" <> wrote in message
    news:Xns9C6D82B2C89D2gbworld@207.46.248.16...
    > "ThatsIT.net.au" <me@work> wrote in
    > news::
    >
    >
    >> how i am doing it is fine, but I just thought it would be a little
    >> better if i did not have to use the common.child syntax as it may be a
    >> little confusing for the front end developers.
    >> what i would like is to use a simple
    >>
    >> dim oUser as commonUserObject = new commonUserObject
    >> oUser.someMethod
    >>
    >> rather then
    >>
    >> dim oUser as commonUserObject = new commonUserObject
    >> oUser.childObject.someMethod

    >
    > I am rolling off the top of my head, so there is no filter here.
    >
    > Reflection - a bit too heavy probably
    > Extension methods - fine, but have to handle when a method does not
    > exist for an object type
    >
    > You might even try a combination of both, but while that will work, it
    > makes things more complex than the child object, so ouch on that.
    >
    > The child object is certainly the safest, as a person trying a method on
    > a child object that is incorrect will not compile.
    >
    > Peace and Grace,
    >




    yes it seems to be the best answer




    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    >
    > Twitter: @gbworld
    > Blog: http://gregorybeamer.spaces.live.com
    >
    > *******************************************
    > | Think outside the box! |
    > *******************************************
    ThatsIT.net.au, Aug 21, 2009
    #7
  8. ThatsIT.net.au wrote:
    > I have an application where different types of users login. depending
    > what role they are in they are assigned a diffrent user object, admin
    > dataEntry and so on.
    >
    > I can do this using some if statments and then makiung a instance of the
    > corect object. the propble with this is that i have to do this on every
    > page. what i would like to do is load the same commonUserObject for each
    > user, and in the class then assign the right object as a child object. i
    > could then access the object like this
    >
    >
    > dim oUser as commonUserObject = new commonUserObject
    > oUser.childObject.someMethod
    >
    > This will do, but i was wondering if there was a more elegent way of
    > doing this, can i some how make the commonUserObject morph into the
    > right object.
    >


    You can make the specific user objects inherit your common user object,
    and use a factory method to create them:

    Dim user As CommonUserObject = CommonUserObject.Create()
    user.SomeMethod()

    The static Create method will create a specific user object depending on
    the conditions, but it's return type is still CommonUserObject.

    --
    Göran Andersson
    _____
    http://www.guffa.com
    Göran Andersson, Aug 21, 2009
    #8
  9. "Göran Andersson" <> wrote in message
    news:...
    > ThatsIT.net.au wrote:
    >> I have an application where different types of users login. depending
    >> what role they are in they are assigned a diffrent user object, admin
    >> dataEntry and so on.
    >>
    >> I can do this using some if statments and then makiung a instance of the
    >> corect object. the propble with this is that i have to do this on every
    >> page. what i would like to do is load the same commonUserObject for each
    >> user, and in the class then assign the right object as a child object. i
    >> could then access the object like this
    >>
    >>
    >> dim oUser as commonUserObject = new commonUserObject
    >> oUser.childObject.someMethod
    >>
    >> This will do, but i was wondering if there was a more elegent way of
    >> doing this, can i some how make the commonUserObject morph into the right
    >> object.
    >>

    >
    > You can make the specific user objects inherit your common user object,
    > and use a factory method to create them:
    >
    > Dim user As CommonUserObject = CommonUserObject.Create()
    > user.SomeMethod()
    >
    > The static Create method will create a specific user object depending on
    > the conditions, but it's return type is still CommonUserObject.
    >



    thanks
    sillly me , i had instance object in my mind and could think outside it.
    thanks




    > --
    > Göran Andersson
    > _____
    > http://www.guffa.com
    ThatsIT.net.au, Aug 21, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. itsme
    Replies:
    1
    Views:
    1,611
    Ralf Hildebrandt
    Jul 23, 2003
  2. Anand P Paralkar
    Replies:
    2
    Views:
    9,348
    Srinivasan Venkataramanan
    Aug 4, 2003
  3. Johnsy Joseph

    Conditional assignment to signals

    Johnsy Joseph, Sep 21, 2004, in forum: VHDL
    Replies:
    8
    Views:
    4,852
    Raghavendra
    Sep 24, 2004
  4. Replies:
    2
    Views:
    2,760
  5. Alec S.
    Replies:
    10
    Views:
    10,083
    Alec S.
    Apr 16, 2005
Loading...

Share This Page