1

nuget の EntityFramework.Patterns パッケージを使用しようとしていますが、行き詰まっています。

質問: ninject を使用して、mvc コントローラー内に productRepo と unitOfWork を注入する方法は?

ここにチュートリアルのリンクがあります: http://efpatterns.codeplex.com/wikipage?title=Pattern%20%3a%20Use%20Repository%20and%20UOF

DbContextAdapter adapter = new DbContextAdapter(ctx);
IRepository<Product> productRepo = new Repository<Product>(adp);
IUnitOfWork unitOfWork = new UnitOfWork(adp);
4

2 に答える 2

0

それらを引数として受け取るコンストラクターをコントローラーに追加します。詳細については、コンストラクター インジェクションを検索してください。

于 2012-04-16T22:33:49.673 に答える
0
  • nuget から Ninject.MVC3 パッケージをダウンロードします。
  • AppStart フォルダーから「ninject web common」部分を削除します
  • global.asax を開き、コードを次のように変更します。

namespace OnBoardingMVC
{
    public class MvcApplication : Ninject.Web.Common.NinjectHttpApplication
    {
        protected override IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            NinjectConfig.RegisterServices(kernel);
            return kernel;
        }    
        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

次に、App_Start フォルダーに新しい NinjectConfig.cs ファイルを作成し、次のコードをクラスに追加します。

namespace OnBoardingMVC
{
    public class NinjectConfig
    {
        public static void RegisterServices(IKernel kernel)
        {
            // e.g. kernel.Load(Assembly.GetExecutingAssembly());
            kernel.Bind(typeof(IEmployeeUow))
                  .To(typeof(EmployeeUow))
                  .WithConstructorArgument("adapter", <Add new AdapterVariable here>)
            ;
        }
    }
}

次に、EmployeeUowクラスから継承するクラスを作成し、から継承する をUnitOfWork作成することもできます。コンテキスト アダプターはコンストラクターのパラメーターになり、そのコンストラクターはアダプターをクラスの基本コンストラクターにも渡します。IEmployeeUowIUnitOfWorkUnitOfWork

于 2014-01-23T15:13:29.633 に答える