'IMyInterface'不包含'MediaExists'的定义 - 没有任何意义

时间:2014-04-10 18:37:18

标签: c# asp.net-mvc umbraco

我一直在重构一些代码,将一些核心功能拆分成第二个代码库。作为其中的一部分,我将我的一个接口拆分为两个。这是以前的看法...

namespace Ministry.Ministryweb.Repositories
{
    public interface IMinistrywebPublishedContentRepository
    {
        /// <summary>
        /// Gets the media item.
        /// </summary>
        /// <param name="id">The id of the item.</param>
        /// <returns></returns>
        IPublishedContent MediaItem(int id);

        /// <summary>
        /// Gets the umbraco helper.
        /// </summary>
        UmbracoHelper Umbraco { get; }

        /// <summary>
        /// Determines if a piece of media exists.
        /// </summary>
        /// <param name="mediaId">The media id.</param>
        /// <returns></returns>
        bool MediaExists(int? mediaId);

        /// <summary>
        /// Gets the URL for some media content.
        /// </summary>
        /// <param name="mediaId">The media id.</param>
        /// <returns>A nicely formed Url.</returns>
        string MediaUrl(int? mediaId);

        /// <summary>
        /// Gets the content URL.
        /// </summary>
        /// <param name="nodeId">The node id.</param>
        /// <returns>A nicely formed Url.</returns>
        string ContentUrl(int nodeId);

        /// <summary>
        /// Returns the content at a specific node.
        /// </summary>
        /// <param name="id">The node id.</param>
        /// <returns>Dynamic content.</returns>
        dynamic Content(int? id);

        /// <summary>
        /// Gets the blog roll.
        /// </summary>
        IBlogRoll BlogRoll { get; }

        /// <summary>
        /// Gets the root ancestor.
        /// </summary>
        IPublishedContent RootAncestor { get; }

        /// <summary>
        /// Gets the consultancy URL.
        /// </summary>
        string ConsultancyUrl { get; }

        /// <summary>
        /// Gets the development URL.
        /// </summary>
        string DevelopmentUrl { get; }

        /// <summary>
        /// Gets the team management URL.
        /// </summary>
        string TeamManagementUrl { get; }

        /// <summary>
        /// Gets the name of the root ancestor.
        /// </summary>
        string RootAncestorName { get; }

        /// <summary>
        /// Gets an article with the specified ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        Article Article(int id);
    }
}

现在看来它看起来如何我将界面分成两部分。第一个界面在Ministry,Ministryweb类库中定义......

namespace Ministry.Ministryweb.Repositories
{
    public interface IMinistrywebPublishedContentRepository : IPublishedContentRepository
    {
        /// <summary>
        /// Gets the blog roll.
        /// </summary>
        IBlogRoll BlogRoll { get; }

        /// <summary>
        /// Gets the root ancestor.
        /// </summary>
        IPublishedContent RootAncestor { get; }

        /// <summary>
        /// Gets the consultancy URL.
        /// </summary>
        string ConsultancyUrl { get; }

        /// <summary>
        /// Gets the development URL.
        /// </summary>
        string DevelopmentUrl { get; }

        /// <summary>
        /// Gets the team management URL.
        /// </summary>
        string TeamManagementUrl { get; }

        /// <summary>
        /// Gets the name of the root ancestor.
        /// </summary>
        string RootAncestorName { get; }

        /// <summary>
        /// Gets an article with the specified ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        Article Article(int id);
    }
}

现在从Umbraco.Pylon中的父接口继承,我的新库...

namespace Umbraco.Pylon
{
    /// <summary>
    /// An interface for accessing published content.
    /// </summary>
    public interface IPublishedContentRepository
    {
        /// <summary>
        /// Gets the media item.
        /// </summary>
        /// <param name="id">The id of the item.</param>
        /// <returns></returns>
        IPublishedContent MediaItem(int id);

        /// <summary>
        /// Gets the umbraco helper.
        /// </summary>
        UmbracoHelper Umbraco { get; }

        /// <summary>
        /// Determines if a piece of media exists.
        /// </summary>
        /// <param name="mediaId">The media id.</param>
        /// <returns></returns>
        bool MediaExists(int? mediaId);

        /// <summary>
        /// Gets the URL for some media content.
        /// </summary>
        /// <param name="mediaId">The media id.</param>
        /// <returns>A nicely formed Url.</returns>
        string MediaUrl(int? mediaId);

        /// <summary>
        /// Gets the content URL.
        /// </summary>
        /// <param name="nodeId">The node id.</param>
        /// <returns>A nicely formed Url.</returns>
        string ContentUrl(int nodeId);

        /// <summary>
        /// Returns the content at a specific node.
        /// </summary>
        /// <param name="id">The node id.</param>
        /// <returns>Dynamic content.</returns>
        dynamic Content(int? id);
    }
}

我的主要Web项目是第三个名为Ministryweb的项目,它引用了两个库。 Ministry.Ministryweb也提到了Umbraco.Pylon。使用此设置,一切正常编译,但当我加载此视图...

@inherits MinistrywebViewPage
@{
    Layout = "_Layout.cshtml";
    ViewBag.Title = DynamicModel.Name + " - " + Ministryweb.Content.RootAncestorName;
}
@section PageTitle
{
    @DynamicModel.PrimaryHeader
}
<section id="primary">
    @if (Ministryweb.Content.MediaExists(DynamicModel.MainImage))
    {
        <div class="serviceImage">
            <img src="@Ministryweb.Content.MediaUrl(DynamicModel.MainImage)" alt="@DynamicModel.Name" />
        </div>
    }
    @DynamicModel.IntroText
    @foreach (var service in DynamicModel.Children)
    {
        <h3><a href="@service.NiceUrl()">@service.Name</a></h3>
        <p>@service.Summary</p>
        <span class="linkButton"><a href="@service.NiceUrl()">@service.TagLine</a></span>
        <br />
    }
    <div class="clearfix"></div>
</section>
<section id="asideContainer">
    @Html.Partial("_SocialSidebar")
    @Html.Partial("_LogosSidebar")
</section>
<section id="articles">
    @Html.Partial("_LatestArticleForAll")
</section>

我收到以下错误...

&#39; Ministry.Ministryweb.Repositories.IMinistrywebPublishedContentRepository&#39;不包含&#39; MediaExists&#39;

的定义

这对我来说绝对毫无意义。

最初没有从Ministryweb网站项目直接参考Umbraco.Pylon,因此我明确地将其包含在另一个帖子的建议之后,但这没有任何区别。

令人抓狂的是,如果我从IMinistrywbPublishedContentRepository界面中的IPublishedContentRepository接口复制代码,那么一切正常,ReSharper只是对我呻吟(这是正确的)。任何关于可能导致这种情况的想法都会受到欢迎,因为它停止了我在其中的重构工作。

1 个答案:

答案 0 :(得分:0)

好的 - 这是一个解决方法而不是最终的答案,所以我不会这样做,我只是觉得这可能与将来看这个的人有关。

仅在尝试通过视图访问接口的属性时才会出现此问题。我在整个代码库中使用相同的接口作为传递给各种对象的参数,它完全正常。这有点奇怪(我仍然想知道为什么会这样)。

视图通过此类访问接口,该类仅用于将内容元素公开给视图...

namespace Ministry.Ministryweb
{
    /// <summary>
    /// Elements for the site.
    /// </summary>
    public class Ministryweb
    {
        private static IContainer container;

        #region | Construction |

        /// <summary>
        /// Initializes a new instance of the <see cref="Ministryweb" /> class.
        /// </summary>
        /// <param name="umbracoHelper">The umbraco helper instance.</param>
        public Ministryweb(UmbracoHelper umbracoHelper)
        {
            Content = new MinistrywebPublishedContentRepository(umbracoHelper);
        }

        #endregion

        public IMinistrywebPublishedContentRepository Content { get; private set; }
    }
}

视图本身继承自此基类以获取类的实例...

/// <summary>
/// An abstract base class for Ministryweb views.
/// </summary>
public abstract class MinistrywebViewPage : UmbracoTemplatePage
{
    private Ministryweb ministryweb;

    /// <summary>
    /// Gets the model in a dynamic form.
    /// </summary>
    public dynamic DynamicModel { get { return CurrentPage; } }

    /// <summary>
    /// Entry point for helper functions defined at root site level.
    /// </summary>
    /// <value>
    /// The ministryweb helper object.
    /// </value>
    public Ministryweb Ministryweb
    {
        get
        {
            ministryweb = ministryweb ?? new Ministryweb(Umbraco);
            return ministryweb;
        }
    }
}

我想到了,因为这只是Views的访问器,所以绝对没有理由将Content属性定义为接口。如果相反,我这样做......

namespace Ministry.Ministryweb
{
    /// <summary>
    /// Elements for the site.
    /// </summary>
    public class Ministryweb
    {
        private static IContainer container;

        #region | Construction |

        /// <summary>
        /// Initializes a new instance of the <see cref="Ministryweb" /> class.
        /// </summary>
        /// <param name="umbracoHelper">The umbraco helper instance.</param>
        public Ministryweb(UmbracoHelper umbracoHelper)
        {
            Content = new MinistrywebPublishedContentRepository(umbracoHelper);
        }

        #endregion

        public MinistrywebPublishedContentRepository Content { get; private set; }
    }
}

......一切正常。

我希望这有助于人们。