Templated data-bound server control design question

Discussion in 'ASP .Net Building Controls' started by joe_s, Jul 24, 2003.

  1. joe_s

    joe_s Guest

    Hello,
    I am working on an n-tier .NET application. The presentation is through aspx
    pages and custom data-bound server controls which use templates to separate
    interface from content.

    In all the samples I've seen regarding such controls, the templates are
    instantiated in a class which inherits [Web]Control and implements
    INamingContainer (here's the QuickStart source which demonstrates this:
    http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/a
    spplus/samples/webforms/ctrlauth/templates/Repeater2.src&file=CS\Repeater2.c
    s&font=3). This class has all the properties which will eventually be shown
    on the page. What I want to know is if there is a way to accomplish the same
    thing without this class.

    This might seem strange, but the reason I'd like to do this, is that I've
    already got a class with the same properties in my business logic layer, and
    I don't want to create another almost identical class. It would make
    maintenance a nightmare. I can't use this class directly in the
    ITemplate.InstantiateIn() method because it does not inherit Control, or
    implement any interface, and it shouldn't.

    I hope I made myself clear. If you have any suggestions or questions, please
    do not hesitate to reply.

    Thank you,
    Joe
     
    joe_s, Jul 24, 2003
    #1
    1. Advertising

  2. Perhaps you should explain why you have a class in your business logic layer
    which you use for instantiating (presentation layer) templates.

    --
    John Saunders
    Internet Engineer



    "joe_s" <dontmail@localhost> wrote in message
    news:...
    > Hello,
    > I am working on an n-tier .NET application. The presentation is through

    aspx
    > pages and custom data-bound server controls which use templates to

    separate
    > interface from content.
    >
    > In all the samples I've seen regarding such controls, the templates are
    > instantiated in a class which inherits [Web]Control and implements
    > INamingContainer (here's the QuickStart source which demonstrates this:
    >

    http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/a
    >

    spplus/samples/webforms/ctrlauth/templates/Repeater2.src&file=CS\Repeater2.c
    > s&font=3). This class has all the properties which will eventually be

    shown
    > on the page. What I want to know is if there is a way to accomplish the

    same
    > thing without this class.
    >
    > This might seem strange, but the reason I'd like to do this, is that I've
    > already got a class with the same properties in my business logic layer,

    and
    > I don't want to create another almost identical class. It would make
    > maintenance a nightmare. I can't use this class directly in the
    > ITemplate.InstantiateIn() method because it does not inherit Control, or
    > implement any interface, and it shouldn't.
    >
    > I hope I made myself clear. If you have any suggestions or questions,

    please
    > do not hesitate to reply.
    >
    > Thank you,
    > Joe
    >
    >
    >
    >
     
    John Saunders, Jul 24, 2003
    #2
    1. Advertising

  3. joe_s

    joe_s Guest

    I am not using that class, I can't because it doesn't inherit Control and
    implement INamingContainer, and I know I shouldn't.

    If you check out that link, you'll see that there is a class
    (RepeaterItem.cs) which defines a data item with the data that will be
    displayed on the page. If I implement my control in this way, I would need
    to create such a class, but it would be virtually identical to my BL class,
    except for the inheritance. Just to illustrate, here's an example:

    // BL layer
    class Car
    {
    int id;
    string manufacturer;
    string model;

    // get/set properties here
    }

    public class CarCollection : ArrayList
    {
    public CarCollection() : base() {}
    public CarCollection(ICollection c) : base(c) {}
    }

    class DBProcs
    {
    public CarCollection GetCarsFromDB()
    {
    CarCollection cc = new CarCollection();
    // get data from db and fill the collection with Car objects //
    return cc;
    }
    }

    Let's say I want to display these items in by using a server control as I
    explained in the original post. I would need to create the following class
    in the presentation layer

    // presentation
    class CarItem : Control, INamingContainer
    {
    // everything's same as in the Car class above
    // bad for maintenance
    }

    class MyServerControl : Control, INamingContainer
    {
    ...
    protected override void OnDataBinding(EventArgs e)
    {
    CarCollection cc = new CarCollection();
    cc = GetCarsFromDB();
    ...
    foreach(Car car in cc)
    {
    // I want to avoid the following
    CarItem ci = new CarItem();
    ci.Model = car.Model;
    ci.Manufacturer = car.Manufacturer;

    ItemTemplate.InstantiateIn(ci);
    ...
    }
    ...
    }
    ...
    }

    The syntax may not be 100% correct, but I hope you understand my problem
    now.

    "John Saunders" <> wrote in message
    news:Ov$...
    > Perhaps you should explain why you have a class in your business logic

    layer
    > which you use for instantiating (presentation layer) templates.
    >
    > --
    > John Saunders
    > Internet Engineer
    >
    >
    >
    > "joe_s" <dontmail@localhost> wrote in message
    > news:...
    > > Hello,
    > > I am working on an n-tier .NET application. The presentation is through

    > aspx
    > > pages and custom data-bound server controls which use templates to

    > separate
    > > interface from content.
    > >
    > > In all the samples I've seen regarding such controls, the templates are
    > > instantiated in a class which inherits [Web]Control and implements
    > > INamingContainer (here's the QuickStart source which demonstrates this:
    > >

    >

    http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/a
    > >

    >

    spplus/samples/webforms/ctrlauth/templates/Repeater2.src&file=CS\Repeater2.c
    > > s&font=3). This class has all the properties which will eventually be

    > shown
    > > on the page. What I want to know is if there is a way to accomplish the

    > same
    > > thing without this class.
    > >
    > > This might seem strange, but the reason I'd like to do this, is that

    I've
    > > already got a class with the same properties in my business logic layer,

    > and
    > > I don't want to create another almost identical class. It would make
    > > maintenance a nightmare. I can't use this class directly in the
    > > ITemplate.InstantiateIn() method because it does not inherit Control, or
    > > implement any interface, and it shouldn't.
    > >
    > > I hope I made myself clear. If you have any suggestions or questions,

    > please
    > > do not hesitate to reply.
    > >
    > > Thank you,
    > > Joe
    > >
    > >
    > >
    > >

    >
    >
     
    joe_s, Jul 25, 2003
    #3
  4. Joe, simply have your CarItem class hold a reference to a Car instead of
    duplicating all the properties:

    foreach(Car car in cc)
    {
    if (ItemTemplate != null)
    {
    CarItem ci = new CarItem(car);
    ItemTemplate.InstantiateIn(ci);
    }
    }
    ...

    class CarItem : Control, INamingContainer
    {
    private Car m_Car;

    public CarItem(Car car)
    {
    m_Car = car;
    }

    public Car DataItem
    {
    get {return m_Car;}
    }
    }
    ....

    Your templates can then reference DataBinder.Evan(Container.DataItem,
    "Manufacturer");


    BTW, I'd separate the database access from the control. Give the control a
    DataSource property of type CarCollection and let the page developer decide
    what sort of car collection is wanted. You might even make the DataSource
    property of type IEnumerable and let the control check at runtime whether
    the enumerated items happen to be of type Car (or some derived class
    thereof). That would allow you to use other collection types in the future -
    a HashTable of cars, keyed on Owner, for instance.
    --
    John Saunders
    Internet Engineer



    "joe_s" <dontmail@localhost> wrote in message
    news:...
    > I am not using that class, I can't because it doesn't inherit Control and
    > implement INamingContainer, and I know I shouldn't.
    >
    > If you check out that link, you'll see that there is a class
    > (RepeaterItem.cs) which defines a data item with the data that will be
    > displayed on the page. If I implement my control in this way, I would need
    > to create such a class, but it would be virtually identical to my BL

    class,
    > except for the inheritance. Just to illustrate, here's an example:
    >
    > // BL layer
    > class Car
    > {
    > int id;
    > string manufacturer;
    > string model;
    >
    > // get/set properties here
    > }
    >
    > public class CarCollection : ArrayList
    > {
    > public CarCollection() : base() {}
    > public CarCollection(ICollection c) : base(c) {}
    > }
    >
    > class DBProcs
    > {
    > public CarCollection GetCarsFromDB()
    > {
    > CarCollection cc = new CarCollection();
    > // get data from db and fill the collection with Car objects //
    > return cc;
    > }
    > }
    >
    > Let's say I want to display these items in by using a server control as I
    > explained in the original post. I would need to create the following class
    > in the presentation layer
    >
    > // presentation
    > class CarItem : Control, INamingContainer
    > {
    > // everything's same as in the Car class above
    > // bad for maintenance
    > }
    >
    > class MyServerControl : Control, INamingContainer
    > {
    > ...
    > protected override void OnDataBinding(EventArgs e)
    > {
    > CarCollection cc = new CarCollection();
    > cc = GetCarsFromDB();
    > ...
    > foreach(Car car in cc)
    > {
    > // I want to avoid the following
    > CarItem ci = new CarItem();
    > ci.Model = car.Model;
    > ci.Manufacturer = car.Manufacturer;
    >
    > ItemTemplate.InstantiateIn(ci);
    > ...
    > }
    > ...
    > }
    > ...
    > }
    >
    > The syntax may not be 100% correct, but I hope you understand my problem
    > now.
    >
    > "John Saunders" <> wrote in message
    > news:Ov$...
    > > Perhaps you should explain why you have a class in your business logic

    > layer
    > > which you use for instantiating (presentation layer) templates.
    > >
    > > --
    > > John Saunders
    > > Internet Engineer
    > >
    > >
    > >
    > > "joe_s" <dontmail@localhost> wrote in message
    > > news:...
    > > > Hello,
    > > > I am working on an n-tier .NET application. The presentation is

    through
    > > aspx
    > > > pages and custom data-bound server controls which use templates to

    > > separate
    > > > interface from content.
    > > >
    > > > In all the samples I've seen regarding such controls, the templates

    are
    > > > instantiated in a class which inherits [Web]Control and implements
    > > > INamingContainer (here's the QuickStart source which demonstrates

    this:
    > > >

    > >

    >

    http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/a
    > > >

    > >

    >

    spplus/samples/webforms/ctrlauth/templates/Repeater2.src&file=CS\Repeater2.c
    > > > s&font=3). This class has all the properties which will eventually be

    > > shown
    > > > on the page. What I want to know is if there is a way to accomplish

    the
    > > same
    > > > thing without this class.
    > > >
    > > > This might seem strange, but the reason I'd like to do this, is that

    > I've
    > > > already got a class with the same properties in my business logic

    layer,
    > > and
    > > > I don't want to create another almost identical class. It would make
    > > > maintenance a nightmare. I can't use this class directly in the
    > > > ITemplate.InstantiateIn() method because it does not inherit Control,

    or
    > > > implement any interface, and it shouldn't.
    > > >
    > > > I hope I made myself clear. If you have any suggestions or questions,

    > > please
    > > > do not hesitate to reply.
    > > >
    > > > Thank you,
    > > > Joe
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    John Saunders, Jul 25, 2003
    #4
  5. joe_s

    joe_s Guest

    Wow, that's very simple. I don't know what I was thinking.

    As for separating database access, that's exactly what I've done, the code
    below was just quickly put together to illustrate the problem.

    Thank you very much for your help, John.

    "John Saunders" <> wrote in message
    news:...
    > Joe, simply have your CarItem class hold a reference to a Car instead of
    > duplicating all the properties:
    >
    > foreach(Car car in cc)
    > {
    > if (ItemTemplate != null)
    > {
    > CarItem ci = new CarItem(car);
    > ItemTemplate.InstantiateIn(ci);
    > }
    > }
    > ..
    >
    > class CarItem : Control, INamingContainer
    > {
    > private Car m_Car;
    >
    > public CarItem(Car car)
    > {
    > m_Car = car;
    > }
    >
    > public Car DataItem
    > {
    > get {return m_Car;}
    > }
    > }
    > ...
    >
    > Your templates can then reference DataBinder.Evan(Container.DataItem,
    > "Manufacturer");
    >
    >
    > BTW, I'd separate the database access from the control. Give the control a
    > DataSource property of type CarCollection and let the page developer

    decide
    > what sort of car collection is wanted. You might even make the DataSource
    > property of type IEnumerable and let the control check at runtime whether
    > the enumerated items happen to be of type Car (or some derived class
    > thereof). That would allow you to use other collection types in the

    future -
    > a HashTable of cars, keyed on Owner, for instance.
    > --
    > John Saunders
    > Internet Engineer
    >
     
    joe_s, Jul 25, 2003
    #5
    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. cosine... zero
    Replies:
    0
    Views:
    694
    cosine... zero
    Jun 9, 2004
  2. Replies:
    0
    Views:
    2,283
  3. Mario Krsnic
    Replies:
    0
    Views:
    398
    Mario Krsnic
    Jun 23, 2006
  4. news.microsoft.com

    Templated Data-Bound Control Sample

    news.microsoft.com, Aug 20, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    217
    Sam Fields
    Aug 20, 2004
  5. Manu

    Data Bound Templated Control - design time problem

    Manu, May 22, 2006, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    124
Loading...

Share This Page