每个接口都明确实现了吗? (涉及IoC) [英] Every interface explicitly implemented? (IoC involved)

查看:199
本文介绍了每个接口都明确实现了吗? (涉及IoC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很清楚这样的问题可能已经发布了.然而,在这种情况下,由于IoC的介入以及许多代码的出现,我在一家新公司中看到了一位同事,这引起了这个问题.

场景:

该同事构建的一种产品的代码库中,每个接口都是明确实现的.整个应用程序是通过结构图构建的,但是在某些地方使用了具体的类型并像这样

((IInterface)concreteClass).SomeMethod()

背景:

在询问有关显式实现所有接口的含义之后,这位同事向我解释说,他们最近引入了StructureMap,许多人仍然只是使用具体的类型.因此,从本质上讲,这是教育"公司人员的一种手段.

我对此事的看法:

首先,切换到StructureMap是在几年前完成的,尽管如此,这迫使人们更多地使用接口,但我认为这不是正确的方法.以我的方式看,知道具体类型的人可以看到实现,并且可以轻松实现我上面显示的内容...只需将其转换即可.明确的通信或编码约定将使此更好.如果使用IoC且没有具体的类,则显式实现接口是完全没有用的.

我也听说这确实可以使继承混乱,但不知道示例.我还看到 Jon Skeet 在上面提到的使用方式上有些气it,而不是打算使用的方式,例如IEnumerable<>和其他名称冲突.

谁能为我阐明这件事.利弊(尽管我非常偏向于不这样做,因此我在这里发表了这篇文章),尤其是为什么一个或另一个的原因.

谢谢!

我非常清楚这不是对与错,也没有真正的答案.要了解每种方法的不足,这更是一个问题.为什么在一种情况下我会在另一种情况下使用?

解决方案

这篇文章可能是一个很好的起点:

I'm very aware of the fact that such a question has probably been posted already. Yet with the involvement of IoC in this case and a lot of code I've seen a colleague in a company I'm new in made this question arise.

Scenario:

In the codebase of one product this colleague build every interface is implemented explicitly. The whole application is constructed through structure map, yet in some places the concrete types are used and cast like this

((IInterface)concreteClass).SomeMethod()

Background:

That colleague explained to me, after asking what it is about explicitly implementing all interfaces, that they introduced StructureMap recently and many people would still just use the concrete types. So in essence it's a means to "educate" people in the company.

My cents on the matter:

First of all, the switch to StructureMap has been done several years ago and while this, sort of, forces to use interfaces more, in my opinion this is not the right way. The way I see it, people who know about the concrete type can see the implementation and easily to what I showed above... just cast it. Clear communication or coding conventions would make this much better. If IoC is used and no concrete class, then explicitly implmenting an interface is completely useless.

I've also heard that this can really mess up inheritance, but don't know of an example. I've also seen Jon Skeet sort of discouraging from using it the way mentioned above, but rather the way it was intended to use, like with IEnumerable<> and other name clashes.

Can anyone shed some light on this matter for me. Pros and cons (although I'm highly biased to not doing it, hence my post here) and especially reasons why one or the other.

Thanks!

Edit: I'm very well aware that this is not a matter of right or wrong, nor is there a real answer. This is more of a question to learn to know deficits of each approach. Why would I use in one scenario one over the other approach?

解决方案

A good starting point could be this article: Why I use explicit interface implementation as a default implementation technique

Initially I wasn't to inclined on implementing interfaces explicitly but I have to admit the writer makes some very good points.

UPDATE: In comments below, the OP is stating that explicit implementation screws up inheritance as you can not override the interface implementation in a derived type. I'm not sure if I'm undestanding correctly the issue:

public interface IFooable
{
     void Foo();
     void FooAgain();
}

public class Blah: IFooable
{
     void IFooable.Foo()
     {
         Console.WriteLine("Hi from 'Blah'!");
     }

     void IFooable.FooAgain() {}
}

public class Bar: Blah, IFooable
{
     void IFooable.Foo()
     {
         Console.WriteLine("Hi from 'Bar'!");
     }
}

public static void Main()
{
   var fooList = new List<IFooable>();
   fooList.Add(new Blah());
   fooList.Add(new Bar());

   foreach (var fooable in fooList) //Outputs "Hi from 'Blah'!" / "Hi from 'Bar'!"
       fooable.Foo();

   Console.ReadLine();
}

这篇关于每个接口都明确实现了吗? (涉及IoC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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