dynamically create and save entities

Posts   
 
    
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 08-Mar-2005 08:17:24   

In my web project, I have the need to dynamically create entities and save these to the db. For example, a user can create a new page entity and a page entity can contain multiple modules (module Entities) I don't know which modules will be created for a given page. So currently when a page is being created, I loop through the modules and perform a switch statement that then creates the modules and calls save. Something like this.

foreach(PageTypeModuleSettingEntity pageType in pageTypeModules) { switch(pageType.ModuleId) { case 1: ScopeEntity scope = ScopeManager.Create(String.Empty,page.PageId,page.CompanyId); ScopeManager.Save(scope); break;

        //next case, etc
    }
}

What I'm wondering is if there is a better method than creating this long switch statement. I was thinking/hoping I could use an interface of somesort that would allow me to call save on each entity. Is this possible? Any recommendations?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 08-Mar-2005 10:52:05   

I assume you're using selfservicing, so you can use IEntity.Save(), cast the entity to IEntity and call it's save method. But you also want to create new entities? You can do that by using the GeneralEntityFactory in the EntityFactories namespace.

Frans Bouma | Lead developer LLBLGen Pro
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 08-Mar-2005 11:05:38   

Nope, using adapter. Can't find a reference to GeneralEntityFactory. Is this something that is self servicing only? Anyway, what your saying is I could create a GeneralEntityFactory which would create the entites dynamically and I could then call the adapter.save method recursively on each entity created. That would work, no need for a huge switch statement. So where is the GeneralEntityFactory located.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 08-Mar-2005 11:53:30   

erichar11 wrote:

Nope, using adapter. Can't find a reference to GeneralEntityFactory. Is this something that is self servicing only?

Yes. I gave in another thread an example of a template how to generate one: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=2465

Anyway, what your saying is I could create a GeneralEntityFactory which would create the entites dynamically and I could then call the adapter.save method recursively on each entity created. That would work, no need for a huge switch statement. So where is the GeneralEntityFactory located.

If you're saving recursively, you can also save one entity and let adapter do the recursing for you, as the entities are layed out in a graph, right?

Frans Bouma | Lead developer LLBLGen Pro
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 08-Mar-2005 18:22:44   

using Template studio, added GeneralEntityFactory to the adapter factory was a piece of cake sunglasses Couple of questions.

  1. I assume when you release the beta that this template will be overriden, so I need to just add the code back in again. Correct?

  2. So in attempting to use the GenralEntity factory in a test scenario, I did the following:

IEntity2 entity = GeneralEntityFactory.Create(EntityType.UserEntity); Console.WriteLine(entity.LLBLGenProEntityName); DataAccessAdapter adapter = new DataAccessAdapter(); adapter.SaveEntity(entity);

but the entity was not saved which leads me to believe that I may need to first cast the entity to a UserEntity, but how to do this generically in code? Also, since some of the fields for an entity maybe not nullable, I think I have run into a problem with generically creating entities. hmmm, I'm probably back to using a switch statement.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 08-Mar-2005 23:31:12   

erichar11 wrote:

using Template studio, added GeneralEntityFactory to the adapter factory was a piece of cake sunglasses Couple of questions.

  1. I assume when you release the beta that this template will be overriden, so I need to just add the code back in again. Correct?

Not if you place it in a separate template simple_smile

  1. So in attempting to use the GenralEntity factory in a test scenario, I did the following:

IEntity2 entity = GeneralEntityFactory.Create(EntityType.UserEntity); Console.WriteLine(entity.LLBLGenProEntityName); DataAccessAdapter adapter = new DataAccessAdapter(); adapter.SaveEntity(entity);

but the entity was not saved which leads me to believe that I may need to first cast the entity to a UserEntity, but how to do this generically in code? Also, since some of the fields for an entity maybe not nullable, I think I have run into a problem with generically creating entities. hmmm, I'm probably back to using a switch statement.

if an entity isn't changed, nothing is saved, and in your code snippet you don't change it, perhaps that's the reason why it doesn't get saved?

Frans Bouma | Lead developer LLBLGen Pro
erichar11
User
Posts: 268
Joined: 08-Dec-2003
# Posted on: 09-Mar-2005 17:36:14   

Not if you place it in a separate template

OK, I will place it in a seperate template. My bad. simple_smile

if an entity isn't changed, nothing is saved, and in your code snippet you don't change it, perhaps that's the reason why it doesn't get saved?

Nope, I just provided a poor example frowning However I did get it to save. The next question I have is if I do the following:

IEntity2 entity = GeneralEntityFactory.Create(EntityType.CustomerEntity);

I assume I have to cast the enity to a customer entity to get access to the customer fields in this example. If so, the original idea was to generically loop through a collection of modules associated with a page, change some of the fields and save them to a db. If I have to cast, then I need to somehow progamatically cast to get the right entity. Not sure how to do this. Not sure if I'm going down the right path here. I say this because for each enitity I have, I have to modify it (add info to it) and it appears that implementing this generically wont work.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-Mar-2005 11:40:34   

erichar11 wrote:

The next question I have is if I do the following:

IEntity2 entity = GeneralEntityFactory.Create(EntityType.CustomerEntity);

I assume I have to cast the enity to a customer entity to get access to the customer fields in this example. If so, the original idea was to generically loop through a collection of modules associated with a page, change some of the fields and save them to a db. If I have to cast, then I need to somehow progamatically cast to get the right entity. Not sure how to do this. Not sure if I'm going down the right path here. I say this because for each enitity I have, I have to modify it (add info to it) and it appears that implementing this generically wont work.

You'll always have to deal with a type somewhere. So the variable you're assiging to has a type, and C# doesn't have something like RTTI in C++ where you could dynamical cast or use other casting methods. In .NET there is no Convert(Type, object) method, as it has to have a strong type return type anyway, so you won't be helped with it.

You could, if you know the fields, use the IEntity2.Fields collection to set values, or SetNewFieldValue() to set values. This works across entity types.

Frans Bouma | Lead developer LLBLGen Pro