DI and IOC examples in simple C#

Possible Duplicate: Inversion of Control < Dependency Injection Could anyone please help me to understand DI and IOC with simple C# example please? As I understand IOC is the inversion of control flow (which means nothing to me) and DI means injecting interfaces via properties or constructors. Not sure how these two are related. Thank you. I'll add an explanation that I've be

简单C#中的DI和IOC示例

可能重复: 控制反转<依赖注入 任何人都可以请帮我理解简单的C#示例的DI和IOC吗? 据我所知,IOC是控制流的倒置(这对我来说毫无意义),DI意味着通过属性或构造函数来注入接口。 不知道这两者是如何相关的。 谢谢。 我会添加一个解释,我被告知是有帮助的。 正如你正确指出的那样,依赖注入是一类通过构造函数或setter注入来要求合作者的行为。 你可以把它看作一种编码技术。 另一方面,控制反转更多的是设计

Register certificate to SSL port

I have a windows service (running as LocalSystem) that is self-hosting an OWIN service (SignalR) and needs to be accessed over SSL. I can set up the SSL binding on my local development machine just fine - and I can access my service over SSL on that same machine. However, when I go to another machine and try to run the following command I receive an error: Command: netsh http add sslcert ip

将证书注册到SSL端口

我有一个Windows服务(以LocalSystem身份运行),它是自承载OWIN服务(SignalR)并需要通过SSL访问。 我可以在本地开发机器上设置SSL绑定 - 我可以在同一台机器上通过SSL访问我的服务。 但是,当我转到另一台计算机并尝试运行以下命令时,我收到一条错误消息: 命令: netsh http add sslcert ipport=0.0.0.0:9389 appid={...guid here...} certhash=...cert hash here... 错误: SSL证书添加失败,错误:1312 指定的

Understanding Inversion of Control and Dependency Injection

I am learning the concept of IoC and DI. I checked a few blogs and below is my understanding: A tight-coupling example without the use of IoC: Public Class A { public A(int value1, int value2) { return Sum(value1, value2); } private int Sum(int a, int b) { return a+b; } } After IoC: Public Interface IOperation { int Sum(int a, int b); } Publi

了解控制和依赖注入的反转

我正在学习IoC和DI的概念。 我查了几个博客,下面是我的理解: 不使用IoC的紧耦合示例: Public Class A { public A(int value1, int value2) { return Sum(value1, value2); } private int Sum(int a, int b) { return a+b; } } IoC之后: Public Interface IOperation { int Sum(int a, int b); } Public Class A { private IOperation operation; public

What is the C# equivalent of friend?

Possible Duplicate: Why does C# not provide the C++ style 'friend' keyword? I'd like the private member variables of a class to be accessible to a Tester class without exposing them to other classes. In C++ I'd just declare the Tester class as a friend, how do I do this in C#? Can someone give me an example? There's no direct equivalent of "friend" - the clo

什么是C#相当于朋友?

可能重复: 为什么C#不提供C ++风格的“friend”关键字? 我希望一个类的私有成员变量可以被Tester类访问,而不会将它暴露给其他类。 在C ++中,我只需将Tester类声明为朋友,我如何在C#中执行此操作? 有人能给我一个例子吗? 有没有直接相当于“朋友” - 最接近可用(而不是非常接近)是InternalsVisibleTo。 我只用过这个属性进行测试 - 它非常方便! 示例:放置在AssemblyInfo.cs [assembly: InternalsVisibleTo("

What is a private interface?

In an interview a while ago for a .NET position the interviewer asked me "what would you use a private interface for?". I asked him did he mean the difference between implicit vs explicit interface implementation to which he answered no. So I'm wondering: What he meant? What you would use a private interface for? An interface could be private within another class public c

什么是私人界面?

前一段时间在面试.NET时,采访者问我“你将使用什么私人界面?”。 我问他是否意味着他回答否定的隐式与显式接口实现之间的区别。 所以我想知道: 他的意思是? 你会用什么私人界面? 一个接口可以在另一个类中是私有的 public class MyClass { private interface IFoo { int MyProp { get; } } private class Foo : IFoo { public int MyProp { get; set; } } public st

case of friend classes in C#

Consider the following code pattern: // Each foo keeps a reference to its manager class Foo { private FooManager m_manager; } // Manager keeps a list of all foos class FooManager { private List<Foo> m_foos; } Problem: there is no way to create a new Foo and update both m_foos list in the FooManager, and m_manager reference in the new Foo instance without exposing some privates pu

C#中的好友类的情况

考虑以下代码模式: // Each foo keeps a reference to its manager class Foo { private FooManager m_manager; } // Manager keeps a list of all foos class FooManager { private List<Foo> m_foos; } 问题:没有办法创建一个新的Foo并更新FooManager中的m_foos列表和新的Foo实例中的m_manager引用,而不公开公开地暴露某些私有(并且冒着有人用实际的Foos使列表异化的风险)。 例如,可以在Foo中实现一

Inheritance trees and protected constructors in C#

Given the following inheritance tree, what would be the best way of implementing it in a way that works? abstract class Foo<T> : IEnumerable<T> { public abstract Bar CreateBar(); } class Bar<T> : Foo<T> { // Bar's provide a proxy interface to Foo's and limit access nicely. // The general public shouldn't be making these though, they have access // via Cre

C#中的继承树和受保护的构造函数

鉴于以下继承树,以一种有效的方式实现它的最佳方式是什么? abstract class Foo<T> : IEnumerable<T> { public abstract Bar CreateBar(); } class Bar<T> : Foo<T> { // Bar's provide a proxy interface to Foo's and limit access nicely. // The general public shouldn't be making these though, they have access // via CreateBar() protected Bar(Foo base) {

c#

Possible Duplicate: What is so bad about Singletons? Singleton Design Pattern: Pitfalls I hear a lot of this but din't find firm reason for it. Avoid the singleton anti-pattern and replace it with DI. but, why? Stateful singletons are much more difficult to unit test. I use stateless singletons which I don't see a problem with. Since singletons can implement interfaces, they

C#

可能重复: 单身人士有什么不好? 单身设计模式:陷阱 我听到很多这样的消息,但没有找到确切的理由。 Avoid the singleton anti-pattern and replace it with DI. 但为什么? 有状态的单身人士更难以进行单元测试。 我使用无状态的单身人士,我不认为有问题。 由于singleton可以实现接口,因此可以使用依赖注入来传递它们(并且应尽可能地传递它们)

Why are singletons considered to be a bad practice?

Duplicate: What is so bad about Singletons? I was reading this question, and was surprised to see that (s)he considered a singleton to be considered a "bad practice," and in fact thought that this was common knowledge. I've used singletons quite a bit in any project that uses iBatis to load the queries from XML. It great improves speed in these instances. I'm not sure wh

为什么单身人士被认为是不好的做法?

重复: 单身人士有什么不好? 我正在阅读这个问题,并且很惊讶地看到他认为单身人士被认为是一种“不良行为”,实际上他们认为这是常识。 我在任何使用iBatis从XML加载查询的项目中都使用了很多单例。 在这些情况下它极大地提高了速度。 我不确定你为什么不在这种情况下使用它们。 所以......他们为什么不好? 它们不一定是坏的,只是被滥用和过度使用。 人们似乎莫名其妙地被这种模式所吸引,并寻找新的创造性的方法

Am I going crazy or is Math.Pow broken?

I used the base converter from here and changed it to work with ulong values, but when converting large numbers, specifically numbers higher than 16677181699666568 it was returning incorrect values. I started looking into this and discovered that Math.Pow(3, 34) returns the value 16677181699666568, when actually 3^34 is 16677181699666569. This therefore throws a spanner in the works for me. I a

我会疯了还是Math.Pow坏了?

我从这里使用了基本转换器,并将其更改为使用ulong值,但在转换大数字时,特别是数字高于16677181699666568时,它将返回不正确的值。 我开始研究这一点,发现Math.Pow(3,34)返回值为16677181699666568,实际上3 ^ 34是16677181699666569。这因此为我引发了一个扳手。 我认为这只是Pow方法中的双精度问题? 我最简单的解决方法就是创建我自己的Pow值,并获得ulong值? 如果是这样,做Pow的最快方法是什么? 我假设每次都