To answer your question, Travis, you should read up a bit on the Repository Pattern. This pattern is used to provide a number of advantages, including:
It centralizes the data logic or Web service access logic.
It provides a substitution point for the unit tests.
It provides a flexible architecture that can be adapted as the overall design of the application evolves.
As to your question: "Is this a common practice?" the answer is, "It should be." but, unfortunately, I don't see it as much as I would prefer.
Now, in your example, you show the repository being created within the context of your controller class. (Please note that this is NOT the same thing as an object being made "global" as you put it. Global means that the object is accessible from any scope. Read more about it here at Wikipedia.)
In any case, one of the advantages is that the repository allows you to change between how you are accessing your data (or even where your data is being accessed) by using the correct, concrete version of the repository. So, you may have:
IRepository - Your interface which the concrete repositories implement.
DatabaseRepository - Accesses your data in a database.
FlatFileRepository - Access your data out of a flat file.
etc.
If you wish to switch to other data sources, it is a simple as swapping out the concrete implementation in your controller. (Please note, there are more advanced and flexible ways of doing this through dependency injection, but that is out of scope of this question, although it does/can play heavily in the repository pattern.)
In any case, say your project team decides "Hey, we're going to switch from storing all our data in flat files to using a database." Well, if you have scattered the instantiations of that specific repository throughout your code, you now have a lot of different areas to fix and update, somewhat negating the advantages of the repository pattern. However, by declaring a member repository of your controller, you just switch from a FlatFileRepository to a DatabaseRepository, implement the new repository, and you are finished! Everybody on the team is happy!
Update: "Why Instantiate a Class Variable?"
To answer that question, you have to think of your two options. The first option is that you could hold a relatively small object in memory. The other alternative is you could instantiate a new object in memory every time that a user needs to access one of your actions causing new memory to be allocated (and deallocated when you leave the scope that the object was in) and requiring more work by the server which is hosting your web app.
If you think about how a website is used, users are hitting those actions on a frequent basis. Every action signifies a portion of your website. If you instantiated every single time you needed the repository, you would quickly give the server a much bigger workload than it actually needs - particularly as your site grows in size. (I am sure other folks can think of other reasons why you would want to do it the way shown in the tutorial vs. instantiation in each individual action.)
And, then, of course, there is the refactoring issue as I mentioned above. That is about "efficiency of making changes" or improving the maintainability.
Hope that helps you a bit more and better answers your question!
发布评论
评论(1)
为了回答你的问题,Travis,你应该阅读一下存储库模式。该模式用于提供许多优点,包括:
至于你的问题:“这是一种常见做法吗?”答案是“应该是”。但不幸的是,我并没有像我希望的那样看到它。
现在,在您的示例中,您显示了在控制器类的上下文中创建的存储库。 (请注意,这与您所说的“全局”对象不同。全局意味着可以从任何范围访问该对象。了解更多信息 此处 位于 Wikipedia。)
无论如何,存储库的优点之一是,存储库允许您在访问数据的方式(甚至是访问数据的位置)之间进行更改。数据正在被访问)通过使用正确的,存储库的具体版本。因此,您可能拥有:
如果您想切换到其他数据源,只需更换控制器中的具体实现即可。 (请注意,通过 依赖注入 有更高级和灵活的方法来执行此操作,但那是超出了这个问题的范围,尽管它确实/可以在存储库模式中发挥重要作用。)
无论如何,假设您的项目团队决定“嘿,我们将从将所有数据存储在平面文件中切换到使用数据库”。好吧,如果您将特定存储库的实例分散在整个代码中,那么您现在有很多不同的区域需要修复和更新,这在某种程度上否定了存储库模式的优势。但是,通过声明控制器的成员存储库,您只需从 FlatFileRepository 切换到 DatabaseRepository,实现新的存储库,就完成了!团队里的每个人都很高兴!
更新:“为什么实例化类变量?”
要回答这个问题,您必须考虑两个选择。第一个选项是您可以在内存中保存一个相对较小的对象。另一种选择是,每次用户需要访问您的某个操作时,您都可以在内存中实例化一个新对象,从而导致分配新内存(并在您离开该对象所在的范围时释放内存)并需要执行更多工作托管您的网络应用程序的服务器。
如果您考虑一下网站的使用方式,就会发现用户会频繁点击这些操作。每个操作都代表您网站的一部分。如果您每次需要存储库时都进行实例化,那么您很快就会给服务器带来比实际需要大得多的工作负载 - 特别是当您的站点规模不断扩大时。 (我确信其他人可以想到其他原因,说明为什么您希望按照教程中所示的方式进行操作,而不是在每个单独的操作中进行实例化。)
当然,还有我上面提到的重构问题。那就是关于“更改的效率”或提高可维护性。
希望对您有更多帮助,更好地回答您的问题!
To answer your question, Travis, you should read up a bit on the Repository Pattern. This pattern is used to provide a number of advantages, including:
As to your question: "Is this a common practice?" the answer is, "It should be." but, unfortunately, I don't see it as much as I would prefer.
Now, in your example, you show the repository being created within the context of your controller class. (Please note that this is NOT the same thing as an object being made "global" as you put it. Global means that the object is accessible from any scope. Read more about it here at Wikipedia.)
In any case, one of the advantages is that the repository allows you to change between how you are accessing your data (or even where your data is being accessed) by using the correct, concrete version of the repository. So, you may have:
If you wish to switch to other data sources, it is a simple as swapping out the concrete implementation in your controller. (Please note, there are more advanced and flexible ways of doing this through dependency injection, but that is out of scope of this question, although it does/can play heavily in the repository pattern.)
In any case, say your project team decides "Hey, we're going to switch from storing all our data in flat files to using a database." Well, if you have scattered the instantiations of that specific repository throughout your code, you now have a lot of different areas to fix and update, somewhat negating the advantages of the repository pattern. However, by declaring a member repository of your controller, you just switch from a FlatFileRepository to a DatabaseRepository, implement the new repository, and you are finished! Everybody on the team is happy!
Update: "Why Instantiate a Class Variable?"
To answer that question, you have to think of your two options. The first option is that you could hold a relatively small object in memory. The other alternative is you could instantiate a new object in memory every time that a user needs to access one of your actions causing new memory to be allocated (and deallocated when you leave the scope that the object was in) and requiring more work by the server which is hosting your web app.
If you think about how a website is used, users are hitting those actions on a frequent basis. Every action signifies a portion of your website. If you instantiated every single time you needed the repository, you would quickly give the server a much bigger workload than it actually needs - particularly as your site grows in size. (I am sure other folks can think of other reasons why you would want to do it the way shown in the tutorial vs. instantiation in each individual action.)
And, then, of course, there is the refactoring issue as I mentioned above. That is about "efficiency of making changes" or improving the maintainability.
Hope that helps you a bit more and better answers your question!