문제

I'm creating module for Orchard CMS - viewer widget for elements of custom ContentType.

So, I have interface for service:

public interface IReviewGrabber : IDependency 
{
    IEnumerable<ReviewPart> Reviews { get; }
}

And it's implementation:

public class ReviewGrabber : IReviewGrabber
{
    readonly IOrchardServices _orchardServices;
    readonly IEnumerable<ReviewPart> _reviews;

    public ReviewGrabber(IOrchardServices orchardServices)
    {
        _orchardServices = orchardServices;
        var temp = _orchardServices.ContentManager.Query<ReviewPart, ReviewRecord>();
        _reviews = temp.List();
    }

    public IEnumerable<ReviewPart> Reviews
    {
        get { return _reviews; }
    }
}

And handler for ContentType:

public class ReviewHandler : ContentHandler
{
    public ReviewHandler(IRepository<ReviewRecord> repository)
    {
        Filters.Add(StorageFilter.For(repository));
    }
}

Debugging this, I've added element of type Review manually, and it works. In ReviewGrabber class variable temp fills correctly, but calling method List() for it fires error:

ValueFactory tries to access the Value property of this instance.

_handlers.Value '_handlers.Value' threw an exception of type 'System.InvalidOperationException' System.Collections.Generic.IEnumerable {System.InvalidOperationException}

on row

public IEnumerable<IContentHandler> Handlers {
     get { return _handlers.Value; }
}

in DefaultContentManager.cs

What's wrong? Thank you!

도움이 되었습니까?

해결책

For starters, doing a query in a constructor is a big no-no. Constructors should only do trivial work such as initialize variables, not query databases.

Property getters, similarly, should usually not do a lot of work. You should be using a method, GetReviews, that does the querying as needed. Caching can be added later if necessary.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top