1

このサイトにこのような同様のトピックがいくつかあることは知っていますが、うまくいく解決策を見つけることができません。「SportsStore」というソリューションがあり、3つのプロジェクトが含まれており、すべてが完全な.NET4フレームワークを使用しています。プロジェクトの名前は、「SportsStore.Domain」、「SportsStore.UnitTests」、および「SportsStore.WebUI」です。

'SportsStore.WebUI'プロジェクト内に、'Infrastructure'というフォルダーを作成し、その中に'NinjectControllerFactory.cs'というクラスがあります。その完全なコードは次のとおりです。上部の最後の「using」ステートメントに注意してください:「usingSportsStore.Domain.Abstract」。プログラムがコンパイルされず、名前空間が存在しないことが通知されます。Intellisenseは「SportsStore」を認識しますが、「WebUI」が私の次のオプションであるとだけ言います。'SportStore.Domain'はまったく認識されません。クリーニング、再構築、閉じる、開く、再起動、すべてのプロジェクトのフレームワークをクライアントに戻し、次にフルに戻すことを試みましたが、何も機能しないようです。

結論として、「SportsStore.Domain」プロジェクトのSportsStore.Domain.Abstract名前空間の一部であるIProductRepository.csリポジトリファイルにアクセスしようとしています。

これが簡単に修正できることを願っていますか?前もって感謝します!

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using System.Linq;
using Ninject;
using Moq;
using SportsStore.Domain.Abstract;

//To begin a project that uses Ninject and/or Moq (mocking data) you
//need to add reference to them.  Easiest way is to select 'View', then
//'Other Windows', and 'Package Manager Console'.  Enter the commands:
//Install-Package Ninject -Project SportsStore.WebUI
//Install-Package Ninject -Project SportsStore.UnitTests
//Install-Package Moq -Project SportsStore.UnitTests

//We placed this file in a new folder called 'Infrastructure' within the 
//'SportsStore.WebUI' project.  This is a standard way to define what we
//need to do with Ninject since we are going to use Ninject to create our
//MVC application controllers and handle the dependency injection (DI).
//To do this, we need to create a new class and make a configuration
//change.

//Finally we need to tell MVC that we want to use this class to create
//controller objects, which we do by adding a statement to the 
//'Global.asax.cs' file in this 'SportsStore.WebUI' project.

namespace SportsStore.WebUI.Infrastructure
{
public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;

    public NinjectControllerFactory()
    {
        ninjectKernel = new StandardKernel();
        AddBindings();
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return controllerType == null
            ? null
            : (IController)ninjectKernel.Get(controllerType);
    }

    private void AddBindings()
    {
        //During development, you may not want to hook your IProductRepository to
        //live data yet, so here we can create a mock implementation of the data
        //and bind it to the repository.  This is MOQ in work - great tool to allow
        //you to develop real code and make it think it's using the live data.  This
        //uses the 'System.Linq' namespace
        Mock<IProductRepository> mock = new Mock<IProductRepository>();
        mock.Setup(m => m.Products).Returns(new List<Product>
        {
            new Product { Name = "Football", Price = 25 },
            new Product { Name = "Surf board", Price = 179 },
            new Product { Name = "Running shoes", Price = 95 }
        }.AsQueryable());
        ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
    }
 }
}
4

1 に答える 1

5

クラスが別のアセンブリで宣言された型にアクセスするには、それらを参照する必要があります。Ninjectを使用している場合(おそらく)、これはNinjectアセンブリに対して行っています。

Domainはあなたのアセンブリですが、同じソリューションで、で参照する必要があります。WebUIそうしないと、お互いを知ることができません。

だから、確認しましょう:

  1. WebUIプロジェクトを右クリックします
  2. [参照の追加]を選択します
  3. [プロジェクト]タブに移動します
  4. Domainプロジェクトを選択
  5. 終わり!

再構築すれば、準備完了です。

よろしく

于 2012-07-19T21:13:56.860 に答える