Creating a derived object from a based object

W

Warren

Hi Gurus,

Is there a way for me creating a dervied object with its base referencing a
base class object without doing a deep copy?


I am trying to something similar to this

<facility> is a generated base class for access to a database table
[Facility]
<room> is a dervied class from <facility> and some internal interface <I>
<theatre> is another derived class from <facility>

We would like to use LINQ to instantiate rooms and theatres from the single
database table. LINQ preset the range variable type to <facility> and I
myself do not want to change the generated base class from Visual Studio;
but it seems there are no easy way to wrap a facility object with a room
object or theatre object.

What is the right way to do this?

Thanks in advance.
Warren
 
G

Göran Andersson

Warren said:
Hi Gurus,

Is there a way for me creating a dervied object with its base referencing a
base class object without doing a deep copy?


I am trying to something similar to this

<facility> is a generated base class for access to a database table
[Facility]
<room> is a dervied class from <facility> and some internal interface <I>
<theatre> is another derived class from <facility>

We would like to use LINQ to instantiate rooms and theatres from the single
database table. LINQ preset the range variable type to <facility> and I
myself do not want to change the generated base class from Visual Studio;
but it seems there are no easy way to wrap a facility object with a room
object or theatre object.

What is the right way to do this?

Thanks in advance.
Warren

No, there is no way to convert an object to any of it's derived classes.

You could encapsulate the facility class instead of inheriting it, then
when you create a new room or theatre instance using a facility instance
you just store the reference to it instead of copying all the data in it.
 
P

Paul

If you give us the real world view rather than your interpreted view of what
you are trying to achieve it m ay be easier to help.

Try a domian model rather than an Class Diagram approach...

Things that come to mind.

Should room/theatre be a composite aggregation ('has a') of facility rather
than an 'is a'. Or should facilities be a composite aggregation ('has a') of
Room/theatre.

Is a theatre a building or a room. If a room why not defined from room, it
all depends on you use cases and your domain model based form those use cases.

BTW Linq is not the best method of representing OO principles it too closely
resembles the SQL world rather than an OO interpretation of the real world.

Just my opinion.
 
W

Warren

Dear Paul and All,

Thanks for all your help and advices.

In fact, I admit some information is fictious. I am trying to work out some
of the details to ease development in a later project. The code is attached
below. What I am try to is to create a service layer to "facitlity" while
abstrasting the details. In one hand, I want to use the LINQ to similify
coders work; on the other hand I want to ensure the LINQ class can be
inherited without a lot of coding (When using a has-a relationship, is there
a clean way of exposing the contained object's members to the callers); and
I understand why you said LINQ is not the best representation of OO.

Other patterns or better ways are always welcome, I am not that familiar
myself (sigh).

Regards,
Warren

1) Facility Interface.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace BookingSvr
{

[XmlRoot(ElementName = "Facility")]
public interface IFacility : ISerializable
{
// properties
[XmlElement(ElementName = "FacilityName",DataType="string")]
string FacilityName { get; set; }

[XmlElement(ElementName = "Capacity", DataType = "short")]
short Capacity { get; set; }

[XmlElement(ElementName = "Equipment", DataType = "string")]
string Equipment { get; set; }

[XmlElement(ElementName = "Location", DataType = "string")]
string Location { get; set; }
}
}



2) Implementation.cs

....

namespace BookingSvr
{
[Serializable]
[Table(Name = "dbo.Facilities")]
public class Room : Facility, IFacility
{
public Room() { }

protected Room(SerializationInfo info, StreamingContext ctxt)
{
Capacity = (short)info.GetValue("Capacity", typeof(short));
Equipment = (string)info.GetValue("Equipment", typeof(string));
Location = (string)info.GetValue("Location", typeof(string));
FacilityName = (string)info.GetValue("Facility",
typeof(string));
}

public void GetObjectData(SerializationInfo info, StreamingContext
ctxt)
{
info.AddValue("Capacity", Capacity);
info.AddValue("Equipment", Equipment);
info.AddValue("Location", Location);
info.AddValue("FacilityName", FacilityName);
}

public new short Capacity
{
get { return base.Capacity; }
set { base.Capacity = value; }
}

[Column(Storage = "_Equipment", DbType = "NChar(500) NOT NULL",
CanBeNull = false)]
public new string Equipment
{
get { return base.Equipment; }
set { base.Equipment = value; }
}

[Column(Storage = "_Location", DbType = "NChar(200) NOT NULL",
CanBeNull = false)]
public new string Location
{
get { return base.Location; }
set { base.Location = value; }
}

[Column(Storage = "_FacilityName", DbType = "NChar(100) NOT NULL",
CanBeNull = false)]
public new string FacilityName
{
get { return base.FacilityName; }
set { base.FacilityName = value; }
}
}

public partial class FacilitiesDBDataContext :
System.Data.Linq.DataContext
{
public System.Data.Linq.Table<Room> Rooms
{
get
{
return this.GetTable<Room>();
}
}
}

3) Generated LINQ class, I change some of the private labels to protected

[Table(Name="dbo.Facilities")]
public partial class Facility
{

protected string _FacilityName;

protected string _Equipment;

protected string _Location;

protected short _Capacity;

public Facility()
{
}

[Column(Storage="_FacilityName", DbType="NChar(100) NOT NULL",
CanBeNull=false)]
public string FacilityName
{
get
{
return this._FacilityName;
}
set
{
if ((this._FacilityName != value))
{
this._FacilityName = value;
}
}
}

[Column(Storage="_Equipment", DbType="NChar(500) NOT NULL",
CanBeNull=false)]
public string Equipment
{
get
{
return this._Equipment;
}
set
{
if ((this._Equipment != value))
{
this._Equipment = value;
}
}
}

[Column(Storage="_Location", DbType="NChar(200) NOT NULL",
CanBeNull=false)]
public string Location
{
get
{
return this._Location;
}
set
{
if ((this._Location != value))
{
this._Location = value;
}
}
}

[Column(Storage="_Capacity", DbType="SmallInt NOT NULL")]
public short Capacity
{
get
{
return this._Capacity;
}
set
{
if ((this._Capacity != value))
{
this._Capacity = value;
}
}
}
}


4) Coding to trigger collection of data; now I can do a loadRoom which
contains a subset of facilities

....

private IList<IFacility> rooms;
private IList<IFacility> theatres;

private void loadRooms()
{
using (FacilitiesDBDataContext db = new
FacilitiesDBDataContext())
{
db.Log = Console.Out;
db.DeferredLoadingEnabled = false;

var q = from IFacility f in db.Rooms select f; // some
where clause ommited now
rooms = q.ToList();
}
}

Paul said:
If you give us the real world view rather than your interpreted view of
what
you are trying to achieve it m ay be easier to help.

Try a domian model rather than an Class Diagram approach...

Things that come to mind.

Should room/theatre be a composite aggregation ('has a') of facility
rather
than an 'is a'. Or should facilities be a composite aggregation ('has a')
of
Room/theatre.

Is a theatre a building or a room. If a room why not defined from room, it
all depends on you use cases and your domain model based form those use
cases.

BTW Linq is not the best method of representing OO principles it too
closely
resembles the SQL world rather than an OO interpretation of the real
world.

Just my opinion.

Warren said:
Hi Gurus,

Is there a way for me creating a dervied object with its base referencing
a
base class object without doing a deep copy?


I am trying to something similar to this

<facility> is a generated base class for access to a database table
[Facility]
<room> is a dervied class from <facility> and some internal interface <I>
<theatre> is another derived class from <facility>

We would like to use LINQ to instantiate rooms and theatres from the
single
database table. LINQ preset the range variable type to <facility> and I
myself do not want to change the generated base class from Visual Studio;
but it seems there are no easy way to wrap a facility object with a room
object or theatre object.

What is the right way to do this?

Thanks in advance.
Warren
 
W

Warren

Thanks for your help.

I think the code snippet is suffiicent (in part I have largely changed the
testing code already), but my intention maybe unclear:

The snippet is exactly the coding that is working now as :

Return type is System.Data.Linq.Table<Room> for var q=from f in db.Rooms
select f; and
System.Data.Linq.Table<IFacility> for var q=from IFacility f in db.Rooms
select f;

My intention is to create a single List of different type of objects, where
all the objects are instantiated from a single or multiple database tables,
and that the class specific functions to the objects can be invoked easily.

By converting to IFacility, I can actually create a single IList<IFacility>
that includes a list of Rooms + Theatres upcasted to IFacility (thus a
single LINQ query to work against both Rooms and Theatres). If not using
this approach, I can maintain separate list of Rooms and Theatres, but
multiple queries are required on each separate list. A last alternative
is to do an IList<objects> which I am currently exploring.

The reason of using IFacility instead of an actualy object is to hide the
details of Facility from coders.

Warren


Peter Duniho said:
[...]
private void loadRooms()
{
using (FacilitiesDBDataContext db = new
FacilitiesDBDataContext())
{
db.Log = Console.Out;
db.DeferredLoadingEnabled = false;

var q = from IFacility f in db.Rooms select f; // some
where clause ommited now
rooms = q.ToList();
}
}

It is still not possible to fully understand your use of LINQ. In
particular, what kind of collection is "db.Rooms"?

Based on naming alone, there's a strong hint that in spite of your using
the interface IFacility, in fact every "f" you get is going to be a Room.
If that's true, then the solution is obvious: quit using IFacility as the
type you get from the collection, and use Room instead.

If that's not in fact a correct understanding of your database, then
you'll need to explore some of the other options mentioned. For example,
instead of "select f", you could replace "f" with a call to a factory
method that converts the "f" instance to an appropriate Room instance (or
whatever, according to your needs).

As before, without a true concise-but-complete code sample (your previous
post contained code that was neither concise nor complete), it's not
really possible to fully comprehend the scenario. There's an art to
creating a suitable code sample, but the general rule is: it has to
compile without the addition of anything else, and while you have to "dumb
down" the code so that it doesn't contain redundant or irrelevant
information (thus ensuring that it's concise), you still need to preserve
enough of the problem so that there's no ambiguity about what you're
doing.

Pete
 
P

Paul

I think I know what you are trying to do, I cannot comment on the Linq stuff,
I understand it cause Ive created my own attribute driven data access
framework but all that select stuff I leave to the DB.

Anyway, my presumtion is you are tring to do something similar to what I
have done before.My particular case was Orders (Provisioning, cacelation etc
etc) all with similar but differing properties.

My advice is do not use an interface for anything other than behaviour, an
abstract class is much better. From what u have said your Facitiy class
should be abstract and your room and theatre classes should derive from that
class. This will stop you having to constantly redefine generic properties
and methods.

Ahhh but this is where linq fails big time if I recollect, it cannot see
inherited properties..Hmmmm..Yeah that sucks and was why I stopped reading
about Linq. My model base class I used for years would not work with it (id,
datearchived fields generic to all my tables), and i will stick to what I
know works.

This goes back to what I said previously Linq sucks with OO in mind, it just
so clearly reperesents table structure rather than the OO view of the world.

Anyway what I did with the orders was to abstract my different order type
tables with stored procedures. So for a list of all orders just pull up a
list of orders showing only the common properties (Select from orders) , then
if clicking on a order say look at the order type property in a factory to
determine which order type to load call the relevent stiored proc that links
a generic order table with the concrete order table (select from order join
concreteorder on....). I don't think you can do this with Linq.

If you can you will prolly need to create a data model that uses the linq
model as a datasource maybe entity <> LINQ <> SQL would do it. But at this
point sounds like more work than creating a custom DAL to me.

It maybe worth you looking at CSLA unless you are looking to utilise SOAP
then that has some limitations too, or biting the bullet and creating an in
house DAL/data provider framework.

Warren said:
Thanks for your help.

I think the code snippet is suffiicent (in part I have largely changed the
testing code already), but my intention maybe unclear:

The snippet is exactly the coding that is working now as :

Return type is System.Data.Linq.Table<Room> for var q=from f in db.Rooms
select f; and
System.Data.Linq.Table<IFacility> for var q=from IFacility f in db.Rooms
select f;

My intention is to create a single List of different type of objects, where
all the objects are instantiated from a single or multiple database tables,
and that the class specific functions to the objects can be invoked easily.

By converting to IFacility, I can actually create a single IList<IFacility>
that includes a list of Rooms + Theatres upcasted to IFacility (thus a
single LINQ query to work against both Rooms and Theatres). If not using
this approach, I can maintain separate list of Rooms and Theatres, but
multiple queries are required on each separate list. A last alternative
is to do an IList<objects> which I am currently exploring.

The reason of using IFacility instead of an actualy object is to hide the
details of Facility from coders.

Warren


Peter Duniho said:
[...]
private void loadRooms()
{
using (FacilitiesDBDataContext db = new
FacilitiesDBDataContext())
{
db.Log = Console.Out;
db.DeferredLoadingEnabled = false;

var q = from IFacility f in db.Rooms select f; // some
where clause ommited now
rooms = q.ToList();
}
}

It is still not possible to fully understand your use of LINQ. In
particular, what kind of collection is "db.Rooms"?

Based on naming alone, there's a strong hint that in spite of your using
the interface IFacility, in fact every "f" you get is going to be a Room.
If that's true, then the solution is obvious: quit using IFacility as the
type you get from the collection, and use Room instead.

If that's not in fact a correct understanding of your database, then
you'll need to explore some of the other options mentioned. For example,
instead of "select f", you could replace "f" with a call to a factory
method that converts the "f" instance to an appropriate Room instance (or
whatever, according to your needs).

As before, without a true concise-but-complete code sample (your previous
post contained code that was neither concise nor complete), it's not
really possible to fully comprehend the scenario. There's an art to
creating a suitable code sample, but the general rule is: it has to
compile without the addition of anything else, and while you have to "dumb
down" the code so that it doesn't contain redundant or irrelevant
information (thus ensuring that it's concise), you still need to preserve
enough of the problem so that there's no ambiguity about what you're
doing.

Pete
 
P

Paul

Warren,

I don't know much about it but the Entity framework may serve you better
than linq.

Paul said:
I think I know what you are trying to do, I cannot comment on the Linq stuff,
I understand it cause Ive created my own attribute driven data access
framework but all that select stuff I leave to the DB.

Anyway, my presumtion is you are tring to do something similar to what I
have done before.My particular case was Orders (Provisioning, cacelation etc
etc) all with similar but differing properties.

My advice is do not use an interface for anything other than behaviour, an
abstract class is much better. From what u have said your Facitiy class
should be abstract and your room and theatre classes should derive from that
class. This will stop you having to constantly redefine generic properties
and methods.

Ahhh but this is where linq fails big time if I recollect, it cannot see
inherited properties..Hmmmm..Yeah that sucks and was why I stopped reading
about Linq. My model base class I used for years would not work with it (id,
datearchived fields generic to all my tables), and i will stick to what I
know works.

This goes back to what I said previously Linq sucks with OO in mind, it just
so clearly reperesents table structure rather than the OO view of the world.

Anyway what I did with the orders was to abstract my different order type
tables with stored procedures. So for a list of all orders just pull up a
list of orders showing only the common properties (Select from orders) , then
if clicking on a order say look at the order type property in a factory to
determine which order type to load call the relevent stiored proc that links
a generic order table with the concrete order table (select from order join
concreteorder on....). I don't think you can do this with Linq.

If you can you will prolly need to create a data model that uses the linq
model as a datasource maybe entity <> LINQ <> SQL would do it. But at this
point sounds like more work than creating a custom DAL to me.

It maybe worth you looking at CSLA unless you are looking to utilise SOAP
then that has some limitations too, or biting the bullet and creating an in
house DAL/data provider framework.

Warren said:
Thanks for your help.

I think the code snippet is suffiicent (in part I have largely changed the
testing code already), but my intention maybe unclear:

The snippet is exactly the coding that is working now as :

Return type is System.Data.Linq.Table<Room> for var q=from f in db.Rooms
select f; and
System.Data.Linq.Table<IFacility> for var q=from IFacility f in db.Rooms
select f;

My intention is to create a single List of different type of objects, where
all the objects are instantiated from a single or multiple database tables,
and that the class specific functions to the objects can be invoked easily.

By converting to IFacility, I can actually create a single IList<IFacility>
that includes a list of Rooms + Theatres upcasted to IFacility (thus a
single LINQ query to work against both Rooms and Theatres). If not using
this approach, I can maintain separate list of Rooms and Theatres, but
multiple queries are required on each separate list. A last alternative
is to do an IList<objects> which I am currently exploring.

The reason of using IFacility instead of an actualy object is to hide the
details of Facility from coders.

Warren


Peter Duniho said:
[...]
private void loadRooms()
{
using (FacilitiesDBDataContext db = new
FacilitiesDBDataContext())
{
db.Log = Console.Out;
db.DeferredLoadingEnabled = false;

var q = from IFacility f in db.Rooms select f; // some
where clause ommited now
rooms = q.ToList();
}
}

It is still not possible to fully understand your use of LINQ. In
particular, what kind of collection is "db.Rooms"?

Based on naming alone, there's a strong hint that in spite of your using
the interface IFacility, in fact every "f" you get is going to be a Room.
If that's true, then the solution is obvious: quit using IFacility as the
type you get from the collection, and use Room instead.

If that's not in fact a correct understanding of your database, then
you'll need to explore some of the other options mentioned. For example,
instead of "select f", you could replace "f" with a call to a factory
method that converts the "f" instance to an appropriate Room instance (or
whatever, according to your needs).

As before, without a true concise-but-complete code sample (your previous
post contained code that was neither concise nor complete), it's not
really possible to fully comprehend the scenario. There's an art to
creating a suitable code sample, but the general rule is: it has to
compile without the addition of anything else, and while you have to "dumb
down" the code so that it doesn't contain redundant or irrelevant
information (thus ensuring that it's concise), you still need to preserve
enough of the problem so that there's no ambiguity about what you're
doing.

Pete
 
W

Warren

Thanks, Paul.

You got exactly where I am heading to. I will try to look up the Entity
Framework and see if there is a cleaner way of doing it. Also, I think the
stored procedure is also a good method and may try out further on that, at
presentI have to recode some of the generated classes to be able to use
LINQ.

The best part is, we want to make it a silverlight capable web-service, and
silverlight is not that "generous" at serializing complex types ... so a lot
of tests follows.

Regards
Warren

Paul said:
Warren,

I don't know much about it but the Entity framework may serve you better
than linq.

Paul said:
I think I know what you are trying to do, I cannot comment on the Linq
stuff,
I understand it cause Ive created my own attribute driven data access
framework but all that select stuff I leave to the DB.

Anyway, my presumtion is you are tring to do something similar to what I
have done before.My particular case was Orders (Provisioning, cacelation
etc
etc) all with similar but differing properties.

My advice is do not use an interface for anything other than behaviour,
an
abstract class is much better. From what u have said your Facitiy class
should be abstract and your room and theatre classes should derive from
that
class. This will stop you having to constantly redefine generic
properties
and methods.

Ahhh but this is where linq fails big time if I recollect, it cannot see
inherited properties..Hmmmm..Yeah that sucks and was why I stopped
reading
about Linq. My model base class I used for years would not work with it
(id,
datearchived fields generic to all my tables), and i will stick to what I
know works.

This goes back to what I said previously Linq sucks with OO in mind, it
just
so clearly reperesents table structure rather than the OO view of the
world.

Anyway what I did with the orders was to abstract my different order type
tables with stored procedures. So for a list of all orders just pull up a
list of orders showing only the common properties (Select from orders) ,
then
if clicking on a order say look at the order type property in a factory
to
determine which order type to load call the relevent stiored proc that
links
a generic order table with the concrete order table (select from order
join
concreteorder on....). I don't think you can do this with Linq.

If you can you will prolly need to create a data model that uses the linq
model as a datasource maybe entity <> LINQ <> SQL would do it. But at
this
point sounds like more work than creating a custom DAL to me.

It maybe worth you looking at CSLA unless you are looking to utilise SOAP
then that has some limitations too, or biting the bullet and creating an
in
house DAL/data provider framework.

Warren said:
Thanks for your help.

I think the code snippet is suffiicent (in part I have largely changed
the
testing code already), but my intention maybe unclear:

The snippet is exactly the coding that is working now as :

Return type is System.Data.Linq.Table<Room> for var q=from f in
db.Rooms
select f; and
System.Data.Linq.Table<IFacility> for var q=from IFacility f in
db.Rooms
select f;

My intention is to create a single List of different type of objects,
where
all the objects are instantiated from a single or multiple database
tables,
and that the class specific functions to the objects can be invoked
easily.

By converting to IFacility, I can actually create a single
IList<IFacility>
that includes a list of Rooms + Theatres upcasted to IFacility (thus a
single LINQ query to work against both Rooms and Theatres). If not
using
this approach, I can maintain separate list of Rooms and Theatres, but
multiple queries are required on each separate list. A last
alternative
is to do an IList<objects> which I am currently exploring.

The reason of using IFacility instead of an actualy object is to hide
the
details of Facility from coders.

Warren


"Peter Duniho" <[email protected]> ???gco?l¢Do¡Ps?D:blush:[email protected]...

[...]
private void loadRooms()
{
using (FacilitiesDBDataContext db = new
FacilitiesDBDataContext())
{
db.Log = Console.Out;
db.DeferredLoadingEnabled = false;

var q = from IFacility f in db.Rooms select f; //
some
where clause ommited now
rooms = q.ToList();
}
}

It is still not possible to fully understand your use of LINQ. In
particular, what kind of collection is "db.Rooms"?

Based on naming alone, there's a strong hint that in spite of your
using
the interface IFacility, in fact every "f" you get is going to be a
Room.
If that's true, then the solution is obvious: quit using IFacility as
the
type you get from the collection, and use Room instead.

If that's not in fact a correct understanding of your database, then
you'll need to explore some of the other options mentioned. For
example,
instead of "select f", you could replace "f" with a call to a factory
method that converts the "f" instance to an appropriate Room instance
(or
whatever, according to your needs).

As before, without a true concise-but-complete code sample (your
previous
post contained code that was neither concise nor complete), it's not
really possible to fully comprehend the scenario. There's an art to
creating a suitable code sample, but the general rule is: it has to
compile without the addition of anything else, and while you have to
"dumb
down" the code so that it doesn't contain redundant or irrelevant
information (thus ensuring that it's concise), you still need to
preserve
enough of the problem so that there's no ambiguity about what you're
doing.

Pete
 
W

Warren

Thanks Peter, I know that you are not judging me, just that I overwritted
some of the previous code already and the project is in a dangling state now
:D

As you said, by far I am able to create a list of mixed types, but I am
still looking for a cleaner way. The difficult part of IList<IFacility>
(originally I tried ICollection<IFacility>) is it becomes very difficult to
serialize; and that's why I reverted back to using List<IFacility>.

Warren
(In fact, I am still struggling with the serializing now. A custom
serializer is required at present ... this is harder than I thought)

Peter Duniho said:
Thanks for your help.

I think the code snippet is suffiicent

All due respect, that is not for you to judge. If others cannot
understand your question given the code you've provided so far, then your
code example is insufficient.

That said...
[...]
Return type is System.Data.Linq.Table<Room> for var q=from f in db.Rooms
select f; and
System.Data.Linq.Table<IFacility> for var q=from IFacility f in db.Rooms
select f;

This is not the static return type. Perhaps you are referring to the LINQ
implementation? Not sure. The static return type should be
IEnumerable<Room> and IEnumerable<IFacility>, maybe. The actual type of
the objects in the returned enumeration (as opposed to the type of the
enumeration itself) will be whatever happens to be the type generated in
the "select" clause. This might be "Room" in your example. Or it might
not.

Again, without a complete code sample, it's impossible to know for sure.
As long as you refuse to provide a complete code sample, these ambiguities
will persist.
My intention is to create a single List of different type of objects,
where
all the objects are instantiated from a single or multiple database
tables,
and that the class specific functions to the objects can be invoked
easily.

By converting to IFacility, I can actually create a single
IList<IFacility>
that includes a list of Rooms + Theatres upcasted to IFacility (thus a
single LINQ query to work against both Rooms and Theatres).

The type of the list isn't necessarily the type of the objects in the
list. In particular, for exactly the reason you state, you can put
instances of Room and Theatre into an IList<IFacility>, and that's fine.
But you have to start with the correct type of objects. Which you can do
if you simply create the correct type of object in your LINQ query. And
maybe you are already doing that.

If you do, then using those objects as Room and Theatre is as simple as
downcasting to the appropriate type.

Again, without a complete code sample, it's impossible to know for sure
what part of this scenario you're actually having trouble with.

Pete
 
P

Paul

No you really need your model/composite classes to be as lightweight as you
can over SOAP and seperate out any logic.

The difficulties arise when you want to identify changes to objects when
they are sent back to your BLL. I take a leaf out of the old recordset type
from VB6 and use an underlying values object that is of the same Type.

I then use a number of methods in a base class to identify the scenarios
below and also populate this underlying values using reflection, but to use
these you pass you object back to you BLL through SOAP.

public class Course:ModelBase<Course>
{...................}

public class ModelBase<T> where T : ModelBase<T>, new()
{

//Add any base level properties (ID, DateArchived) etc

/// <summary>
/// Populate the underlying values when Loading object from a
datasource.
/// Do not populate if you wish to follow with an insert.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="values"></param>
/// <param name="underlyingValues"></param>
public void PopulateUnderlyingValues()
{
Type type = typeof(T);

if (UnderlyingValues == null)
UnderlyingValues = new T();

//Get a list of public properties
PropertyInfo[] properties =
type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
}
//for each property populate the corresponding underlying value
from the object property
foreach (PropertyInfo property in properties)
{
if (property.CanWrite && property.Name != "UnderlyingValues")
{
property.SetValue(UnderlyingValues,
property.GetValue(this, null), null);
}
}
}

Use simalar reflection to create. IsArchive, IsNew, IsDirty etc. Or you
could code for each property.


Just ideas for you.....


Warren said:
Thanks, Paul.

You got exactly where I am heading to. I will try to look up the Entity
Framework and see if there is a cleaner way of doing it. Also, I think the
stored procedure is also a good method and may try out further on that, at
presentI have to recode some of the generated classes to be able to use
LINQ.

The best part is, we want to make it a silverlight capable web-service, and
silverlight is not that "generous" at serializing complex types ... so a lot
of tests follows.

Regards
Warren

Paul said:
Warren,

I don't know much about it but the Entity framework may serve you better
than linq.

Paul said:
I think I know what you are trying to do, I cannot comment on the Linq
stuff,
I understand it cause Ive created my own attribute driven data access
framework but all that select stuff I leave to the DB.

Anyway, my presumtion is you are tring to do something similar to what I
have done before.My particular case was Orders (Provisioning, cacelation
etc
etc) all with similar but differing properties.

My advice is do not use an interface for anything other than behaviour,
an
abstract class is much better. From what u have said your Facitiy class
should be abstract and your room and theatre classes should derive from
that
class. This will stop you having to constantly redefine generic
properties
and methods.

Ahhh but this is where linq fails big time if I recollect, it cannot see
inherited properties..Hmmmm..Yeah that sucks and was why I stopped
reading
about Linq. My model base class I used for years would not work with it
(id,
datearchived fields generic to all my tables), and i will stick to what I
know works.

This goes back to what I said previously Linq sucks with OO in mind, it
just
so clearly reperesents table structure rather than the OO view of the
world.

Anyway what I did with the orders was to abstract my different order type
tables with stored procedures. So for a list of all orders just pull up a
list of orders showing only the common properties (Select from orders) ,
then
if clicking on a order say look at the order type property in a factory
to
determine which order type to load call the relevent stiored proc that
links
a generic order table with the concrete order table (select from order
join
concreteorder on....). I don't think you can do this with Linq.

If you can you will prolly need to create a data model that uses the linq
model as a datasource maybe entity <> LINQ <> SQL would do it. But at
this
point sounds like more work than creating a custom DAL to me.

It maybe worth you looking at CSLA unless you are looking to utilise SOAP
then that has some limitations too, or biting the bullet and creating an
in
house DAL/data provider framework.

:

Thanks for your help.

I think the code snippet is suffiicent (in part I have largely changed
the
testing code already), but my intention maybe unclear:

The snippet is exactly the coding that is working now as :

Return type is System.Data.Linq.Table<Room> for var q=from f in
db.Rooms
select f; and
System.Data.Linq.Table<IFacility> for var q=from IFacility f in
db.Rooms
select f;

My intention is to create a single List of different type of objects,
where
all the objects are instantiated from a single or multiple database
tables,
and that the class specific functions to the objects can be invoked
easily.

By converting to IFacility, I can actually create a single
IList<IFacility>
that includes a list of Rooms + Theatres upcasted to IFacility (thus a
single LINQ query to work against both Rooms and Theatres). If not
using
this approach, I can maintain separate list of Rooms and Theatres, but
multiple queries are required on each separate list. A last
alternative
is to do an IList<objects> which I am currently exploring.

The reason of using IFacility instead of an actualy object is to hide
the
details of Facility from coders.

Warren


"Peter Duniho" <[email protected]> ???gco?l¢Do¡Ps?D:blush:[email protected]...

[...]
private void loadRooms()
{
using (FacilitiesDBDataContext db = new
FacilitiesDBDataContext())
{
db.Log = Console.Out;
db.DeferredLoadingEnabled = false;

var q = from IFacility f in db.Rooms select f; //
some
where clause ommited now
rooms = q.ToList();
}
}

It is still not possible to fully understand your use of LINQ. In
particular, what kind of collection is "db.Rooms"?

Based on naming alone, there's a strong hint that in spite of your
using
the interface IFacility, in fact every "f" you get is going to be a
Room.
If that's true, then the solution is obvious: quit using IFacility as
the
type you get from the collection, and use Room instead.

If that's not in fact a correct understanding of your database, then
you'll need to explore some of the other options mentioned. For
example,
instead of "select f", you could replace "f" with a call to a factory
method that converts the "f" instance to an appropriate Room instance
(or
whatever, according to your needs).

As before, without a true concise-but-complete code sample (your
previous
post contained code that was neither concise nor complete), it's not
really possible to fully comprehend the scenario. There's an art to
creating a suitable code sample, but the general rule is: it has to
compile without the addition of anything else, and while you have to
"dumb
down" the code so that it doesn't contain redundant or irrelevant
information (thus ensuring that it's concise), you still need to
preserve
enough of the problem so that there's no ambiguity about what you're
doing.

Pete
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top