c#依赖注入 [英] c# Dependency Injection

查看:536
本文介绍了c#依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的MVC 3小项目。我基本上有三层。



第一层 - 用户界面 - 控制器,视图

第二层 - 模型

第三层 - 业务规则。



使用第一层 - 用户界面 - 我创建了对模型和业务规则的引用。



通过业务规则,我创建了一系列接口,并将这些接口应用于UI中的控制器。我使用了Ninject Dependency Injection。



简单到目前为止 - 基本的想法是有效的,但这就是我被困住的地方。



我创建了一个WCF作为一个单独的项目。 WCF在初步测试中运行得很好,但我得出的结论是,我没有在控制器中真正分离代码。



在第一层( UI)我向WCF添加了一个Web引用,并在一个简单的Controller命令中带回了一个项目列表,如下所示:



使用(WCF w =新的WCF()

..w.GetListTeams()

..查看视图.Blah blah。



它有效但是我知道我的直觉告诉我,当涉及到层的分离的真正编码时,这是错误的方法。



我正在努力通过注入将引用传递给WCF到第二层(业务规则)的想法。(Ninject依赖注入)



基本上,我可以添加对UI FROM的引用业务层,但我已经从UI添加了对业务层的引用 - 这将导致循环引用,我怀疑这是一个禁止的。



我去过寻找解决这种方法的文章,但唉......



任何人都可以了解一下我应该检讨哪些文章?



谢谢,

Glenn

I''m working on a small simple MVC 3 project. I have three layers basically.

Layer One - UI - Controllers, Views
Layer Two - Models
Layer Three - Business rules.

With Layer One - UI - I created a reference to Models and Business Rules.

With Business Rules, I created a series of Interfaces and I applied those interfaces to the Controllers in UI. I used Ninject Dependency Injection.

Simple so far - the basic idea works but this is where I''m stuck.

I created a WCF as a separate project. The WCF works just fine in preliminary tests but I came to the conclusion that I''m not doing a true separation of code in the controller.

In Layer One (UI) I added a web reference to the WCF and brought back a list of items in a simple Controller command like this:

Using (WCF w = new WCF()
..w.GetListTeams()
..flood the view. Blah blah.

It works but I know my gut is telling me that this is the "wrong approach" when it comes to true coding of separation of layers.

I''m struggling with the idea of passing the reference to the WCF to Layer Two (business rules) via Injection. (Ninject Dependency Injection)

Basically, I could add a reference to the UI FROM the business layer but I already added a reference to the Business Layer from the UI - this would cause a "circular reference" which I suspect is a no-no.

I''ve been trying to find articles to tackle this method but alas...

Can anyone shed some light on this as to what articles I should review?

Thanks,
Glenn

推荐答案

应该从表示层隐藏WCF对象,I认为。它应该在数据层中,它是一种访问数据实体对象的方式。



我已经使用了Ninject,并将其丢弃。你的控制器可以在MVC中获得由你自己的图层实例化的实体对象,这就是我从未需要的。
The WCF object should be hidden from the presentation layer, I think. It should be in the data layer, it''s a way of getting access to data entity objects.

I''ve used Ninject, and discarded it. Your controllers can get passed entity objects instantiated by your own layer within MVC, and that''s all I''ve never needed.


谢谢,Christian。



如果我们在该层创建了一个数据层并创建了一个Web引用,那么我们如何处理来自数据层的关于WCF的config.sys?



根据我的理解,我可以创建从biz层到数据层的引用?



出于好奇,你为什么要丢弃Ninject?



谢谢,

格伦
Thanks, Christian.

If we created a data layer and created a web reference in that layer, what do we do about the config.sys from the data layer regarding the WCF?

And from what I understand I can create a reference from the biz layer to the data layer?

Out of curiosity, why did you discard Ninject?

thanks,
Glenn


这篇关于c#依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆