我正在尝试使用Ardalis示例http://ardalis.com/introducing-the-cachedrepository-pattern
构建缓存类我收到的错误是它没有参数。我可以弄清楚如何通过顶部。
该类的缓存部分没有错误,基类工作
我在资源库类的顶部做错了什么。我如何继承错误。
感谢您的帮助。
带有问题的存储库类
public class TweetSearchCache : TweetSearch
{
// SingleUserAuthorizer auth;
public TweetSearchCache() : base //(SingleUserAuthorizer auth)
{
}
private static readonly object CacheLockObject = new object();
public override List<Search> GetTweets()
{
string cacheKey = "GetSearch";
var result = HttpRuntime.Cache[cacheKey] as List<Search>;
if (result == null)
{
lock (CacheLockObject)
{
result = HttpRuntime.Cache[cacheKey] as List<Search>;
if (result == null)
{
result = base.GetTweets().ToList();
HttpRuntime.Cache.Insert(cacheKey, result, null,
DateTime.Now.AddMinutes(2), TimeSpan.Zero);
}
}
}
return result;
}
}
基类
public class TweetSearch
{
private readonly SingleUserAuthorizer _auth;
public TweetSearch(SingleUserAuthorizer auth)
{
_auth = auth;
}
public virtual List<Search> GetTweets()
{
string hashTerm = "#searchterm";
string rejectedWords = "Searchterm";
using (var twitterCtx = new TwitterContext(_auth))
{
var queryResults = (from search in twitterCtx.Search
where search.Type == SearchType.Search &&
search.Hashtag == hashTerm ||
// search.Query == twitQuery ||
// search.WordPhrase == twitPhrase ||
search.WordNot == rejectedWords &&
search.ShowUser == true &&
search.IncludeEntities == true &&
search.Locale == "EN" &&
search.PageSize == 100
select search).ToList();
return queryResults;//.ToList();
}
}
答案 0 :(得分:1)
您应该阅读有关基本C#语法的更多信息。参数可以来自派生构造函数中的参数,也可以使用静态字段,属性或方法来构建它。第一个示例显示了派生构造函数的基本参数。第二个例子显示了静态方法的使用。
public TweetSearchCache(SingleUserAuthorizer auth) : base(auth) {
// ...
}
或者
public TweetSearchCache() : base(CreateAuth()) {
// ...
}
public static SingleUserAuthorizer CreateAuth() {
SingleUserAuthorizer createdAuth = ...
// ...
return createdAuth;
}