2017-07-18 76 views
0

上根據控制器上的屬性解決依賴關係我試圖儘可能輕鬆地在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

  1. 的Autofac.Extras.AttributeMetadata包未在ASP.NET MVC核心

  2. 支持

    即使它受到支持,我也看不到它將如何支持包含此對象的對象的屬性檢測。

我很高興介紹一個新的IoC框架,我們並不依賴於Autofac或默認的IoC實現。

是我想要實現的可能嗎?什麼會被認爲是更好的緩存解決方案?

+0

根據[Autofac.Extras.AttributeMetadata v4.0.1發行說明](https://github.com/autofac/Autofac.Extras.AttributeMetadata/releases/tag/v4.0.1),您可以使用Autofac.Features。 .net核心支持的AttributeFilters'。 – Sakis

+0

@Sakis這是真的,但不允許你使用你自己的屬性,據我所知 - 這需要你註冊AttributedMetadataModule [在Autofac.Extras.AttributeMetadata中](https://autofac.org/apidoc /html/D6EFF2D8.htm),這不會被移植到.NET Core。 –

回答

1

我很高興介紹一個新的IoC框架,我們並不依賴於Autofac或默認的IoC實現。

我沒那麼熟悉Autofac,但我熟悉簡單的噴油器,這樣我就可以告訴你如何用簡單的注射器申請此類註冊:

var cache = new CacheService(); 
container.RegisterConditional(typeof(IBuilder), 
    Lifestyle.Transient.CreateRegistration<Builder>(
     () => new Builder(cache), 
     container), 
    c => c.Consumer.ImplementationType.GetCustomAttribute<UseCacheAttribute>() != null); 

container.RegisterConditional(typeof(IBuilder), 
    Lifestyle.Transient.CreateRegistration<Builder>(
     () => new Builder(new NullCacheService()), 
     container), 
    c => !c.Handled); 

這個註冊是有點複雜因爲您希望基於Builder的消費者更改Builder類型的依賴關係。尋找消費者消費者的'連鎖店'是簡單注射器不支持的,因爲它很容易導致不正確的行爲,特別是當中等消費者的生活方式不是短暫的時候。這是條件註冊是IBuilder而不是ICacheService

+0

這看起來像我們要實現它的方式。我目前無法驗證這項工作(我遇到了使用ASP.NET Core Identity工作的Simple Injector的crosswire方法的問題),但它看起來應該起作用。謝謝!如果我有任何問題,我會報告回來;) –

+0

在文檔中有一些關於交叉連接身份信息的信息:https://simpleinjector.org/aspnetcore#working-with-asp-net-core-identity。如果您有任何特別的問題,您可能需要發佈一個新的問題[這裏](https://github.com/simpleinjector/SimpleInjector/issues/new)或[here](https://stackoverflow.com/questions /問)。 – Steven

+0

該指南非常簡單,它並不能真正解釋交叉佈線。我無法在CrossWire的文檔中找到任何內容。這很重要,因爲我有一個自定義的實現,我真的不想連接所有的身份依賴關係......將在其他地方提問:) –