TypeInterceptor в StructureMap 4.7 - PullRequest
       21

TypeInterceptor в StructureMap 4.7

0 голосов
/ 01 марта 2019

Я пытаюсь перейти на StructureMap 4.7 и заметил, что способ реализации перехватчиков изменился.Не могу понять, как изменить мой перехватчик.Вот как это было реализовано ранее.

public class StructureMapTypeInterceptor : TypeInterceptor
{
    private readonly IContainer _container;
    private readonly IAspectConfiguration _aspectConfiguration;
    private readonly ProxyGenerator _proxyGenerator;

    public StructureMapTypeInterceptor(IContainer container, IAspectConfiguration aspectConfiguration)
    {
        _container = container;
        _aspectConfiguration = aspectConfiguration;
        _proxyGenerator = new ProxyGenerator();
    }

    public object Process(object target, IContext context)
    {
        Type targetType = target.GetType();

        var instanceRef = _container.Model.AllInstances.FirstOrDefault(e => e.ConcreteType == targetType);

        if (instanceRef != null)
        {
            Type interfaceToProxy = instanceRef.PluginType;
            return _proxyGenerator.CreateInterfaceProxyWithTargetInterface(interfaceToProxy, target, new[] { (IInterceptor) new AspectInterceptor(_aspectConfiguration) });
        }

        return target;
    }
}

Вот регистрация

var aspectTypeInterceptor = new StructureMapTypeInterceptor(ObjectFactory.Container, AspectConfiguration);
ObjectFactory.Configure(c => c.RegisterInterceptor(aspectTypeInterceptor));
...