Using Llblgens dependency injection

Posts   
 
    
Posts: 56
Joined: 08-Jun-2010
# Posted on: 13-Jul-2010 18:57:27   

Hello

I'm adding domain logic to the llblgen generated entities (llbl V3, adapter).

I'm trying to organize my logic using the stratergy pattern and was wondering if I can use your dependency injection framework to inject the stratergies? I've had a look at some of your documentation (for v2.6) and I'm somewhat confused.

For example if I add a custom property to an entity:

public IStratergy DiscountStratergy {get; set;}

Can I use your DI framework to set this when the entity is constructed?

Thanks

~Brett

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 14-Jul-2010 10:40:19   

BrettBailey wrote:

Hello

I'm adding domain logic to the llblgen generated entities (llbl V3, adapter).

I'm trying to organize my logic using the stratergy pattern and was wondering if I can use your dependency injection framework to inject the stratergies? I've had a look at some of your documentation (for v2.6) and I'm somewhat confused.

For example if I add a custom property to an entity:

public IStratergy DiscountStratergy {get; set;}

Can I use your DI framework to set this when the entity is constructed?

Yes.

On an IStrategy implementing class, add the DependencyInjectionInfo attribute, and specify that for a given entity type, the property "DiscountStrategy" has to be set with an instance of the class the attribute is located on.

You then have to make sure the type is found by the runtime of course, so either use auto-discovery or register manually, like described in the documentation.

In the manual a paragraph is included which states we didn't implement a full DI framework. That paragraph means: we didn't implement something like structuremap, we implemented DI functionality which is usable only through our own system with entities. So you can't use it for injecting stuff in other classes.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 56
Joined: 08-Jun-2010
# Posted on: 14-Jul-2010 10:42:00   

.... to provide a feature in the LLBLGen Pro framework which would make it easy to inject instances of a given type into another type, or better: to set a property on an object X to an object Y.

As I was trying to set a property on object X to an object Y I thought this was going to be the solution.

I don't think I can use a different DI framework here as the entities are instantiated by llblgen so I won't get an opportunity to call into spring?

Do you have any examples or docs on this?

~Brett

Posts: 56
Joined: 08-Jun-2010
# Posted on: 14-Jul-2010 10:43:03   

Thanks Frans, Can you point me at the right bit of documentation I cannot find it!!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 14-Jul-2010 10:45:06   

BrettBailey wrote:

.... to provide a feature in the LLBLGen Pro framework which would make it easy to inject instances of a given type into another type, or better: to set a property on an object X to an object Y.

As I was trying to set a property on object X to an object Y I thought this was going to be the solution.

I don't think I can use a different DI framework here as the entities are instantiated by llblgen so I won't get an opportunity to call into spring?

Do you have any examples or docs on this?

~Brett

Brett, I removed a couple of answers from our support team as they misinterpreted the manual. Please read my reply above.

[DependencyInjectionInfo(typeof(CustomerEntity), "DiscountStratergy", ContextType = DependencyInjectionContextType.NewInstancePerTarget, TargetNamespaceFilter = "Northwind")] public class MyStrategy : IStrategy { // .... }

this is what it takes. Now you have to make sure that CustomerEntity has a IStrategy property with a setter and that the MyStrategy class is in an assembly which is read for injectables. Please see 'Setting up and using Dependency Injection' in the manual.

So it's the same as with the concurrencypredicatefactory, authorizers etch as with your own properties. Also check the DI example on our website to get an idea how it works.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 56
Joined: 08-Jun-2010
# Posted on: 14-Jul-2010 11:06:47   

Thanks Frans thats exactly what I was hoping for.

Posts: 56
Joined: 08-Jun-2010
# Posted on: 14-Jul-2010 18:25:08   

I've been trying out the dependency injection today and it seems to be just what I'm looking for.

I've got a couple of extra questions about configuring it.

I'm trying to inject an instance of the same strategry class into several (but not all) entities.

Can you specifiy multiple entity types in the DependencyInjectionInfoAttribute or add multiple DependencyInjectionInfoAttribute onto the same class?

I've seen from the docs that you can specify EntityBase2 in the DependencyInjectionInfoAttribute to apply to all entities. If this is used to set up a "default" stratergy can it be overridden with a specific stratergy on a specific entity?

As a last note the documentation mentions a "LLBLGen Pro reference manual". I've not been able to find this for v3 so apologies if my question is answered there. You told me in an earlier thread that the v3 docs are not yet finished - is this still the case?

Thanks ~Brett

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 15-Jul-2010 05:40:00   

You can use an interface as targetType, so those entities that implements such interface will be injected with your instance type (note: you can set additional interfaces in Entity Editor in LLBLGen Designer at Output Info Tab). For instance:

Implementation of the interface in desire entities. You can set such interface on LLBLGen Designer ([urldescription="ref..."]):
public Entity CustomerEntity : IEntityStrategyBasic
{
....
}

public Entity ProductEntity : IEntityStrategyBasic
{
....
}

Then you setup the DI info in your instance classes:

[DependencyInjectionInfo(typeof(IEntityStrategyBasic), "DiscountStratergy", 
    ContextType = DependencyInjectionContextType.NewInstancePerTarget,
    TargetNamespaceFilter = "Northwind")]
public class MyStrategy1 : IStrategy
{
// ....
}

[DependencyInjectionInfo(typeof(OrderEntity), "DiscountStratergy", 
    ContextType = DependencyInjectionContextType.NewInstancePerTarget,
    TargetNamespaceFilter = "Northwind")]
public class MyStrategy2 : IStrategy
{
// ....
}

In above example, CustomerEntity and ProductEntity will be injected with MyStrategy1. However OrderEntity's DiscountStrategy property will be injected with MyStrategy2.

You can read more about that in the documentation. ReferenceManual doesn't have that examples and instructions, but can download it from LLBLGen Site -> Customer's Area.

David Elizondo | LLBLGen Support Team