我正在使用对性能有很高要求的互联网应用程序,这意味着良好的缓存功能对我们的成功至关重要。
该解决方案使用 Entity Framework Code First 进行数据库访问,使用 Postsharp 进行缓存。目前,模型看起来如下所示。
public class Article
{
private readonly IProducerOperator _producerOperator;
public Article(IProducerOperator operator)
{ _producerOperator = operator; }
public int Id { get; set; }
...
public int ProducerId { get; set; }
public Producer Producer {
get { return _producerOperator.GetProducer(ProducerId); }
}
}
操作类如下所示。
public class ArticleOperations : IArticleOperations
{
private readonly IDataContext _context;
public ArticleOperations(IDataContext context)
{ _context = context; }
[Cache]
public Article GetArticle(int id)
{
var article = _context.Article.Find(id);
return article;
}
}
public class ProducerOperations : IProducerOperations
{
private readonly IDataContext _context;
public ProducerOperations(IDataContext context)
{ _context = context; }
[Cache]
public Producer GetProducer(int id)
{
var producer = _context.Producer.Find(id);
return producer;
}
}
我不喜欢在业务对象中有依赖关系,但它的论点是从缓存中延迟加载......最多。此解决方案还意味着缓存仅对生产者... at 进行一次GetProducer
。通常我什至不会考虑在那里有依赖关系。对象应该是 POCO,仅此而已。我真的需要一些新的投入。我该怎么做呢?这是最好的方法吗?
我们还需要解决相反的问题,即,从缓存的生产者中,我们应该能够检索其所有文章。