Intercept a class that inherits from a generic

Topics: Policy Injection Application Block
Jul 28, 2008 at 5:48 PM

I've come across a situation that I think its not explicit on the pre-requisits to a class to be interceptable.

Consider the following code:

namespace DataLayer.DAOs.Security
    public interface IUserInfoDAO
    {    }   
    public class UserInfoDAO : DAO<UserInfo> ,IUserInfoDAO
    {    }

The class UserInfoDAO implements a known interface (IUserInfoDAO). However it also inherits from a Generic class, specifying the Type. The super class is as follows:
public class DAO<T> :IDAO<T>
               public DAO()
            this.database = DatabaseFactory.CreateDatabase();

        virtual public T FindById(long id)
            GenericOperator genericOperator = new GenericOperator(database);
            return genericOperator.FindById<T>(id);

So with this configuration, subclasses automatically have an implementation for FindById, T being specified in the subclass' inheritance statement.
However, when I use PolicyInjection.Wrap to wrap up an instance of UserInfoDao the following exception is thrown:

System.ArgumentException: The type UserInfoDAO is not interceptable.
Parameter name: typeToReturn
at Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjector.EnsureTypeIsInterceptable(Type typeToReturn, PolicySet policiesForThisType)
at Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjector.Wrap(Object instance, Type typeToReturn)
at Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjector.Wrap[TInterface](Object instance)

Anybody can help me? Is this a flaw on the documentation, am I doing something wrong, or have I missed something?

Thank you.

at Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Wrap[TInterface](Object instance)

Jul 28, 2008 at 11:01 PM

Extending a generic type shouldn't make a difference. What does your call to PolicyInjection.Wrap() look like? It should be something similar to:

            IUserInfoDAO dao = new UserInfoDAO();

            dao = PolicyInjection.Wrap<IUserInfoDAO>(dao);

Also, I'm assuming you have an empty IUserInfoDAO just to avoid cluttering your post. Otherwise, you wouldn't be able to do anything with the intercepted DAO objects.


Jul 30, 2008 at 11:41 AM
In fact, my call to Wrap goes like this: IUserInfoDAO authDAO = DAOFactory.Resolve<iuserinfodao>(this.state,"userInfo.default"); authDAO = PolicyInjection.Wrap<iuserinfodao>(authDAO); The problem is that I was using Wrap<userinfodao> instead of Wrap<iuserinfodao>. Now it is working. The Resolve method from DAOFactory relies on EntLib Unity to get Dependency Injection. This way it is possible to have both dependency and policy injection. Maybe this thread becomes usefull to people searching for both features. By the way, the Interface was empty to avoid useless code post, as you assumed correctly. Thank you very much. Regards, Gonçalo Luiz.</iuserinfodao></userinfodao></iuserinfodao></iuserinfodao>