[英]How to switch classes with same name and different namespaces?
I have read the blog post Repository Design Pattern 我已经阅读了博客文章Repository Design Pattern
The main idea of the article is to cast classes with same name to get more functionality. 本文的主要思想是强制转换具有相同名称的类以获得更多功能。 It's ok, I understand it.
没关系,我了解。 But i can't understand this code:
但是我听不懂这段代码:
repository.query(new NewestNewsesSpecification());
He has NewestNewsesSpecification
for sql and NewestNewsesSpecification
for Realm. 他
NewestNewsesSpecification
适用于SQL和NewestNewsesSpecification
的境界。
So my question - how the program can understand what implementation of NewestNewsesSpecification
to use, if classes have the same name but in different namespaces? 所以我的问题是-如果类具有相同的名称但在不同的命名空间中,那么程序如何理解要使用的
NewestNewsesSpecification
实现? It's not even a DI framework. 它甚至不是DI框架。 Or is it just that every time I need different storage I need to find and change namespaces in source code files?
还是仅仅是每当我需要不同的存储时,我都需要在源代码文件中查找和更改名称空间?
The code of article is in Java. 本文的代码使用Java。 I'm writing in C#.
我正在用C#编写。
I just read the article very quickly. 我只是很快阅读了这篇文章。 He is explaining the Repository Pattern.
他正在解释存储库模式。 This helps to encapsulate implementation details of the actual used repository.
这有助于封装实际使用的存储库的实现细节。 This way you can swap underlying repositories without modifying your client code.
这样,您可以交换基础存储库,而无需修改客户端代码。 The underlying details are encapsulated in the
Specification
class. 基础细节封装在
Specification
类中。 You have to explicitly pass an implementation of this base class to the repository eg into the 'query' method. 您必须将该基类的实现显式传递给存储库,例如传递给“ query”方法。 This is the point where you the client have to decide which repository to use (or to query) by creating the appropriate instance.
这是客户端必须通过创建适当的实例来决定使用(或查询)哪个存储库的关键。
If you have multiple implementations sharing the same name then they are required to reside in a different namespace to avoid ambiguities. 如果您有多个共享相同名称的实现,则它们必须位于不同的名称空间中以避免歧义。 You are right if you assume that the compiler can't resolve this colliding references.
如果您认为编译器无法解析此冲突引用,那您是对的。 Usually you have to fully qualify the type (eg instantiation, cast): 'new Sql.NewestNewsesSpecification()' or use the compiler directive 'using' or 'import' to declare the appropriate namespace so that the short form can be used: 'new NewestNewsesSpecification()'.
通常,您必须完全限定类型(例如,实例化,强制转换):'new Sql.NewestNewsesSpecification()'或使用编译器指令'using'或'import'声明适当的名称空间,以便可以使用缩写形式:' new NewestNewsesSpecification()”。 The article assumes that you include either 'Sql' namespace or the 'Realm' namespace.
本文假定您包括“ Sql”名称空间或“ Realm”名称空间。 Main idea was, my guess, to show which parts of code will change when replacing the underlying repository (the context).
我的主要想法是,显示替换基础存储库(上下文)时,哪些部分代码会更改。 If you include both namespaces simultaneously, you can specify an alias for one of the implementations or use fully qualified references so that the references are not ambiguous any more.
如果同时包含两个名称空间,则可以为其中一种实现指定别名,或使用完全限定的引用,以使这些引用不再歧义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.