Extending Entity error

Posts   
 
    
vikramka
User
Posts: 42
Joined: 08-Sep-2004
# Posted on: 26-Oct-2005 05:50:20   

I am trying to extend 2 entities and am getting an error "specified cast is not valid".


                    MyPurchaseEntity p = new MyPurchaseEntity(34);
                    IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.PurchaseEntity);
                    prefetchPath.Add(MyPurchaseEntity.PrefetchPathPurchaseItems);
                    prefetchPath.Add(MyPurchaseEntity.PrefetchPathVendor);
                    using(DataAccessAdapter adapter = new DataAccessAdapter())
                    {
                        adapter.FetchEntity(p,prefetchPath);
                    }
    ***error****        decimal amount = ((MyPurchaseItemsEntity)p.PurchaseItems[0]).ItemTotal;


My extended entities are as below. Any idea where I am going wrong?


    public class MyPurchaseEntity:PurchaseEntity
    {
        public MyPurchaseEntity() : base()
        {
            base.PurchaseItems.EntityFactoryToUse = new MyPurchaseItemsEntityFactory();
        }
        public MyPurchaseEntity(System.Int32 id) : base(id)
        {
            base.PurchaseItems.EntityFactoryToUse = new MyPurchaseItemsEntityFactory();
        }
        public MyPurchaseEntity(IEntityFields2 fields) : base(fields)
        {
            base.PurchaseItems.EntityFactoryToUse = new MyPurchaseItemsEntityFactory();
        }

        [TypeContained(typeof(MyPurchaseItemsEntity))]
        public override Reqlog.DAL.HelperClasses.EntityCollection PurchaseItems
        {
            get { return base.PurchaseItems; }
        }

        private decimal GetOrderTotal() 
        {
            decimal total = 0;
            if (this.PurchaseItems.Count > 0) 
            {
                foreach (MyPurchaseItemsEntity _item in base.PurchaseItems) 
                {
                    total += _item.ItemTotal;
                }
            }
            return total;
        }
        public decimal OrderTotal
        {
            get { return this.GetOrderTotal(); }
        }
    }
    public class MyPurchaseEntityFactory: PurchaseEntityFactory
    {
        public MyPurchaseEntityFactory()
        {
        }
        public override IEntity2 Create()
        {
            return new MyPurchaseEntity();
        }
        public override IEntity2 Create(IEntityFields2 fields)
        {
            return new MyPurchaseEntity(fields);
        }
    }

    public class MyPurchaseItemsEntity : PurchaseItemsEntity
    {
        public MyPurchaseItemsEntity() : base()
        {
        }
        public MyPurchaseItemsEntity(System.Int32 purchaseId, System.Int16 itemId) : base(purchaseId,itemId)
        {
        }
        public MyPurchaseItemsEntity(IEntityFields2 fields) : base(fields)
        {
        }
        public MyPurchaseItemsEntity(PurchaseItemsValidator validator) : base(validator)
        {
        }
        public MyPurchaseItemsEntity(System.Int32 purchaseId, System.Int16 itemId, PurchaseItemsValidator validator) : base(purchaseId, itemId, validator)
        {
        }
        public MyPurchaseItemsEntity(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }

        private decimal GetItemTotal() 
        {
            //initialization
            decimal orderQty = base.TestOriginalFieldValueForNull(PurchaseItemsFieldIndex.Ordered) ? 0 : base.Ordered;
            decimal recvQty = base.TestOriginalFieldValueForNull(PurchaseItemsFieldIndex.Received) ? 0 : base.Received;
            decimal unitPrice = base.TestOriginalFieldValueForNull(PurchaseItemsFieldIndex.UnitPrice) ? 0 : base.UnitPrice;
            
            //getting the total 
            if (unitPrice == 0)
                return 0;
            if (recvQty != 0)
                return (recvQty * unitPrice);
            if (orderQty != 0)
                return (orderQty * unitPrice);
                
            return 0;
        }

        public decimal ItemTotal
        {
            get { return this.GetItemTotal(); }
        }
    }

    public class MyPurchaseItemsEntityFactory: PurchaseItemsEntityFactory
    {
        public MyPurchaseItemsEntityFactory()
        {
        }
        public override IEntity2 Create()
        {
            return new MyPurchaseItemsEntity();
        }
        public override IEntity2 Create(IEntityFields2 fields)
        {
            return new MyPurchaseItemsEntity(fields);
        }
    }


vikramka
User
Posts: 42
Joined: 08-Sep-2004
# Posted on: 26-Oct-2005 06:07:37   

Never mind.

I forgot to add this.


        public new static IPrefetchPathElement2 PrefetchPathPurchaseItems
        {
            get
            {
                return new PrefetchPathElement2(new EntityCollection(new MyPurchaseItemsEntityFactory()),
                    PurchaseEntity.Relations.PurchaseItemsEntityUsingPurchaseId, 
                    (int)EntityType.PurchaseEntity, (int)EntityType.PurchaseItemsEntity, 0, null, null, null, null, "PurchaseItems", RelationType.OneToMany);
            }
        }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 26-Oct-2005 10:33:55   

You should also check the extendedentity templates, which generate the code for you that's necessary, so you could base your templates on these simple_smile

Frans Bouma | Lead developer LLBLGen Pro
vikramka
User
Posts: 42
Joined: 08-Sep-2004
# Posted on: 26-Oct-2005 14:51:24   

Currently Im trying to add extra functionality to the entities, not really extend them. So I might as well have put in some custom code to the generated classes. But I dont like to touch the llbl code, as sometimes I just like to wipe out the code (Shift + delete) and regenerate it . This is much easier than to try and find out why some entities or relations are not working. Working with llblgen is so easy that I dont think twice about deleting any generated code and starting over.

That is why I am trying to extend the entity in my own BL layer, and avoid future problems with custom code.

Is this the right track? What would be a better way of using llblgen.

I checked the extended entity generator, that is how I realized the error of my ways. simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 26-Oct-2005 16:00:10   

Extending the entities could be done best with include templates. This way you generate the code into the generated classes without having to mess with the generated code. In 1.0.2005.1, you can now also use .lpt templates as include templates so you have full freedom what to generate.

In user code regions you can also add your code to extend the entities, which is preserved when you regenerate teh code but indeed, if you want to start over, you're out of luck.

Deriving from entities is also a way to add new code, though it comes with some issues, as inheriting from a type just to add code which should have been in the base type, isn't optimal. (it works, but it's not optimal).

Frans Bouma | Lead developer LLBLGen Pro