2012-02-15 81 views
1

這裏是我的設置與Asp.Net MVC 3:autofac基類屬性爲

public abstract class BaseProvider 
{ 
    protected ICache Cache; 
} 

public interface ICache 
{ 
    void Add(string key, object data); 
    void Remove(string key); 
    ... 
} 

public class MyCache : ICache 
{ 
    private static MemoryCache cache = MemoryCache.Default; 
    void Add(string key, object data) 
    { 
    ... 
    } 
} 

public interface IEmployeeProvider 
{ 
    IEnumerable<Employee> GetEmployees(string department); 
} 

public class EmployeeProvider:BaseProvider,IEmployeeProvider 
{ 
    public IEnumerable<Employee> GetEmployees(string department) 
    { 
     **if (Cache.Get("employees_"+department)!=null)** 

    } 
} 

出演線拋出一個錯誤,指出緩存爲空。 我試圖將基類註冊爲類型,但是,我猜這是錯誤的。 我Autofac的設置是這樣的:

builder.Register(r => new EmployeeProvider()).As<IEmployeeProvider>().InstancePerHttpRequest(); 
builder.Register(r => new MyCache()).As<ICache>().InstancePerHttpRequest(); 
builder.RegisterType<BaseProvider>().PropertiesAutowired(); 

我缺少什麼?

回答

1

您應該註冊EmployeeProviderPropertiesAutowired()或掃描組件,每一個從BaseProvider繼承類。

第一次註冊忘記了PropertiesAutowired(),第三行忘記了EmployeeProvider