首页 \ 问答 \ .net字符串缓存/池(.net string cache/pool)

.net字符串缓存/池(.net string cache/pool)

.net sting类是否支持缓存/池机制以减少分配开销?


Does .net sting class support cache/pool mechansism to reduce allocation overhead?


原文:https://stackoverflow.com/questions/3577287
更新时间:2023-09-30 10:09

最满意答案

通常,您会将异常处理代码放在控制器中。 在您的术语中,我假设MVC控制器存在于您的“Web”层中,并且这些控制器调用“服务”层中的方法,例如您显示的“DeleteOrder”方法。 如果是这种情况,在DeleteOrder中的错误处理代码中,您应该抛出一个异常:

if (order == null)
{
    throw new InvalidOperationException("Specified OrderId does not exist");
}

这样,未处理的异常将传递给您的异常处理代码所在的控制器,在那里您可以记录异常并将用户重定向到相应的错误页面。

至于如何在控制器中处理异常,您有许多选择:

  1. 对每个操作方法使用try-catch块
  2. 通过实现OnException方法在控制器类上实现IExceptionFilter接口
  3. 使用内置的HandleErrorAttribute异常过滤器
  4. 创建自己的自定义异常处理过滤器

第四种方法(创建自己的异常过滤器)可能是最强大的方法。 在这里,您可以添加异常日志记录,以及根据引发的异常类型将用户重定向到适当的错误页面的代码。

您可以在此处找到MVC控制器异常处理的概述。


Generally you would put your exception handling code in your controllers. In your terminology, I am assuming the the MVC controllers live in your "Web" layer, and that these controllers call methods in your "service" layer, such as the "DeleteOrder" method you've shown. If this is the case, in your error handling code in DeleteOrder, you should simply throw an exception:

if (order == null)
{
    throw new InvalidOperationException("Specified OrderId does not exist");
}

This way the unhandled exception will be passed to your controller, where your exception handling code lives, and there you can log the exception and redirect the user to the appropriate error page.

As far as how to handle the exception in your controller, you have a number of options:

  1. Use a try-catch block for each action method
  2. Implement the IExceptionFilter interface on your controller class by implementing the OnException method
  3. Use the built-in HandleErrorAttribute exception filter
  4. Create your own custom exception handling filter

The fourth method (create your own exception filter) is probably the most robust way to go. In here, you can add exception logging, as well as code to redirect the user to an appropriate error page based on the type of exception that is thrown.

You can find a good overview of MVC controller exception handling here.

相关问答

更多
  • 我看到它的方式,有一个包含以下类的BusinessLogic类库:CustomerBusinessLogic.cs,OrderBusinessLogic.cs等 哎哟。 获取并阅读Scott Ambler的“构建对象应用程序”。 你的方法不是,而且是一个维护工作 - 没有对象。 我使用LINQ-To-SQL与数据库通信。 WCF服务,用于定义业务逻辑层的数据协定接口。 然后将MVP与ASP.NET Forms一起用于UI / BLL。 是。 使应用程序人为地更复杂和更慢的好方法。 抛弃完整的WCF服务 - ...
  • MVC不是要取代N-Tier,而是组织表示层的一种方法。 我不会说控制器比逻辑层更强大。 相反,控制器(作为表示层的一部分)仍然应该调用逻辑层。 控制器应该只准备视图的数据并处理来自视图的操作。 你仍然应该使用你的BLL。 MVC is not to replace N-Tier, it is a way to organize the presentation layer. I would not say that the controller is more powerful than the logi ...
  • 如果您的项目足够小,每层只有一个库就足够了,那么我会采用这种方法。 这有助于保持关注点的明确分离。 根据我的经验,单独的DLL不会对性能产生不利影响。 在某些情况下,它可以帮助提高性能(例如延迟加载很少使用的组件)。 所有的DLL都被加载到相同的地址空间中,因此就运行时而言,一个或多个DLL几乎没有任何区别。 应该编写每个层,就好像多个前端将要使用它一样。 这将进一步有助于保持分离,并鼓励更正确,更易于维护的代码。 If your project is small enough that only one ...
  • 您是否在codeplex中查看MyPrettyCMS Framework? 很明显...... https://myprettycms.codeplex.com 在下一个版本中,它包括Web API和Web API OData处理。 如果您想关注开发者组,请找到Linked in group:myPrettyCMS Happy Contributors Associates Did you look MyPrettyCMS Framework in codeplex ? It's exactely that ...
  • 经典的2层应用程序 是您描述的那个:您有连接到服务器的客户端(浏览器)(可以配置为多个服务器)运行ASP应用程序,您的AppCode类加入本地网络(或同一台机器)中的数据库 n层应用程序 您可以使用WCF来交付客户端连接到服务器的n层应用程序,并且服务器连接到在同一服务器或其他几个服务器上运行的WCF服务 注意:这是一个简短的故事,命名有争议。 Classic 2 tiers app Is the one you described: You have the client (the browser) co ...
  • 就个人而言,我会对整体结构有一些改变,但我猜一个完整的设计评审并不是你所要求的。 关于你的实际问题,不 - 你的其他层不需要引用MVC。 对于大多数应用程序,需要在表示层中配置和初始化IoC。 最终,您的表示层需要一个参考链(直接或间接引用)到您想要注册的所有内容,但这一直都是正确的。 您已经引用了Helpers,Resource,ViewModel和BLL,因此您可以轻松地在这些层中注册接口的实现。 您还可以添加对DLL的引用以注册该层的实现。 您还可以使用间接路由并在每个层中添加一个类,该类引用您的Io ...
  • 依赖注入是关于注入实例的,因此在此上下文中不能使用静态类和/或方法。 整个想法是创建松散耦合的代码,我们这样做是编程到抽象 ,这意味着接口。 接口不适用于静态方法。 会话中的值与DI无关。 我们的想法是将定义(接口)与其实现(类)分开。 可以在与对应的实现类不同的层中定义接口。 这就是它如此强大的原因。 您可以在相当低级别的层中定义接口,并将实现放在UI层中。 这方面的一个示例是用户上下文类,其中接口位于业务层中,实现类位于Web层中,因为这是您要在实现中使用的会话。 DI使您可以在业务层中使用此接口的实现 ...
  • 通常,您会将异常处理代码放在控制器中。 在您的术语中,我假设MVC控制器存在于您的“Web”层中,并且这些控制器调用“服务”层中的方法,例如您显示的“DeleteOrder”方法。 如果是这种情况,在DeleteOrder中的错误处理代码中,您应该抛出一个异常: if (order == null) { throw new InvalidOperationException("Specified OrderId does not exist"); } 这样,未处理的异常将传递给您的异常处理代码所在 ...
  • WCF配置文件服务可以满足您的要求。 看看这里 。 您可以在此MSDN页面中看到它提供的方法列表 The WCF profile service does what you are asking for. Have a look at it here. You can see the list of methods it provides in this MSDN page
  • 我从来没有使用过NpgSql,但是如果你在连接字符串中添加“enlist = true” ,那么阅读NpgSql的文档似乎有一些TransactionScope()的支持。 我正在查看以下NpgSql文档的“System.Transactions支持”部分: http ://npgsql.projects.postgresql.org/docs/manual/UserManual.html 假设TransactionScope()确实有效,那么你可以做这样的事情...... using (var scope ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。