(นี่เป็นคำถามที่เกี่ยวข้องกับคำถามนี้ ซึ่งใช้สำหรับ SimpleInjector ฉันเป็น แนะนำให้สร้างคำถามแยกกันสำหรับแต่ละคอนเทนเนอร์ IoC)
ด้วย Unity ฉันสามารถเพิ่มการสกัดกั้นตามคุณลักษณะเช่นนี้ได้อย่างรวดเร็ว
public sealed class MyCacheAttribute : HandlerAttribute, ICallHandler
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return this;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
// grab from cache if I have it, otherwise call the intended method call..
}
}
จากนั้นฉันลงทะเบียนกับ Unity ด้วยวิธีนี้:
container.RegisterType<IPlanRepository, PlanRepository>(new ContainerControlledLifetimeManager(),
new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
ในรหัสพื้นที่เก็บข้อมูลของฉัน ฉันสามารถเลือกตกแต่งวิธีการบางอย่างที่จะแคชได้ (ด้วยค่าแอตทริบิวต์ที่สามารถปรับแต่งแยกกันสำหรับแต่ละวิธี):
[MyCache( Minutes = 5, CacheType = CacheType.Memory, Order = 100)]
public virtual PlanInfo GetPlan(int id)
{
// call data store to get this plan;
}
ฉันกำลังสำรวจวิธีการที่คล้ายกันใน Autofac จากสิ่งที่ฉันอ่านและค้นหาดูเหมือนว่ามีเพียงการสกัดกั้นระดับอินเทอร์เฟซ/ประเภทเท่านั้น แต่ฉันชอบตัวเลือกในการตกแต่งวิธีการแต่ละวิธีด้วยพฤติกรรมการสกัดกั้นที่ควบคุมคุณลักษณะประเภทนี้ มีคำแนะนำอะไรบ้าง?