0

我将 IOC 容器配置为添加横切关注点,但这些关注点并未添加到 AbstractFactories 构造的类中。

public static void Main(string[] args)
{
    IUnityContainer container = new UnityContainer();
    container = Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(container);
    container.RegisterType<IApplication, MyApplication>();
    container.RegisterType<IUserStoreFactory, MyUserStoreFactory>();
    container.RegisterType<IUserStore, MyUserStore>();

    var app = container.Resolve<IApplication>();
    app.Run();
}
public interface IApplication
{
    void Run();
}

public class MyApplication : IApplication
{
    private readonly IUserStoreFactory userStoreFactory;

    public MyApplication(IUserStoreFactory userStoreFactory)
    {
        Debug.Assert(null != userStoreFactory, "UserFactory is required to construct user store.");
        this.userStoreFactory = userStoreFactory;
    }

    public void Run()
    {
        Console.Write("Enter user id: ");
        var userId = Console.ReadLine();
        var userStore = this.userStoreFactory.Create(userId);

        Console.Write("Enter user name: ");
        var userName = Console.ReadLine();

        userStore.Put("name", new { Name = userName });

        var obj = userStore.Get("name");

        Console.WriteLine(obj);
    }
}

public interface IUserStoreFactory
{
    IUserStore Create(string userId);
}

public class MyUserStoreFactory : IUserStoreFactory
{
    public IUserStore Create(string userId)
    {
        return new MyUserStore(userId);
    }
}

public interface IUserStore
{
    void Put(string key, object obj);

    object Get(string key);
}

public class MyUserStore : IUserStore
{
    private readonly string userId;

    public MyUserStore(string userId)
    {
        this.userId = userId;
    }

    public void Put(string key, object obj)
    {
        // Hard coding this dependency for the sake of simplicity.
        GlobalStore.Put(this.userId + key, obj);
    }

    public object Get(string key)
    {
        // Hard coding this dependency for the sake of simplicity.
        return GlobalStore.Get(this.userId + key);
    }
}

public static class GlobalStore
{
    private static readonly Dictionary<string, object> store = new Dictionary<string, object>();

    public static void Put(string key, object obj)
    {
        store[key] = obj;
    }

    public static object Get(string key)
    {
        return store[key];
    }
}

public class LoggingInterceptionBehavior : IInterceptionBehavior
{
    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        Console.WriteLine("Before method execution : {0} | {1}", input.Target, input.MethodBase);
        IMethodReturn msg = getNext()(input, getNext);
        Console.WriteLine("After method execution : {0} | {1}", input.Target, input.MethodBase);
        return msg;
    }

    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public bool WillExecute
    {
        get
        {
            return true;
        }
    }
}

拦截器在配置文件中定义为:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

<alias alias="IApplication" type="AbstractFactoryInterceptionTest.IApplication, AbstractFactoryInterceptionTest"/>
<alias alias="IUserStore" type="AbstractFactoryInterceptionTest.IUserStore, AbstractFactoryInterceptionTest"/>
<alias alias="MyUserStore" type="AbstractFactoryInterceptionTest.MyUserStore, AbstractFactoryInterceptionTest"/>
<alias alias="LoggingInterceptionBehavior" type="AbstractFactoryInterceptionTest.LoggingInterceptionBehavior, AbstractFactoryInterceptionTest" />

<sectionExtension
   type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,  
         Microsoft.Practices.Unity.Interception.Configuration" />

<container>
  <extension type="Interception"/>
  <register type="IApplication">
    <interceptor type="InterfaceInterceptor" />
    <interceptionBehavior type="LoggingInterceptionBehavior"/>
  </register>
  <register type="IUserStore">
    <interceptor type="InterfaceInterceptor" />
    <interceptionBehavior type="LoggingInterceptionBehavior"/>
  </register>
</container>

LoggingInterceptor 已为 IApplication 和 IUserStore 定义。但是,如果您运行代码,您可以看到 IUserStore 没有被拦截(因为它没有通过 Unity 解析)。

这是上述程序的输出:

Before method execution : AbstractFactoryInterceptionTest.MyApplication | Void Run()
Enter user id: 123
Enter user name: Arnab
{ Name = Arnab }
After method execution : AbstractFactoryInterceptionTest.MyApplication | Void Run()

如何修改此代码,以便将拦截器也应用于工厂构造的对象?

4

0 回答 0