Extend generated factory class (EntityFactory)

Posts   
 
    
khorvat avatar
khorvat
User
Posts: 65
Joined: 17-Feb-2011
# Posted on: 02-May-2012 17:32:07   

Hi,

is there a way to extend the generated factory class without changing the Templates e.g.

I need to override a create method and return entity that is created via custom DI container etc.


public partial class AspnetUsersEntityFactory : EntityFactoryBase2<AspnetUsersEntity> 
{
...
public override IEntity2 Create(IEntityFields2 fields) {
            IEntity2 toReturn = DIContainer.Get<AspnetUsersEntity>(fields);
            // __LLBLGENPRO_USER_CODE_REGION_START CreateNewAspnetUsersUsingFields
            // __LLBLGENPRO_USER_CODE_REGION_END
            return toReturn;
        }
...
}

Can you please point me in the right direction ?

Thanks

I'm using LLBLGen 3.1

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 02-May-2012 19:53:19   

Use the "Partial Class" feature of .NET. i.e. extending a class implementation through many files.

khorvat avatar
khorvat
User
Posts: 65
Joined: 17-Feb-2011
# Posted on: 02-May-2012 20:29:54   

I can't override a method in partial class so it can't be done that way, any other suggestions?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-May-2012 07:00:45   

Here are some options:

A. Use the USER_CODE_REGION. Whatever you put in there is kept as-is after code re-generations. I think this is the easy one.

public override IEntity2 Create(IEntityFields2 fields) {
    IEntity2 toReturn = new AspnetUsersEntity(fields);
    // __LLBLGENPRO_USER_CODE_REGION_START CreateNewCustomerUsingFields
        toReturn = DIContainer.Get<AspnetUsersEntity>(fields);
    // __LLBLGENPRO_USER_CODE_REGION_END
    return toReturn;
}

B. Create an inherited version of the factory. Override the Create method:

public partial class MyCustomAspnetUserEntityFactory : AspnetUserEntityFactory
{

    public MyCustomAspnetUserEntityFactory ()
        : base()
    { }
        
    public override IEntity2 Create(IEntityFields2 fields)
    {
        IEntity2 toReturn = DIContainer.Get<AspnetUsersEntity>(fields);
        return toReturn;
    }       
}

Then you can use it to create the involved entity...:

var factory = new MyCustomAspnetUserEntityFactory ();
var aspnetUser = factory.Create();

... or you can create a custom AspnetUserEntity that inherit from the original and use the custom factory:

public class MyCustomAspNetEntity : AspnetUserEntity 
{
    protected override IEntityFactory2 CreateEntityFactory()
    {
        return EntityFactoryCache2.GetEntityFactory(typeof(MyCustomAspnetUserEntityFactory ));
    }
}
David Elizondo | LLBLGen Support Team
khorvat avatar
khorvat
User
Posts: 65
Joined: 17-Feb-2011
# Posted on: 03-May-2012 07:26:15   

Hi

I'll probably go with option B, but how can I tell Llblgen to use my factory globally?

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 03-May-2012 11:19:25   

khorvat wrote:

Hi

I'll probably go with option B, but how can I tell Llblgen to use my factory globally?

Thanks

You mean, in a 'set and forget' way? The only way for that is to alter the templates (or better, create your own copy, modify it and use that in your preset), or go for option A. If you want to fetch entity collections with your new factory, you can simply specify it in the CTor.

If you need help with setting up your own factory template with e.g. using settings to control (in the designer) whether you get your DI line or the normal code, please let us know.

Frans Bouma | Lead developer LLBLGen Pro
khorvat avatar
khorvat
User
Posts: 65
Joined: 17-Feb-2011
# Posted on: 03-May-2012 11:25:12   

Ok, I though that I would need to change the template.

Thanks again.