我正在尝试第一次设置Ninject。我有一个IRepository接口和一个Repository实现。我使用ASP.NET MVC和我试图注入像这样实现:Ninject未开火?
public class HomeController : Controller
{
[Inject] public IRepository<BlogPost> _repo { get; set; }
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
var b = new BlogPost
{
Title = "My First Blog Post!",
PostedDate = DateTime.Now,
Content = "Some text"
};
_repo.Insert(b);
return View();
}
// ... etc
}
而这里的Global.asax:
public class MvcApplication : NinjectHttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected override void OnApplicationStarted()
{
RegisterRoutes(RouteTable.Routes);
}
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel(new BaseModule());
return (kernel);
}
}
而这里的BaseModule类:
public class BaseModule : StandardModule
{
public override void Load()
{
Bind<IRepository<BlogPost>>().To<Repository<BlogPost>>();
}
}
尝试使用_repo.Insert(b)时,当我浏览到Index()动作时,我得到“对象引用未设置为对象的实例”。我要抛弃什么?
什么版本Ninject您使用的是? – 2009-08-05 02:08:08
我正在使用版本1.0 – 2009-08-05 13:19:44