avoid duplicate code by setting a entity property

Posts   
 
    
stuarty
User
Posts: 15
Joined: 15-Dec-2008
# Posted on: 19-Jan-2009 14:52:47   

Hi

I have a load of entities that are going to do pretty much the same thing. Rather than cutting and pasting I would like to be able to pass an entity object and set a property, something like

    this.oEnitity =(MyEntity) new MyEntity();
    this.oFields =(myFields)new MyFields();

how would I go about this?

thanks Stuart

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 19-Jan-2009 15:21:47   

Are these custom properties or mapped to database fields?

stuarty
User
Posts: 15
Joined: 15-Dec-2008
# Posted on: 19-Jan-2009 15:26:21   

hi the myFields and myEntity are mapped to a table in a database cheers stuart

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 19-Jan-2009 15:33:55   

You can pass the base class (EntityBase) or the interface IEntity. From either you should define the this.oEnitity property. Also you should define the this.oFields to be of type IEntityFields.

stuarty
User
Posts: 15
Joined: 15-Dec-2008
# Posted on: 19-Jan-2009 16:01:57   

hi I am still having probems my code is


  public EntityBase oEntityBase;
        public IEntityFields oFields;

        this.oEntityBase = (MyEntity)new MyEntity();
        this.oFields = (MyFields)new MyFields();



I am getting the following errors my errors

Error 1 Cannot implicitly convert type 'business.EntityClasses.MyEntity' to 'SD.LLBLGen.Pro.ORMSupportClasses.EntityBase' Error 2 Cannot implicitly convert type 'business.HelperClasses.MyFields' to 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityFields

TIA Stuart

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Jan-2009 07:08:30   

Hi Stuart,

What LLBLGen version are you using? Using Adapter or SelfServicing? Could you please elaborate a little more your scenario/situation?

David Elizondo | LLBLGen Support Team
stuarty
User
Posts: 15
Joined: 15-Dec-2008
# Posted on: 20-Jan-2009 07:28:17   

hi I am using the lastest version of LLBLGen and adapter , I have to move some data from MySql and save to an xml file , the basic code I am using is below and it works, I need to do the same thing on maybe 20 other tables, I can just cut and past the code and manually change the adapter and field objects, but it would be nice to be able to pass the objects in somehow, so that when I need to do maintenance I change to code in one place.



            RelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(DropsFields.Mydown == 0);
            DataAccessAdapter adapter = new DataAccessAdapter();
            DataSet ds = new DataSet();
            DropsEntity oDrops = new DropsEntity();
            DataTable dt = new DataTable();
            ISortExpression sorter = new SortExpression(DropsFields.stamp | SortOperator.Ascending);
            adapter.FetchTypedList(oDrops.Fields, dt, filter, this.maxFiles,sorter,false);
            ds.Tables.Add(dt);
            string fileName = lcTarget + this.oFileStuff.createFileName(lcFileType);
            dt.WriteXml(fileName);
            EntityCollection<DropsEntity> oDropsCol = new EntityCollection<DropsEntity>();
            int i = 0;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Int64 eventid = (Int64)ds.Tables[0].Rows[i]["eventID"];
                DropsEntity oDrop = new DropsEntity(eventid);
                oDrops.Mydown = 1;
                oDropsCol.Add(oDrops);
                i = i + 1;
            }
           adapter.SaveEntityCollection(oDropsCol);


hope that all makes sense I have only just started using LLBLGen TIA Stuart

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 20-Jan-2009 09:23:38   

For Adapter you should use the (2) v ersion of these classes:

Error 1 Cannot implicitly convert type 'business.EntityClasses.MyEntity' to 'SD.LLBLGen.Pro.ORMSupportClasses.EntityBase' Error 2 Cannot implicitly convert type 'business.HelperClasses.MyFields' to 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityFields

Use EntityBase2 / IEntity2 and IEntityFields2.

But I think what you need is to dynamically create an entity, entityCollection and the corresponding fields enum, based on a string name or type, right?

Something like in here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=4594 http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=13293