Looping through All Entity Classes

Posts   
 
    
Anson
User
Posts: 26
Joined: 26-Jul-2006
# Posted on: 03-Aug-2006 16:23:00   

How would I loop through in all the EntityClasses, in say a ForEach loop, being able to work with each Entity separately and then continue to the next. I have a need to do this and cannot figure out how to do it. I am working c#. Any help would be appreciated.

Chris

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 03-Aug-2006 20:48:05   

What exactly do you want to do to them? Is this something where you want to modify and/or extend the generated code or is this something you want to do at runtime?

If at runtime, what do you want to do to each of them?

Anson
User
Posts: 26
Joined: 26-Jul-2006
# Posted on: 03-Aug-2006 21:47:42   

WayneBrantley wrote:

What exactly do you want to do to them? Is this something where you want to modify and/or extend the generated code or is this something you want to do at runtime?

If at runtime, what do you want to do to each of them?

I have a database that I want I need to have access to all the tables in it. I need to take the each table and convert it to a specially formatted text file. I need to have access the individual field names. There about 115 tables that I need to access. I was thinking that I could run the OR mapper on the database and then process every table through the Entities Collection of some sort and then process each all records in each entity having the field names available.

I am new to using the OR mapper so maybe there is a better way. I am open to suggestions and ideas.

Thanks, chris

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 03-Aug-2006 22:45:39   

I do not think this is available at runtime.

However, your project begs to use the code generation of LLBLGen.

You can easily write a template that generates the code to create every collection and to write out your special text files.

If you can write a generic function that can write out the text file given any EntityCollection, then all you have to do is generate the couple of lines of code to create the collection and call your function.

When you generate your DAL, setup this additional template to run during generation and all the code you need will be generated - it really is a great system.

Wayne

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 04-Aug-2006 01:42:05   

Another possibility would be to use reflection. Retrieve all types which derive from EntityBase (or Entitybase2) and that will have all of your tables.

Anson
User
Posts: 26
Joined: 26-Jul-2006
# Posted on: 04-Aug-2006 03:43:35   

bclubb wrote:

Another possibility would be to use reflection. Retrieve all types which derive from EntityBase (or Entitybase2) and that will have all of your tables.

How would I do that? I don't know what reflection is.

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 04-Aug-2006 05:32:22   

I believe you could also loop through the EntityType enum and pass each to GeneralEntityFactory.Create().


        foreach (EntityType value in Enum.GetValues(typeof(EntityType)))
        {
            IEntity2 entity = GeneralEntityFactory.Create(value);
            for (int x = 0; x < entity.Fields.Count; x++)
            {
                string fieldName = entity.Fields[x].Name;
                //do something with the name
            }
        }  

That's untested code--you may have to tinker with it.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 04-Aug-2006 11:02:22   

Anson wrote:

bclubb wrote:

Another possibility would be to use reflection. Retrieve all types which derive from EntityBase (or Entitybase2) and that will have all of your tables.

How would I do that? I don't know what reflection is.

It's a .NET feature. Please consult the MSDN documentation of the .NET SDK / vs.net

Frans Bouma | Lead developer LLBLGen Pro
Anson
User
Posts: 26
Joined: 26-Jul-2006
# Posted on: 04-Aug-2006 19:08:39   

psandler wrote:

I believe you could also loop through the EntityType enum and pass each to GeneralEntityFactory.Create().


        foreach (EntityType value in Enum.GetValues(typeof(EntityType)))
        {
            IEntity2 entity = GeneralEntityFactory.Create(value);
            for (int x = 0; x < entity.Fields.Count; x++)
            {
                string fieldName = entity.Fields[x].Name;
                //do something with the name
            }
        }  

That's untested code--you may have to tinker with it.

Thanks, I will give this a try...

Chris

Anson
User
Posts: 26
Joined: 26-Jul-2006
# Posted on: 04-Aug-2006 19:09:08   

Otis wrote:

Anson wrote:

bclubb wrote:

Another possibility would be to use reflection. Retrieve all types which derive from EntityBase (or Entitybase2) and that will have all of your tables.

How would I do that? I don't know what reflection is.

It's a .NET feature. Please consult the MSDN documentation of the .NET SDK / vs.net

thanks, I found the information and will play with this.

Chris