SCSF. Chapter 2. C. The Shell Application and Initialization Process
You tend not to do a whole lot 大批的 in the shell application, just as you tend not to do a whole lot in any program's main file. The shell application contains your program's entry point and performs initialization. Most of the interesting business logic lives elsewhere 在别处.
你尝试不要在shell程序中做大量的工作,就像你尝试不在程序的主文件中做大量的工作一样。shell程序包含你的项目的入口点和执行初始化。大部分有趣的业务逻辑存在于其他地方。
The SCSF Wizard generates a shell application class deriving from the SCSF base class SmartClientApplication,which in turn derives from the FormShellApplication of CAB. The code is shown on the facing page 对开页. You can find the former class in your app's Infrastructure.Library project. It adds a number of services to the base application, such as the action catalog service (IActionCatalogService) and the entity translator service (IEntityTranslatorService).
SCSF向导生成了一个shell程序类派生于SCSF基础类SmartClientApplication,并同时派生于CAB的FormShellApplication类。代码展示在对开页中,你可以在你的项目的Infrastructure.Library项目中找到前面这些类。它添加了一些基础程序的服务。就像是action catalog service (IActionCatalogService)和entity translator service(IEntityTranslatorService)。
In the declaration of your application class, you pass it the types that you want the shell to use for its shell form and root WorkItem. The ShellForm class was generated for you by the wizard. If you think of it as MyOwnShellForm, you'll have the right mental model. The first generic parameter represents 代表 the class to use for the root WorkItem. In the early days of CAB, it was common to derive your own class from the base WorkItem, but this design philosophy 哲学 has now changed, as I discuss in Chapter 3. Today, you generally use the CAB class WorkItem directly, as shown in the example. You start the application by calling ShellApplication.Run( ). To that method, we now turn our attention.
在你的程序类的声明中,你把shell form和root workItem需要用到的类型传递给它。ShellForm类是在向导中生成的。如果你想想MyOwenShellForm,你会得到正确的心智模型。第一个泛型参数是root WorkItem所使用的。在CAB的早些时候,通常将你的类派生于基础WorkItem类。这种设计理念并未改变。今天,你直接使用CAB workItem类,就像例子中一样,你通过调用ShellApplication.Run()方法启动程序。我们现在把注意力集中在这个方法上。
using System;
using System.Windows.Forms;
using SimplestSCCF.Infrastructure.Library;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
namespace SimplestSCCF.Infrastructure.Shell
{
/// <summary>
/// Main application entry point class.
/// Note that the class derives from CAB supplied base class
/// FormShellApplication, and the main form will be ShellForm,
/// also created by default by this solution template
/// </summary>
class ShellApplication : SmartClientApplication
<WorkItem, ShellForm>
{
/// <summary>
/// Application entry point.
/// </summary>
[STAThread]
static void Main()
{
#if (DEBUG)
RunInDebugMode();
#else
RunInReleaseMode();
#endif
}
private static void RunInDebugMode()
{
Application.SetCompatibleTextRenderingDefault(false);
new ShellApplication().Run();
}
}
}
The startup sequence 顺序 of any application is always important to understand. I show it to you in Figure 2-4. Here is what happens when you call ShellApplication.Run( ):
任何程序的启动顺序都是最需要去理解的。我在图2-4中已经说了。当ShellApplication.Run()启动的时候会发生哪些事情。
-
The CAB Framework creates the root work item, an object of the class passed in the generic template on the previous page (in that case, WorkItem)
CAB框架创建了root work item,workitem对象被传递到泛型模板中。
-
Any visualizers (see the end of Chapter 1) that are specified in the app.config file are created and shown, thereby 因此 allowing a developer to monitor the internals of the CAB program.
app.config文件中指定的visulizers被创建和展示,从而允许开发者监视CAB项目的内部。
-
CAB now adds the services that its infrastructure always needs, the list of which is hard-wired into the code. Examples of these required services are the authentication, module loader, and module enumerator services.
CAB添加了基础设施需要的服务,硬编码在代码中。这些必须的服务包含authentication, module loader, and module enumerator services.
-
You can add new services or modify existing ones in the shell application's app.config file, as I discuss later in this chapter. These configured service operations are now performed.
你可以添加新的服务或者在app.config中修改已存在的服务,这些配置服务被执行了。
-
CAB now calls the shell application's AddServices() method. If you want to add or modify any services programmatically 动态配置, you override this method in your shell application class and place your code there.
CAB启动了shell appplication的AddServices()方法。如果你想动态配置添加或者修改服务,你则要在shell application类中重写这些方法。
-
The CAB startup code now fetches the registered implementation of its IAuthenticationService interface and calls its Authenticate( ) method to authenticate the user. The default implementation sets the CAB program's identity to that of the user who is logged in to the Windows desktop.
CAB启动代码获得IAuthenticationService接口注册的实现然后启动Authenticate( ) 方法去验证用户。默认实现设置了可以登录到windows桌面的用户的ID。
-
The main CAB program obviously has to be loaded and running for any of this to have taken place, but CAB hasn't really thought about the properties of the main program yet. The module loader service now processes the shell assembly, checking, for example, for service dependencies.
CAB主程序明显需要被加载和运行,但是CAB没有真正考虑过主程序的属性。module loader service处理shell程序集,例如,服务依赖。
-
The root work item now has its BuildUp method called. This causes it to be scanned for dependencies, and those dependencies to be injected, being created in the process if need be. The root work item is dependent on the shell form, and this dependency causes the form to be created during this call.
root work item现在需要启动它的构造方法。这导致扫描它的依赖,和那些被注入的依赖,在过程中需要的则被创建。root work item在shell form中被依赖,因此导致此时form被创建。
-
Modules are now loaded into the CAB program. The module enumerator service fetches its list of modules, and the module loader service loads and initializes them. The default enumerator service reads its list from the ProfileCatalog.xml file. The last portion 部分 of this chapter shows you the possibility of customizing this behavior.
modules这时被加载到CAB项目中。module enumerator service获取modules列表,module loader service加载并初始化他们。default enumerator service读取ProfileCatalog.xml文件。这章最后的部分展示了自定义的可能性。
-
CAB calls Run on the root work item, which fires its OnRunStarted event.
CAB启动root work item里的run方法,触发了它们的OnRunStarted事件。
-
CAB calls Start on its application, which in turn calls System.Application.Run, passing ShellForm. This is normal Windows Forms startup that begins to service the application's Windows message pump. This method does not return until the application shuts down.
CAB启动了程序中的start方法,依次启动System.Application.Run方法,传递ShellForm对象。这是通常winform启动并开启windows message服务。
这个方法并不返回,直到程序关闭。
Figure 2-4. Startup sequence of a CAB application.

浙公网安备 33010602011771号