上根据控制器上的属性解决依赖关系我试图尽可能轻松地在ASP.NET Core MVC
项目的数据访问层中实现缓存。主要问题是我们不希望从所有页面上的缓存中读取,只有一部分。下面的例子应该说明了这种设置,我们有:如何在ASP.NET Core MVC
[UseCache]
public class ControllerA : Controller
{
public ControllerA(IBuilder builder)
{
// Should resolve an IBuilder with a CacheService
}
}
public class ControllerB : Controller
{
public ControllerB(IBuilder builder)
{
// Should resolve an IBuilder with a NullCacheService
}
}
public class Builder : IBuilder
{
public Builder(ICacheService cacheService)
{
// The type of the resolved ICacheService depends on the UseCache
// attribute on any of the object that depends on this IBuilder
}
}
public class CacheService : ICacheService
{
public Object Get(string key, Func<Object> getValue)
{
// Check if the value is cached against Key and return it if it's not
// Obviously needs a lot more here regarding caching timeframes, expiry etc
}
}
public class NullCacheService : ICacheService
{
public Object Get(string key, Func<Object> getValue)
{
// Don't do anything with key, just do the work in getValue and return it
}
}
public class UseCacheAttribute : Attribute
{
}
我知道Autofac can deal with resolving dependencies using attributes但
的Autofac.Extras.AttributeMetadata包未在ASP.NET MVC核心
- 支持
即使它受到支持,我也看不到它将如何支持包含此对象的对象的属性检测。
我很高兴介绍一个新的IoC框架,我们并不依赖于Autofac或默认的IoC实现。
是我想要实现的可能吗?什么会被认为是更好的缓存解决方案?
根据[Autofac.Extras.AttributeMetadata v4.0.1发行说明](https://github.com/autofac/Autofac.Extras.AttributeMetadata/releases/tag/v4.0.1),您可以使用Autofac.Features。 .net核心支持的AttributeFilters'。 – Sakis
@Sakis这是真的,但不允许你使用你自己的属性,据我所知 - 这需要你注册AttributedMetadataModule [在Autofac.Extras.AttributeMetadata中](https://autofac.org/apidoc /html/D6EFF2D8.htm),这不会被移植到.NET Core。 –