使用实体框架在引用的 dll 中使用存储库
我创建了一个DLL,其中包含大量身份验证和用户管理,我尝试在单独的项目(MVC 3 网站)中使用它们。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestProj.Authentication;
namespace TestSite.MVC.Areas.Admin.Controllers
{
public class TestController : Controller
{
AuthenticationRepository authrep = new AuthenticationRepository();
public ActionResult Index()
{
authrep.DeleteUser(1);
return View();
}
}
}
现在这显然行不通了,这是可以理解的。
我这里需要依赖注入吗?
在这种情况下,基本代码将如何查找?
我需要在构造函数中为引用的DLL 添加一些内容吗?
I have created a DLL that contains lots of authentication and user management that I'm trying to use in a separate project (MVC 3 Website).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestProj.Authentication;
namespace TestSite.MVC.Areas.Admin.Controllers
{
public class TestController : Controller
{
AuthenticationRepository authrep = new AuthenticationRepository();
public ActionResult Index()
{
authrep.DeleteUser(1);
return View();
}
}
}
Now this obviously does'nt work, which is understandable.
Is it dependency injection I need here?
And in that case, how would the basic code look for that?
Do I need to add something in the constructor for the referenced DLL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试像这样构造你的控制器:
为你的存储库创建一个接口。然后,您可以使用 DI 框架(例如 MVC 3 的 Ninject)将 AuthenticationRepository 的实例注入到 IAuthenticationRepository 的使用中。
https://github.com/ninject/ninject/wiki
Try structuring your controller like this:
Create an interface for your repository. You could then use a DI framework (like Ninject for MVC 3) to inject instances of AuthenticationRepository into usages of IAuthenticationRepository.
https://github.com/ninject/ninject/wiki