DataAccessAdapter in a partial class

Posts   
 
    
simon831
User
Posts: 152
Joined: 19-Jan-2006
# Posted on: 30-Jul-2007 19:48:37   

I have a property in an ProductEntity partial class that does something like:

public partial class ProductEntity 
{
    public int Price
    {
        get
        {
            using (DataAccessAdapter a = new DataAccessAdapter())
            {
                price = 
                //get the price from other Entities 
             }
            return price;
        }
    }

This all works fine until I try to return an EntityCollection of ProductEntity via a webservice. It seems to access the Price property as it serializes the EntityCollection and it then complains that: "The ConnectionString property has not been initialized." - for the adapter object.

Whats the different between calling this as a webservice and a normal method call? Can this be made to work, or am I doing things the completely wrong way?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Jul-2007 05:50:29   

IMHO, this you should have a method GetPrice(ProductEntity product) in another BL layer. Are you doing that in such way for any special reason?

David Elizondo | LLBLGen Support Team
simon831
User
Posts: 152
Joined: 19-Jan-2006
# Posted on: 31-Jul-2007 10:48:02   

You would call BL.GetPrice() from within an Entity property?

Wouldn't this still have the same problem with the connection as I am seeing?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 31-Jul-2007 17:35:37   

simon831 wrote:

You would call BL.GetPrice() from within an Entity property?

Wouldn't this still have the same problem with the connection as I am seeing?

The property is read through reflection by the XML producer in a webservice scenario when the entity is serialized. So this reads the property and then thus runs into your dataaccessadapter. What's odd is that this is causing the connection string error as this then should always result in such an error or never result in such an error.

Could you provide a stacktrace where the error originates ?

Frans Bouma | Lead developer LLBLGen Pro
simon831
User
Posts: 152
Joined: 19-Jan-2006
# Posted on: 31-Jul-2007 17:41:43   

Otis wrote:

Could you provide a stacktrace where the error originates ?

Sorry, had to do some serious re-writing to get round this.

Whats the recommended way to do this?

I need a property on an Entity in a partial class that fetches other entities from the database in order to calculate its own value.

Is this designed to be possible?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 31-Jul-2007 18:14:00   

simon831 wrote:

Otis wrote:

Could you provide a stacktrace where the error originates ?

Sorry, had to do some serious re-writing to get round this.

Whats the recommended way to do this?

You could get away with the way you're doing it, except you're sending the entity over the wire. When that happens you're out of luck, because what will happen on the client when some code reads 'Price' ? Then the adapter is created but the database isn't available on the client (obviously).

So typically, you would do something like this: - add a private member variable _price - add a method called CalculatePrice(adapter);

In that method, you calculate the price value and set _price to that value the property Price returns the _price value.

On the service side, you call CalculatePrice and the entity's Price property will have the value it needs.

When the entity is serialized to the client, the property will have a value and everything is good, when the property is READ on the CLIENT, no database access is possible and also not needed.

The PROBLEM is simply that you use the same entity on the client and on the service and reading the property makes a difference on the service than on the client because on the client you just want to read the Price value the entity has, not the value read from the database because you don't have db access on the client.

I need a property on an Entity in a partial class that fetches other entities from the database in order to calculate its own value.

Is this designed to be possible?

It's the never ending DDD discussion: should a domain object know about repositories of related aggregate roots / domain objects?

I'd say: no. If this necessity is there, call into a static class which will do the price fetching for you and on the client this method simply returns null so you should keep the value you have.

Frans Bouma | Lead developer LLBLGen Pro
simon831
User
Posts: 152
Joined: 19-Jan-2006
# Posted on: 31-Jul-2007 19:00:07   

Very helpful. Thanks.

[quotenick="Otis"][quotenick="simon831"]

Otis wrote:

On the service side, you call CalculatePrice and the entity's Price property will have the value it needs.

Where would you call CalculatePrice? Is there some sort of Entity constructor I can override and force this method to run on creation of the object?

I think if I call CalculatePrice from within a Price property then it will only get run for the first time as the object is serialized.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 01-Aug-2007 10:12:43   

Please check the section Extending the CustomerEntityFactory in the following post: http://weblogs.asp.net/fbouma/archive/2006/06/09/LLBLGen-Pro-v2.0-with-ASP.NET-2.0.aspx

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 01-Aug-2007 10:43:45   

You can check if the entity is being deserialized by checking the property (protected) IsDeserializing. If true, the entity is being deserialized, false otherwise.

Frans Bouma | Lead developer LLBLGen Pro