Inheritance thru tiers

Posts   
 
    
Posts: 5
Joined: 06-Dec-2006
# Posted on: 06-Dec-2006 01:44:27   

Hi,

I am trying to build out a business domain model using the generated DAL (adapter model). We are keen to separate out the DAL and business tier so the business domain is being implemented as a separate class library. However, in order to improve productivity, I am inheriting entity classes and simply adding methods, properties, etc as required. So, for example, one class in by business tier might be:

    public class People : PersonalEntity
    {
        public People(Int32 personId)
        {
            Id = personId;
            using (DataAccessAdapter adapter = new DataAccessAdapter(true))
            {
                adapter.FetchEntity(this);
            }
        }

        public People(): base()
        {
        }

        public static EntityCollection<People> GetPeople()
        {
            EntityCollection<People> people = new EntityCollection<People>(new PersonalEntityFactory());
            try
            {
                using (DataAccessAdapter adapter = new DataAccessAdapter(true))
                {
                    adapter.FetchEntityCollection(people, null);
                }
            }
            catch (Exception e)
            {
                Diagnostics.LogToFile(MethodInfo.GetCurrentMethod(), e);
                throw e;
            }
            return people;
        }
  }

The trouble is, when I call something like this:

                EntityCollection<People> people;
                people = People.GetPeople();

...in my presentation code, I get and error “entityToAdd isn't of the right type”. I assume this is because of the inheritance. When I change GetPeople to return EntityCollection<PersonalEntity> everything is fine (apart from the class being downcast of course).

So, I am not sure if I am asking a technical question or asking for best-practice architectural advice but either way your help would be greatly appreciated.

wink

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 06-Dec-2006 08:24:51   

At which line of code does the error/exception occur?

Instead of:

EntityCollection<People> people = new EntityCollection<People>(new PersonalEntityFactory());

Try:

EntityCollection<People> people = new EntityCollection<People>();

No need (any more) for entityFactories in the constructor of the generic collection.

Posts: 5
Joined: 06-Dec-2006
# Posted on: 06-Dec-2006 09:34:41   

Hi Walaa

This is the line that fires the exception:

    adapter.FetchEntityCollection(people, null);

I had:

            EntityCollection<People> people = new EntityCollection<People>();

...originally and that doesn’t work either confused – I was just trying something different to get the code to run.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 06-Dec-2006 09:50:19   

You also have to derive factory classes. You can automate this through a template if you want to. (similar to generating the derived classes: generate them using a different preset into a separate folder/project)

The thing is this: If you create an EntityCollection<Person>, you can add Person objects. You can't add PersonEntity objects as that would make everything type unsafe: what if you then would do: Person p = myCollection[0]; That would then break at runtime, so the code tests on this.

Because you don't derive a class from the factory classes, the factory will be creating PersonEntity instances. Which are of the wrong type, as the type to add is Person, not PersonEntity.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 5
Joined: 06-Dec-2006
# Posted on: 06-Dec-2006 10:42:06   

Ok Otis – thanks. I am only adding computed properties and methods rather than attributes so assumed an up-cast would be safe but that’s ok – I’ll try extending the factories as well. I like the idea of automating as we have about 150 entities (and still growing). How do I go about automating through a template? (I’m new to code-gens, OR-Mappers, etc as you may have guessed flushed )

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 06-Dec-2006 15:14:38   

First read the following sections in the LLBLGen Pro manual: "Concepts -> Task based code generation" "Concepts -> Templates and Template groups"

Then you should download and read the LLBLGen Pro SDK manual. (it's not that huge to read)