在类初始化期间响应应用程序终止事件

发布于 2024-07-16 06:13:33 字数 471 浏览 0 评论 0原文

启动我的应用程序时,我有几个类需要读取某些文件才能创建一组默认数据。

(对我来说)执行此操作的逻辑位置是在 Shared 类构造函数中; 这个想法是,如果默认文件的读取失败,则抛出一个类级事件。 不幸的是,这不起作用,因为尝试访问此类事件时,为了向其附加处理程序,需要在附加事件之前触发类构造函数。 在失败的情况下,构造函数启动,触发失败事件,构造函数完成,然后在事件触发后附加事件处理程序。

我能想到的唯一其他解决方案是为该类提供一个“typeInitializedSuccessively”布尔属性,并在每个调用周围放置一个 try/catch 块以构造该类的实例,这对我来说似乎不必要的混乱。

有人可以建议一个更优雅的解决方案吗?

编辑:因为这是一个基本类,以一种或另一种形式在几乎所有软件工具中使用,所以我非常喜欢一种解决方案,该解决方案将通知未来的程序员需要调用类型初始化程序,这就是我最初走向的原因共享构造函数作为解决方案。

When starting my application, I have a couple of classes which are required to read certain files in order to create a set of default data.

The logical place (to me) to do this is in a Shared class constructor; the idea would be to throw a class-level event if the reading of the defaults file fails. Unfortunately, this does not work as attempting to access such an event, in order to attach a handler to it, fires the class constructor before the event has been attached. In a failing case, the constructor starts, fires the fail event, the constructor completes, and then the event handler is attached, after the event has fired.

The only other solution I can think of is to give the class a "typeInitialisedSuccessfully" boolean property and put a try/catch block around every call to construct an instance of the class, which seems unnecessarily kludgey to me.

Can someone suggest a more elegant solution?

EDIT: Because this is a fundamental Class, used in one form or another across nearly all of our software tools, I would greatly prefer a solution that will notify future programmers that the type initialiser needs to be called, which is why I initially went towards the Shared Constructor as a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-07-23 06:13:33

我建议您的类上有一个 Init (可以是静态的)方法。 您可以在应用程序启动时为每个要初始化的类运行此方法。 在这种情况下,您可以将其包装在 try catch 中并采取相应的操作。

重新编辑:
一般来说,在共享构造函数之类的地方抛出异常是个坏主意 - 你无法真正捕获它,很难预测何时调用 init 等。

如果你指定,我会添加要检查的 IsInitialized 字段和异常调用需要它的方法/属性时抛出。 但它可能会变得乏味。

我会选择的另一个选择可能与您当前的架构相去甚远。 在许多 IoC 工具中,他们使用 Startable 的概念(castleautofac) - 即您指定一个带有启动方法的接口,IoC 将确保该方法在指定时间运行(最有可能在应用程序启动时)。 您将在“Start”方法中进行错误处理,并使用所有详细信息来冒泡任何包装的异常。
我可以详细说明这一点,但我感觉这并不是您真正想要的方式。

I would suggest having an Init (can be static) method on your classes. You would run this method on your application start for each of your classes-to-be-initialized. In that case you can wrap it in try catch and act accordingly.

Re your edit:
Generally throwing exceptions in places like shared constructor is bad idea - you can't really catch it, it's hard to predict when the init will be called etc.

In case you specify I'd add either IsInitialized field that would be checked and an exception thrown when calling method/property that requires it. It can get tedious though.

The other option I would go for might be far from your current architecture. In many IoC tools they use concept of Startable (castle, autofac) - i.e. you specify an interface with a start method and the IoC will make sure the method runs at specified time (most likely when application is started). You would do your error handling in your 'Start' method and bubble up whatever wrapped exception with all the detailed information.
I could elaborate on this but I have feeling it's not really the way you would go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文