我们可以从我们称之为全局方法的地方创建全局类 [英] can we create global class from where we call global methods

查看:58
本文介绍了我们可以从我们称之为全局方法的地方创建全局类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同步编解码器。我已经在不同的控制器中编写了相同的函数,并希望在一个地方编写该函数并在应用程序的任何地方调用,以便在何处编写该方法以及如何访问而不是存储库。

i want to synchronize codec. i have written same function in different controller and want to write that function in one place and call in anywhere in application so where to write that methods and how too access rather than repository.

推荐答案

1.如果代码与多个控制器中的相同功能相关,最好是为所有控制器创建基类并仅保留代码。通过这种方式,您可以拥有:将在所有控制器中继承的受保护,公共或静态方法。



我在下一篇MVC4文章中以这种方式做到了:< a href =http://www.codeproject.com/Articles/545026/MVC-Basic-Site-Step-Exceptions-Management> MVC基本站点:第2步 - 例外管理 [ ^ ]所有控制器都继承 BaseController 类。



2.通常,如果您希望某些全局方法可以访问整个上下文(程序集,应用程序,解决方案),您应该使用静态方法并放置方法将一个公共类和一个类放入一个项目(程序集)中,该项目可以访问所有需要这种通用功能的项目。
1.If the code is related with the same functionality in multiple controller, better is to create a base class for all your controller and keep your code only there. In this way you could have: protected, public or static methods that will be inherited in all your controllers.

I did in this way in my next MVC4 article: MVC Basic Site: Step 2 - Exceptions Management[^] All controller are inhering BaseController class.

2.In generally, if you want some global methods to be accessible into an entire context (assembly, application, solution) you should use static methods and place the methods into a public class and the class into a project (assembly) that is accessible into all projects that will need that common functionality.


这篇关于我们可以从我们称之为全局方法的地方创建全局类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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