首页 > 代码库 > Naked objects(v8.x) 起步
Naked objects(v8.x) 起步
Introduction to NOF 8.1
8.0 版本的 Naked Objects Framework 提供了新的用户界面,不仅外观不一样,还提供了‘Single Page’ 架构(SPA)。 写的客户端编译成 JavaScript,使用 Angular.js 框架 (version 1.5),提供了大量本地执行的功能。APA使用 Restful API通讯,卷宗 Restful Objects 1.1 公共规范 (详见 www.restfulobjects.org )。
8.1 版本引入三个新的强大功能:
- MultiLine dialogs, 大容量数据入口。
- Multi-select actions on collections within objects. (为减少歧义,之前所谓的‘collection-contributed actions’,只是应用于查询结果,现在叫做‘query-contributed actions’)。
- 能够在对象集合中关联其它动作。
修复了一些小 bug 。
重要提示: 8.1版本使用 TypeScript 2。确保安装了 TypeScript 2 , Naked Objects 项目从 8.0升级后,项目会被配置成 TypeScript 2。
接下来 SemVer 约定, 8.1 添加新的不中断现有已发布 API的能力。鉴于这个因素,此手册依然可用于 8.0,不会在使用新功能时候产生异常。
运行模板项目
第一次开始 NOF8,或将 NOF8 运行项目添加到现有的 NOF7 应用程序,最好的方式是使用 NakedObjects.Template 项目。你可以从这里下载 .zip文件:
https://github.com/NakedObjectsGroup/NakedObjectsFramework/blob/master/Run/NakedObjects.Template.zip?raw=true.
Note: We recommend using the .zipped version of the Template project, rather than taking the source directly from the Master branch of the repository - because the latter is under continusous development and may references versions of packages that have not yet been released publicly on the NuGet gallery.
It should be possible to just run the unzipped project ‘as is’ within Visual Studio 2015 - this starts the server, and the single page application to connect with it.
故障捕获: If, when you start up the application it displays the message ‘Failed to connect to server …’ this usually means that the server has not started up. You can check the log file nakedobjects_log.txt (found in the same directory as the project file) for any errors. The most likely cause is that Entity Framework has been unable to create the database. Check the connection string in Web.config:
<connectionStrings> <add name="NakedObjectsExample" connectionString="Data Source=(LocalDB)\MSSQLLocalDB; Initial Catalog=NakedObjectsExample; Integrated Security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /> </connectionStrings>
this is set to work with LocalDB, which should be set up by default on VS2015. If you do not have LocalDB set up, try changing the connection string to point to a database that does work for you e.g. SQLEXPRESS.
The Template project contains a minimal 示例模型. Running the project will create the database . You should then be able to create a Customer, and retrieve all saved customers. See also NOF 8 用户界面向导。
Once you have successfully run it. Note the following:
- The Template application has installed the NakedObjects.Run NuGet package. This in turn has installed various dependent Naked Objects packages.
- The 示例模型 is included within the ‘run’ project. In a real application we recommend that you create your domain object model in one or more separate projects. The model project(s) would then depend only on the very light-weight NakedObjects.ProgrammingModel package - not the rest of the Naked Objects framework.
- Within the run project, the application is configured primarly through the NakedObjectsRunSettings class. For details of what must be specified see: 应用程序配置. (For existing users: note that this is exactly the same as for a NOF 7 run project).
- The template run project creates the server that provides a complete Restful API from the domain model, and also provides the ‘single page application’ client that runs within a browser. The latter consists of the single html page (index.html) and the necessary script files, some of which are specific to Naked Objects and others being third party frameworks such as Angular, LoDash, and JQuery. All of the Naked Objects scripts are written in TypeScript and compiled to JavaScript.
- For a larger scale application you might prefer to maintain the Restful API server and the Spa client in separate projects. To do this you should un-install just the NakedObjects.Run and NakedObjects.Spa packages, leaving all the other Naked Objects packages installed. Then you can install just the NakedObjects.Spa package into a new ASP.NET project, and configure the nakedbjects.config.ts script to point to the Restful API. Note, however, that you will probably also need to configure the server for cross-origin resource sharing (CORS) using standard ASP.NET patterns.
浏览示例模型
这个示例由两个领域类组成. Customer 是一个 领域对象 (实体) 类:
public class Customer { [NakedObjectsIgnore] public virtual int Id { get; set; } [Title] public virtual string Name { get; set; } }
Note that:
- All persisted properties in a Naked Objects application must be virtual.
- [NakedObjectsIgnore] specifies that this property is never going to be displayed on the user interface.
- [Title] specifes that the value of that property should be displayed as the title for that object (shown at the top of a view, or in a link to that object).
CustomerRepository 是一个领域服务 用于创建和返回这些实体 (严格来讲是结合了 ‘factory’ 和‘repository’ 服务 – 如果你喜欢你可以将这两个角色分到两个服务里):
public class CustomerRepository { public IDomainObjectContainer Container { set; protected get; } public Customer CreateNewCustomer() { return Container.NewTransientInstance<Customer>(); } public IQueryable<Customer> AllCustomers() { return Container.Instances<Customer>(); } public IQueryable<Customer> FindCustomerByName(string name) { return AllCustomers().Where(c => c.Name.ToUpper().Contains(name.ToUpper())); } }
The IDomainObjectContainer is the single point of contact between domain model code and the outside world. Amongst other things, this Container acts as a wrapper for the Entity Framework, so your domain model need not (and should not) have any direct contact with Entity Framework functionality. The container is automatically injected into this repository by the Naked Objects Framework; the container may also be injected into an entity object; and you can use the same pattern to inject an instance of your own services, such as this CustomerRepository, into other services or entities.
领域模型项目包含了使用标准Entity Framework Code First的模式将映射的对象放到数据库里的职责 - 也就是 ExampleDbContext 类的意图:
public class ExampleDbContext : DbContext{ public ExampleDbContext(string dbName) : base(dbName) { Database.SetInitializer<DbContext>(new DropCreateDatabaseIfModelChanges<DbContext>()); } public DbSet<Customer> Customers { get; set; }}
这个示例中领域类型与数据库表是根据默认约定自动映射的; 在真实的项目中,你可能想使用标准Entity Framework模式直接控制映射。 你也可能想提取 DbContext,将领域模型中数据设备类分离到各个项目中。详见 Working withEntity Framework.
升级现有的 NOF 7项目
Upgrading an existing project from NOF7 toNOF8 is straightforward – at least if you are using the generic Naked Objectsuser interface. However, anycustomisations you have made to thegeneric UI will need to be completely re-written to work in the new Single PageApplication client architecture, and you will need to be familiar withTypeScript and Angular to do this. See Addingcustom views to the UI.
Note: we do not recommend attempting to upgrade directly to NOF8from NOF6 or earlier versions of the framework, without having first upgradedto NOF7 and run successfully. (Instructions for upgrading from NOF6 to NOF7were included in the NOF7 Developer Manual, which may be obtained from the .zipof the NOF7.0 release on GitHub).
If you have an existing application runningunder NOF 7, then the good news is that nochanges are required to your domain model(s). NOF 8.0 has deliberatelystayed with the 7.0 Programming Model for this reason. As well as minimisingthe upgrade effort, the huge advantage of this design decision is that you canrun both the NOF 7 client and the NOF 8 client in parallel, against the samedomain model(s). Indeed, we recommend running the two in parallel rather than upgrading your existing Run(client) project(s), at least until NOF 8 has gone to full release and youare happy with the new user interface.
- So start by obtaining the NakedObjects.Template project and add this to yoursolution alongside your existing NOF7 Run project.
- Rename the project andnamespaces as desired.
- Add references to your domainmodel project(s).
- Delete the Example Model folder.
- Replace the NakedObjectsRunSettings from the template with a copy of the equivalent file in your NOF7run project.
编写新的应用程序
We recommend that you create your domainmodel in separate project(s). A domain model project needs to have the NakedObjects.ProgrammingModel NuGet package installed, but not any other part of the NakedObjects Framework.
Before proceding further we recommend thatyou install the NakedObjects.Ide NuGet package. This does not contain any run-time code, and istherefore not necessary for writing aNaked Objects application, but it does contain a number of useful filetemplates and code snippets, that can make writing applications even faster. Itis not necessary to have this package installed in each project. Once you’ve installedit into any project, the templates and code snippets will be available on allVisual Studio projects.
Now start to create your domain model,following the patterns and conventions described in Domainmodel - programming concepts and A how-to guide,and using the short-cuts described in See Using theNaked Objects IDE.
To turn your domain model into anapplication you will need to add a ‘Run’ project into your solution, and addreferences to the domain model. By farthe best way to so this is to use a copy of Templateproject, changing the project name and namespaces as desired. Delete the Example Model folder and allreferences to it in NakedObjectsRunSettings.
Now configure the application, followingthe guidelines in Application configuration .
Naked objects(v8.x) 起步