ASP.NET依赖注入 [英] ASP.NET Dependency Injection

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

问题描述




我有一个属性类Userinfo,如Firstname,Connectionstring etc,它在登录时保存信息。现在,我创建了一个类报表,在实例化报表类时接受UserInfo类。



如何解决这个依赖注入问题?

谢谢

解决方案

这个问题毫无意义。 依赖注入不是一个待解决的问题,它是一种用于解决其他问题的技术,一种广为人知的设计模式。请参阅:

http://en.wikipedia.org/wiki/Dependency_injection [< a href =http://en.wikipedia.org/wiki/Dependency_injectiontarget =_ blanktitle =New Window> ^ ]。



现在,如果您解释您的问题而不是帖子中写的内容,关于UserInfo,Report等的实际问题,您从未描述过的课程,我们将很乐意尝试帮助你,但没有人会保证你会使用从属注入解决它。当你需要就某个问题提出问题时,你不应该同时指定解决这个问题的方法,这是不正确的。







全局对象的问题(在ASP.NET中讨论对象共享,一些较弱的概念会更正确)被简化为基本事实:HTTP协议是无状态,并且应用程序的所有生命周期(代码隐藏)都在于单个HTTP请求/响应。不仅全局内存对象不能在不同页面之间共享,它们不会在回发之间共享。请参阅:

http://en.wikipedia.org/wiki/Http [< a href =http://en.wikipedia.org/wiki/Httptarget =_ blanktitle =New Window> ^ ],

http://en.wikipedia.org/wiki/Stateless_protocol [ ^ ]。



这对Web编程强加了严格的限制,以及州的实施包括以下内容:

http://en.wikipedia.org/wiki/Http #HTTP_session_state [ ^ ]。



一般来说,你需要将状态存储在某个地方。实际上,如果我们讨论用户信息,最常使用的是某些数据库。数据库已经提供了事务性机制,这将使您不必担心来自不同用户的请求的同步。使用其他东西(例如,服务器端的某些文件)将完全不切实际,但原则上可能......



-SA

Hi
I have a Property Class "Userinfo" like "Firstname,Connectionstring etc" that holds Information upon login. Now, I've Create a Class "Report" that accept UserInfo class upon Instantiating Report Class.

How Can I solve this Dependency Injection thing ?
Thanks

解决方案

The question makes no sense. "Dependency injection" is not a problem to be solved, it is a technique used to solve other problems, a widely-known design pattern. Please see:
http://en.wikipedia.org/wiki/Dependency_injection[^].

Now, if you explain your problem instead of what is written in your post, the actual problem about "UserInfo", "Report", etc., you classes which you never described, we will gladly try to help you, but no one is going to guarantee you that it will be resolved using dependenfcy injection. When you need to ask a question on some problem, you should not specify the method of solving this problem at the same time, it would be incorrect.

[EDIT]

The problems of "global" objects (it would be more correctly to discuss object sharing instead, some weaker concept) in ASP.NET is reduced to the fundamental facts: HTTP protocol is stateless, and all the lifetime of the application (code behind) lies in single HTTP request/response. Not only the global memory objects cannot be shared between different pages, they are not shared between postbacks. Please see:
http://en.wikipedia.org/wiki/Http[^],
http://en.wikipedia.org/wiki/Stateless_protocol[^].

This imposes draconian limitation on the Web programming, and the approaches to implementation of states include the following:
http://en.wikipedia.org/wiki/Http#HTTP_session_state[^].

Generally, you need to store the state somewhere. In practice, if we are discussing user information, some database is most usually used. The databases already provide transactional mechanisms which will allow you not to worry about synchronization of request from different users. Using something else (for example, some files on the server side) would be utterly impractical but possible in principle…

—SA


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

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