Domanda

Sto cercando di eseguire alcune intercettazioni basate su attributi usando StructureMap ma sto lottando per legare gli ultimi fini sciolti.

Ho un registro personalizzato che scansiona le mie assemblee e in questo registro ho definito il seguente ITypeInterceptor il cui scopo è abbinare i tipi decorati con l'attributo dato e quindi applicare l'interceptor se abbinato. La classe è definita come tale:

public class AttributeMatchTypeInterceptor<TAttribute, TInterceptor> 
   : TypeInterceptor 
   where TAttribute : Attribute 
   where TInterceptor : IInterceptor
{
    private readonly ProxyGenerator m_proxyGeneration = new ProxyGenerator();

    public object Process(object target, IContext context)
    {
        return m_proxyGeneration.CreateInterfaceProxyWithTarget(target, ObjectFactory.GetInstance<TInterceptor>());
    }

    public bool MatchesType(Type type)
    {
        return type.GetCustomAttributes(typeof (TAttribute), true).Length > 0;
    }
}

//Usage
[Transactional]
public class OrderProcessor : IOrderProcessor{
}
...   
public class MyRegistry : Registry{
    public MyRegistry()
    {
         RegisterInterceptor(
             new AttributeMatchTypeInterceptor<TransactionalAttribute, TransactionInterceptor>());
         ...
    }
}

Sto usando DynamicProxy dal castello.core per creare gli intercettori, ma il mio problema è che l'oggetto è tornato dal CreateInterfaceProxyWithTarget (...) Call non implementa l'interfaccia che ha attivato la creazione dell'istanza target in StructureMap (cioè iorderprocessor nell'esempio sopra). Speravo che il parametro icontext rivelasse questa interfaccia, ma non riesco solo a ottenere una presa del tipo concreto (cioè il programma ordinatore nell'esempio sopra).

Sto cercando una guida su come far funzionare questo scenario, chiamando il proxygenerator per restituire un'istanza che implementa tutte le interfacce come istanza target, ottenendo l'interfaccia richiesta da StructureMap o attraverso un altro meccanismo.

È stato utile?

Soluzione

Io in realtà ha qualcosa a lavorare con una leggera avvertimento quindi mi limiterò a posto questo come la risposta. Il trucco era quello di ottenere l'interfaccia e passare che in CreateInterfaceProxyWithTarget . Il mio unico problema era che non riuscivo a trovare un modo per interrogare il IContext su quale interfaccia è stata attualmente risolvendo così ho finito per guardare la prima interfaccia sul bersaglio, che ha lavorato per me. Vedere codice seguente

public class AttributeMatchTypeInterceptor<TAttribute, TInterceptor> : 
    TypeInterceptor
    where TAttribute : Attribute 
    where TInterceptor : IInterceptor
{
    private readonly ProxyGenerator m_proxyGeneration = new ProxyGenerator();

    public object Process(object target, IContext context)
    {
        //NOTE: can't query IContext for actual interface
        Type interfaceType = target.GetType().GetInterfaces().First(); 
        return m_proxyGeneration.CreateInterfaceProxyWithTarget(
            interfaceType, 
            target, 
            ObjectFactory.GetInstance<TInterceptor>());
    }

    public bool MatchesType(Type type)
    {
        return type.GetCustomAttributes(typeof (TAttribute), true).Length > 0;
    }
}

Spero che questo aiuti qualcuno

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top