Injecting an instance (of a custom class) into a TypedList

Posts   
 
    
sherman
User
Posts: 9
Joined: 31-Jul-2007
# Posted on: 10-Mar-2008 13:01:08   

I want to inject (via LLBGen's DI mechanism) a "filter" (my custom class) into a TypedList. Is it possible?

Is this one the correct way?:

Sample TypedList

    public partial class MySampleTypedList
    {
            ...         
        private Filter _filterToUse;
        public Filter FilterToUse
        {
            get { return filterToUse; }
            set { filterToUse = value; }
        }
        ...
    }

Class to inject

    [DependencyInjectionInfo(typeof(TypedListBase2), "FilterToUse")]
    [Serializable]
    public class Filter
    {
        public Filter()
        {
        }
        ...     
    }

        ...
        MySampleTypedList sampleTypedList = new MySampleTypedList();
        DependencyInjectionInfoProviderSingleton.PerformDependencyInjection(sampleTypedList);
        
        DataAccessAdapter adapter = new DataAccessAdapter();
        adapter.FetchTypedList(sampleTypedList);
        ...

Always I get an System.TypeInitializationException "The type initializer for 'SD.LLBLGen.Pro.ORMSupportClasses.DependencyInjectionInfoProviderSingleton' threw an exception." in DependencyInjectionInfoProviderSingleton.PerformDependencyInjection(sampleTypedList);

LLBLGen Pro 2.5 (August 27th, 2007) Adapter / .NET 2.0

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Mar-2008 02:12:13   

Hi sherman, accordingly to the Manual, you have to perform the injection at the constructor of the class. This is copied from LLBLGenPro Help - Concepts - Dependency Injection and Inversion of Control:

The DI mechanism in LLBLGen Pro is used for injecting objects into entities: entity classes are prepared to get the objects they depend on injected by the DI mechanism, other classes are not. This doesn't mean you can't enable these classes to use the DI mechanism, because you can. All you have to do is call the DependencyInjectionProvider to inject the objects the instance relies on, from the constructor of the class. Do this with the following code: (For entities, you don't have to do anything, it's been taken care of for you. Only use the following code if you want to use the LLBLGen Pro DI mechanism to inject objects into classes not yet prepared for DI).

C#
DependencyInjectionInfoProviderSingleton.PerformDependencyInjection(this);

After using this line of code in the constructor of your class, you can use code like:

C#
MyClass c = new MyClass();

and c will get all objects to inject into an instance of MyClass injected. For entities, you don't have to do anything, it's been taken care of for you.

If you still have troubles, please post the whole exception stack trace.

Cheers.

David Elizondo | LLBLGen Support Team
sherman
User
Posts: 9
Joined: 31-Jul-2007
# Posted on: 11-Mar-2008 09:39:32   

Forget it, I've overlooked that phrase ("from the constructor of the class") flushed

Thanks for the quick reply!