Inheritance and seperate assemblies for BL and DAL

Posts   
 
    
phillipsm
User
Posts: 3
Joined: 28-Aug-2007
# Posted on: 28-Aug-2007 23:39:11   

Hi,

I have some class library assemblies that comprise the (LLBLGen Pro generated) Adapter style Entity layer of an object model. As an example, this layer contains a class called BaseEvent.

I would like to have the entity assemblies ref'd from another assembly thereby allowing further inheritance of some of the classes in the Entity Layer, specifically the class BaseEvent, to allow me to create a class called MyEvent. I want this new class to be persistant so that the framework understands it is part of an inheritance hierarchy (single table by the way)

Now, I understand that factory classes are used for the creation of the types in the Entity Layer, however this layer is unaware of MyEvent and so a different/extended factory pattern is required in order to allow the framework to deal with MyEvent. My guess is this would happen in my new assembly containing MyEvent, and cannot happen in the entity layer as it knows nothing of the new type, but I am at a loss as to what I need to provide to make it happen.

Anyone have any ideas, or am I completely off track?

jb_ke
User
Posts: 27
Joined: 12-Jun-2007
# Posted on: 29-Aug-2007 01:33:37   

P,

I'm not sure if I understood you correctly, but asuming I am, I do the same thing constantly. For example when I have a user entity that eather needs to get inherited in a next tier because it needs extending or needs some extra, non db related functionality, I just inherit from the generated entityclass and create a new factory class for this newly inherited entity.

I do this as alot of times where the generated entity class needs some extra properties/methods wothever that arent db related, like when you have a usertype enum mapped with a typeconvertor in your generated entity but would like the consumer of your code to have a property 'IsPhysicalPerson'. This new property woudn't be db related as that data is already available in the generated class but as an enumeration due to the typeconvertor use. The new property would then just return a bool based on the base entitie's usertype field.

The following is an inherited class from a generated base entity + its own factory class


namespace C4.Epm.Core.Energy.EntityClasses
{
    public class UserEntity : UserEnergyEntity, IEnergyUser
    {
        #region Constructors

        public UserEntity()
        {
        }

        public UserEntity(Guid id) : base(id)
        {
        }

        public UserEntity(IEntityFields2 fields) : base(fields)
        {
        }

        #endregion

        /// <summary>Creates a new instance of the factory related to this entity</summary>
        protected override IEntityFactory2 CreateEntityFactory()
        {
            return new UserEnergyFactory();
        }

        #region Extended properties

        /// <summary>
        /// The sector type of the user.
        /// </summary>
        /// <remarks>
        /// Will return a textual representation of the <see cref="UserSectorTypeEnum"/>.
        /// </remarks>
        public string SectorName
        {
            get { return (Enum.GetName(typeof (UserSectorTypeEnum), Sector)); }
        }

        #endregion
    }
}

The factory class:


using C4.Epm.Llbl.EntityClasses;
using C4.Epm.Llbl.FactoryClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace C4.Epm.Core.Energy.FactoryClasses
{
    internal class UserEnergyFactory : UserEntityFactory
    {
        /// <summary>returns the name of the entity this factory is for, "UserEnergy"</summary>
        public override string ForEntityName
        {
            get { return "UserEntity"; }
        }

        public override IEntity2 Create(IEntityFields2 fields)
        {
            return new UserEnergyEntity(fields);
        }
    }
}

Hope its wot your looking for.

Regards, Carl

phillipsm
User
Posts: 3
Joined: 28-Aug-2007
# Posted on: 29-Aug-2007 02:58:28   

Thanks Carl, great response.

Whilst the code allows persistence of the derived class, I need to extend the factory class (I think) to allow for a specific discriminator value to identify the derived type both when saving the instance of the derived type and when loading it from the database.

So just to clarify, the inheritance mechanism I use employs one table with a discriminator column which is a string. In LLBLGen I define a class called BaseEvent which has a discriminator value of BaseEventDesc.

In a second assembly, I've implemented the code you specified but my new class definition (and factory) inherits from the classes specific to BaseEvent. I need to be able to specifiy a descriminator (like MyDerivedClassDesc) somewhere that informs the framework that when it returns a record from the database that has this descriminator value it should return an instance of the derived type and not the base type.

May be I need to override some of the Hierarchy functions in the factory?

Thanks,

Mark

phillipsm
User
Posts: 3
Joined: 28-Aug-2007
# Posted on: 30-Aug-2007 04:44:34   

Would I possibly have to extend the InheritanceInfoProvider? I ask as it has EntityInfo added in an initialisation method that informs about the supertype, subtype, and entity factory to use for creation of the type.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 04-Sep-2007 11:20:01   

It will be a bit of a mess I think. The main reason is relations to the new type. The inheritance provider knows which types are in a hierarchy, and you want to add a new type to that hierarchy via new code in another assembly... that's not really going to happen as the code is static code, called by the static ctor.

As it's just generated code, why not add the new type to the project you already have (perhaps create a copy of the .lgp) and re-generate the code?

Frans Bouma | Lead developer LLBLGen Pro