Mixing object model and interface model.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Quail

    #1

    Mixing object model and interface model.

    Hi to all,

    Having come from a delphi environment, I remember that when using
    interfaces, we were told not to mix the object models and interface
    models. In other words, when I create an object, return and interface
    straight away, instead of creating an object of type object and then
    getting the interface. The reason given for this was that when passing
    interfaces around, the interface could
    release the object and then if I try to use the object reference, I
    would
    get access violations.

    In C# I have seen examples like:

    Person myPerson;
    AddressInterfac e IAddress;


    myPerson = Person.Create;
    AddressInterfac e = myPerson as IAddress;

    etc.

    Is this correct or should I do :

    AddressInterfac e = Person.Create;

    In which case I never ever use myPerson.

    TIA
    Steven.
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Mixing object model and interface model.

    Steven,

    I think the question is moot, because if you are going to use an
    interface model, then you should be using a class factory to create the
    classes, and have the factory return the interface. However, I really don't
    see the difference between:

    Person myPerson;
    AddressInterfac e IAddress;
    myPerson = Person.Create;
    AddressInterfac e = myPerson as IAddress;

    And:

    AddressInterfac e = Person.Create;

    Ultimately though, I would do this:

    AddressInterfac e IAddress = Person.Create;

    Only because it saves me three lines of code (as opposed to four or two
    in the examples you gave, the second example requires two because you need
    the declaration still).

    Also, I would only use the interface if there wasn't anything exposed in
    the underlying type of the implementation that you needed to access.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com


    "Steven Quail" <squail71@yahoo .com> wrote in message
    news:3aabc57a.0 307210506.7bd16 54c@posting.goo gle.com...[color=blue]
    > Hi to all,
    >
    > Having come from a delphi environment, I remember that when using
    > interfaces, we were told not to mix the object models and interface
    > models. In other words, when I create an object, return and interface
    > straight away, instead of creating an object of type object and then
    > getting the interface. The reason given for this was that when passing
    > interfaces around, the interface could
    > release the object and then if I try to use the object reference, I
    > would
    > get access violations.
    >
    > In C# I have seen examples like:
    >
    > Person myPerson;
    > AddressInterfac e IAddress;
    >
    >
    > myPerson = Person.Create;
    > AddressInterfac e = myPerson as IAddress;
    >
    > etc.
    >
    > Is this correct or should I do :
    >
    > AddressInterfac e = Person.Create;
    >
    > In which case I never ever use myPerson.
    >
    > TIA
    > Steven.[/color]


    Comment

    • Steven Quail

      #3
      Re: Mixing object model and interface model.

      Hi Nicholas,

      Thank you for your reply.

      What the problem with mixing models with delphi was that if I did the
      following:

      Person myPerson;
      myPerson = Person.Create;

      DealWithAddress (myPerson as IAddress); // where parameter is passed
      by value.

      myPerson.DoOthe rThing; // Leads to an access violation.
      ....

      Then once DealWithAddress is finished, the reference count goes down
      and myperson is destroy. If I use myPerson I will get an access
      violation.

      This was true if DealWithAddress had the IAddress parameter by value.

      My concern is in C#, would this still occur?

      TIA
      Steven.

      "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote in message news:<O$bkdt4TD HA.1688@TK2MSFT NGP11.phx.gbl>. ..[color=blue]
      > Steven,
      >
      > I think the question is moot, because if you are going to use an
      > interface model, then you should be using a class factory to create the
      > classes, and have the factory return the interface. However, I really don't
      > see the difference between:
      >
      > Person myPerson;
      > AddressInterfac e IAddress;
      > myPerson = Person.Create;
      > AddressInterfac e = myPerson as IAddress;
      >
      > And:
      >
      > AddressInterfac e = Person.Create;
      >
      > Ultimately though, I would do this:
      >
      > AddressInterfac e IAddress = Person.Create;
      >
      > Only because it saves me three lines of code (as opposed to four or two
      > in the examples you gave, the second example requires two because you need
      > the declaration still).
      >
      > Also, I would only use the interface if there wasn't anything exposed in
      > the underlying type of the implementation that you needed to access.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nicholas.paldin o@exisconsultin g.com
      >
      >
      > "Steven Quail" <squail71@yahoo .com> wrote in message
      > news:3aabc57a.0 307210506.7bd16 54c@posting.goo gle.com...[color=green]
      > > Hi to all,
      > >
      > > Having come from a delphi environment, I remember that when using
      > > interfaces, we were told not to mix the object models and interface
      > > models. In other words, when I create an object, return and interface
      > > straight away, instead of creating an object of type object and then
      > > getting the interface. The reason given for this was that when passing
      > > interfaces around, the interface could
      > > release the object and then if I try to use the object reference, I
      > > would
      > > get access violations.
      > >
      > > In C# I have seen examples like:
      > >
      > > Person myPerson;
      > > AddressInterfac e IAddress;
      > >
      > >
      > > myPerson = Person.Create;
      > > AddressInterfac e = myPerson as IAddress;
      > >
      > > etc.
      > >
      > > Is this correct or should I do :
      > >
      > > AddressInterfac e = Person.Create;
      > >
      > > In which case I never ever use myPerson.
      > >
      > > TIA
      > > Steven.[/color][/color]

      Comment

      • BS

        #4
        Re: Mixing object model and interface model.

        You will not have this problem with .NET. The CLR does not engage in
        reference-counting; the garbage collector dynamically scans for
        orphaned objects in the background. No object is destroyed as long as
        there is at least one reference to it somewhere in the application,
        whether that reference is typed as an interface, or as the object's
        class, or a base class of the object's class.

        squail71@yahoo. com (Steven Quail) wrote in message news:<3aabc57a. 0307220236.6758 1088@posting.go ogle.com>...[color=blue]
        > Hi Nicholas,
        >
        > Thank you for your reply.
        >
        > What the problem with mixing models with delphi was that if I did the
        > following:
        >
        > Person myPerson;
        > myPerson = Person.Create;
        >
        > DealWithAddress (myPerson as IAddress); // where parameter is passed
        > by value.
        >
        > myPerson.DoOthe rThing; // Leads to an access violation.
        > ...
        >
        > Then once DealWithAddress is finished, the reference count goes down
        > and myperson is destroy. If I use myPerson I will get an access
        > violation.
        >
        > This was true if DealWithAddress had the IAddress parameter by value.
        >
        > My concern is in C#, would this still occur?[/color]

        Comment

        Working...