MVC Unity DI Setter 方法问题
我已经阅读了几天有关 MVC 中的 Unity 的内容,并且一直在努力使其发挥作用 - 希望你们能提供帮助。
我已经按照描述的方式设置了 Unity 这里,从广泛的谷歌搜索来看,这似乎很常见。我可能可以确认这个设置正在工作,因为在我的 Home Controller 上我可以让这个 DI 很好地工作:
public class HomeController : Controller
{
[Dependency]
public IRepository repo { get; set; }
public ActionResult Index()
{
string message = repo.GetMessage();
ViewData["OUT"] = message;
return View();
}
}
但是,我有一个类(“Contact”),由于各种原因,它也需要访问存储库方法,因此耦合,以及 DI 的要求。因此,在类中我有一个 [Dependency] 属性,如上所述。例如,我有以下代码:
public partial class Contact
{
[Dependency]
public IRepository repo { get; set; }
public string CoupledProperty
{
get
{
return repo.GetCoupledProperty();
}
}
那么,如果我尝试检索家庭控制器上已存在的联系人对象,例如:
public class HomeController : Controller
{
[Dependency]
public IRepository repo { get; set; }
public ActionResult Index()
{
Contact contact = repo.GetContact(1);
string message = contact.CoupledProperty;
ViewData["OUT"] = message;
return View();
}
}
我收到一个未实例化的对象错误,突出显示 Contact 类上 CoupledProperty 尝试执行的代码行访问存储库。当然,将 Contact 类中的 Dependency 属性替换为硬编码存储库对象可以正常工作。
谁能看到这里出了什么问题吗?
干杯,
蒂姆。
编辑:
存储库中的 GetContact(int) 方法是:
public Contact GetContact(int id)
{
return db.Contacts.SingleOrDefault(c => c.ContactID == id);
}
I have been reading for a couple of days about Unity in MVC, and I've been trying to get this to work - hoping you guys can help.
I've set up Unity in the way described here which, from extensive googling, seems common enough. I can probably confirm that this setup is working, since on my Home Controller I can get this DI to work nicely:
public class HomeController : Controller
{
[Dependency]
public IRepository repo { get; set; }
public ActionResult Index()
{
string message = repo.GetMessage();
ViewData["OUT"] = message;
return View();
}
}
However, I have a class ("Contact") which for various reasons also needs access to repository methods, hence the coupling, and the requirement for DI. So within the class I have a [Dependency] property just as above. So for example I have the following code:
public partial class Contact
{
[Dependency]
public IRepository repo { get; set; }
public string CoupledProperty
{
get
{
return repo.GetCoupledProperty();
}
}
So then if I attempt to retrieve an already existing Contact object on the Home Controller, e.g.:
public class HomeController : Controller
{
[Dependency]
public IRepository repo { get; set; }
public ActionResult Index()
{
Contact contact = repo.GetContact(1);
string message = contact.CoupledProperty;
ViewData["OUT"] = message;
return View();
}
}
I get an object not instantiated error highlighting the line of the code on the Contact class where the CoupledProperty attempts to access the repository. And of course, swapping out the Dependency property in the Contact class for a hard coded repository object works properly.
Can anyone see what is going wrong here?
Cheers,
Tim.
EDIT:
The GetContact(int) method in the repository is:
public Contact GetContact(int id)
{
return db.Contacts.SingleOrDefault(c => c.ContactID == id);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GetContact(int)
方法在您的存储库实现中是什么样子的?它使用 Unity 来生成联系人吗?我怀疑事实并非如此。What does the
GetContact(int)
method look like in your repository implementation? Does it use Unity to produce the contact? I suspect it doesn't.您可以使用 DependencyResolver 来解析它(如果您正在使用该解析器)。
You can resolve it with DependencyResolver, if thats the resolver you are using.
我建议您在所有这些代码旁边编写一些单元测试,以持续测试和验证您编写的代码。除了 StriplingWarrior 的建议之外,我还建议您检查 DI 容器接线。
I would suggest you write some unit tests alongside all this code to continuously test and validate the code you have written. Besides StriplingWarrior's suggestion, I would also recommend you to check your DI Container wireup.