在使用外部框架时,如何避免过多地打开我的课程?

时间:2023-01-13 16:14:12

We have a bunch of MVC and WebAPI projects that use dependency injection to access service classes and other dependencies like that from the controllers in both frameworks. The problem I'm having is that I wanted to make all the services specific to each project internal, but I can't because the controller classes on both frameworks need to be public, which causes compilation errors if I try to receive instances of parameters that are internal types using constructor injection.

我们有一堆MVC和WebAPI项目,它们使用依赖注入来访问服务类和其他依赖项,例如来自两个框架中的控制器的依赖项。我遇到的问题是我想让每个项目内部都有特定的服务,但我不能,因为两个框架上的控制器类都需要公开,如果我尝试接收参数实例,会导致编译错误这是使用构造函数注入的内部类型。

This has bitten me more than once. It happens with almost all frameworks that call your code, be it web frameworks like WebAPI, MVC or even WebForms, and with other kinds of projects like tests, where the test classes need to be public also.

这不止一次咬我。几乎所有调用代码的框架都会发生这种情况,无论是Web框架,如WebAPI,MVC甚至WebForms,还是其他类型的项目,如测试,测试类也需要公开。

My understanding of object orientation in general is that you want to have as closed a scope as possible, so that things only have access to a minimal set of features that they need to work with. I find this a very good approach in general, because it minimizes the influence of other stuff on each class, and leads to clearer and easier to discover code.

我对面向对象的理解通常是你希望尽可能地关闭一个范围,这样事物只能访问他们需要使用的一组最小特征。我发现这是一种非常好的方法,因为它可以最大限度地减少其他内容对每个类的影响,并且可以更清晰,更容易地发现代码。

My service classes are just that. They were created to be used just by the controllers in the same assembly, and by nobody else. By having to mark them public, I'm exposing them to external callers for no reason, and I lose a lot of important compile time information by doing so, like for instance FxCop will warn me if there is no one calling a certain method from an internal class, but it can't infer that if the class is marked public, since someone else outside of the project (or even the solution) could be using that type and calling that method.

我的服务类就是这样。它们被创建为仅由同一组件中的控制器使用,而不是由其他任何人使用。通过必须将它们标记为公开,我无缘无故地将它们暴露给外部调用者,并且通过这样做我丢失了许多重要的编译时间信息,例如,如果没有人调用某个方法,FxCop会警告我一个内部类,但它不能推断如果该类被标记为public,因为项目之外的其他人(甚至解决方案)可能正在使用该类型并调用该方法。

This extends to interfaces as well obviously. As I said, we are using dependency injection (with Unity) and usually there is a interface for each service class in that scenario. Even if I can mark implementations themselves as internal, needing the interface to be public is still not ideal.

这显然扩展到接口。正如我所说,我们正在使用依赖注入(使用Unity),并且通常在该场景中为每个服务类都有一个接口。即使我可以将实现标记为内部,但需要公开的接口仍然不理想。

Is there some pattern that would allow me to cleanly define internal classes and interfaces but still work with external frameworks that need my outside facing classes to be public so they can call me? How should I proceed in general to make sure my code is as tightly scoped as possible?

是否有一些模式可以让我干净地定义内部类和接口,但仍然可以使用外部框架,需要我的外部类公开,以便他们可以打电话给我?我应该如何进行以确保我的代码尽可能严格限定范围?

1 个解决方案

#1


0  

I recommend using the facade pattern, discussed here:

我建议使用这里讨论的外观模式:

http://en.m.wikipedia.org/wiki/Facade_pattern

#1


0  

I recommend using the facade pattern, discussed here:

我建议使用这里讨论的外观模式:

http://en.m.wikipedia.org/wiki/Facade_pattern