Naked objects 起步

If you’re new to Naked Objects start by trying this ultra-simple example: Writing your first Naked Objects application. Then you can progress in a slightly more formal manner.

A Naked Objects application will typically consist of multiple projects.

First, there will typically be one or more ‘Model’ projects, containing your domain classes. See:

Creating a domain model project

Next you will need one or more ‘Run’ projects. If you want a ready-made user interface then you should add an MVC project. See:

Running your domain model(s) as a Naked Objects MVC application

Later you might also decide to add a Restful API, as an alternative way to ‘run’ your domain model:

Creating and using a Restful Objects API

Or the ability to run as a standalone executable, for example for Batch operations:

Running without a user interface

You will also want to add one or more ‘Test’ projects.  These may be conventional Unit Tests, but we strongly recommend that you also consider using the powerful Naked Objects XAT framework, to write functional or integration tests that are UI-independent:

Executable Application Tests (XATs)

If you are sticking to the generic Naked Objects MVC User Interface then there is typically no need to then write UI tests (which are notoriously brittle to change). But if you customise the user interface (other than just by modifying the CSS) then you should write UI tests.  We’ve written some helper classes to work with the respected Selenium framework, see:

End to End Testing with Selenium

Writing your first Naked Objects application

Follow these steps to write your first ultra-simple Naked Objects application, featuring one simple domain class only:

  1. Using Visual Studio 2013 (Express version is fine) create a new ASP.NET Web Application project called, say, MyWebApp, using the MVC template.
  2. Install the NuGet Package NakedObjects.Mvc-FileTemplates, selecting Yes To All when asked if you wish to overwrite existing files.
  3. In the Models folder add a new class Customer as follows. Note that:
  • All properties in a Naked Objects application must be virtual.
  • [Hidden] specifies that this property is not for display on the user interface.
  • [Title] specifes that the value of the property should be displayed in the Tab.

using NakedObjects;

namespace MyWebApp

{

public class Customer

{

[Hidden]

public virtual int Id { get; set; }

[Title]

public virtual string Name { get; set; }

}

}

  1. Create a DbContext object as follows. (This is standard Entity Framework Code First coding.)

using System.Data.Entity;

namespace MyWebApp

{

public class MyDbContext : DbContext

{

public DbSet<Customer> Customers { get; set; }

}

}

  1. In the App_Start folder find the RunWeb class and edit two members as follows. First, the MenuServices property which defines the services to be shown on the main menu. (NakedObjects.Services.SimpleRepository is a ready-made class for early-stage prototyping only.)

protected override IServicesInstaller MenuServices {

get {

return new ServicesInstaller(

new SimpleRepository<Customer>());

}

}

Second, the Persistor property, in which we need to specify the DbContext(s) that it needs to inspect:

protected override IObjectPersistorInstaller Persistor

{

get

{

var installer = new EntityPersistorInstaller();

installer.UsingCodeFirstContext(() => new MyDbContext());

return installer;

}

}

  1. Run the project.  Using the actions on the Customers menu, try creating and retrieving Customer objects.

Creating a domain model project

Naked Objects uses Microsoft’s Entity Framework to persist domain objects in a database. Most developers who work with Entity Framework now use it in ‘Code First’ mode -  and this is what we now use throughout this manual. The name is slightly misleading:  you can use ‘Code First’ mode even when creating an application to work against an existing database – ‘Code First’ just means that your persistence is entirely defined in program code (typically C#).  Naked Objects can, however, work equally well with Entity Franework in the older mode, where the entity model is defined in XML with a .edmx file.

This manual does not attempt to provide an introduction to Entity Framework Code First development - rather it just emphasises what you need to do to make your project work with Naked Objects. We therefore recommend that you gain some general familiarity with Entity Framework Code First development: there are numerous on-line tutorials, and we also strongly recommend the book Programming Entity Framework - Code First by Julia Lerman and Rowan Millar.

When copying any domain code examples from the book or on-line Code First tutorials mentioned, please remember the following basic rules:

  • Naked Objects requires that all properties are virtual.
  • Naked Objects requires that all collections are virtual, and are initialised (but not in a constructor). See Collection properties.
  • Entity Framework Code First makes all properties optional in the database, unless specified as mandatory (using the Required attribute, or via the Code First Fluent API). However, at the user interface (in Naked Objects MVC) or Restful API (Restful Objects for .NET) the Naked Objects framework treats all properties as mandatory, unless marked up with the Optionally attribute - as we believe that this is the safer default behaviour. Note that it is possible to configure Naked Objects to work the other way if you wish (see Optional vs. Required).

Define your model project(s)

Your starting point will be to develop one or more Model projects, as follows:

  1. Create a new Class Library project.
  2. Invoke Manage NuGet Packages, find and install the NakedObjects.ProgrammingModel package (see also note below about Naked Objects IDE)
  3. Add POCO domain classes, following the simple programming conventions that are recognised by the Naked Objects framework. See Domain object.
  4. Create some services to act as Repositories/Factories.

Naked Objects IDE

When you installed the NakedObjects.ProgrammingModel via the NuGet Package Manager, it automatically added the NakedObjects.Ide package, which consists of a set of Item Templates and Code Snippets that can be very useful when construction your domain model. The item templates are used when invoking Add > New Item: you will find them listed under Naked Objects for the C# programming language. To see the list of code snippets: Tools > Code Snippets Manager > [Language] > My Code Snippets > Naked Objects. Most can be invoked using their shortcut, such as ‘propcho‘ to add a Choices method for a property. Note, however, that if you have other tools installed, such as Resharper, then these can sometimes invalidate the shortcuts. The IDE has been placed in its own package so that you have the option when un-installing the NakedObjects.ProgrammingModel from a specific project, whether or not you want to uninstall the IDE, which may be used by multiple projects.

Define your DbContext(s)

Next you will need to set up a ‘Context‘ class for your model project, which is defined in exactly the same way as for conventional Code First development. You can use the Naked Objects > DbContext item template to help create this class - as shown in the example below:

namespace DataAccess

{

public class MyContext : DbContext

{

public MyContext(string name) : base(name) { }

public MyContext() { }

public DbSet<Foo> Foos { get; set; }

public DbSet<Bar> Bars { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

//Initialisation

//Use the Naked Objects > DbInitialiser template to add a custom initialiser, then reference thus:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());

//Mappings

//Use the Naked Objects > Mapping template to add mapping classes & reference them thus:

//modelBuilder.Configurations.Add(new Employee_Mapping());

}

}

}

The context inherits from the class System.Data.Entity.DbContext it defines methods to return one or more DbSets.

It is not necessary to define a DbSet for each of your domain classes - just for the ‘root‘ classes in your model hierarchy.

Within the OnModelCreating method you may also:

  • Add database mappings using the Code First ‘Fluent API‘. Use the Naked Objects > DbMapping item template to create a mapping class quickly.
  • Add a database initialiser to determine when/whether to drop and re-create the schema. Use the Naked Objects > DbInitialiser item template to create an initialiser class quickly. You can use this initializer to seed the database, but you should consider - as an alternative - using Naked Objects‘ own Data Fixtures pattern as this has several advantages,

Overriding the default database schema generation

By default, Entity Framework Code First creates the database schema by following a set of conventions, based on the class and property names. These convention-based schema may be over-ridden or enhanced, either by using Code First Data Annotations in the domain classes, or by means of the Code First Fluent API. The latter is invoked by creating one or more configuration classes (inheriting from EntityTypeConfiguration<T>) and referencing them from within the DbContext, as in the following example (quoted from Programming Entity Framework - Code First by Julia Lerman and Rowan Millar):

namespace DataAccessFluent

{

public class DestinationConfiguration : EntityTypeConfiguration<Destination>

{

public DestinationConfiguration()

{

Property(d => d.Name).IsRequired();

Property(d => d.Description).HasMaxLength(500);

Property(d => d.Photo).HasColumnType("image");

}

}

namespace DataAccessFluent

{

public class DestinationConfiguration : EntityTypeConfiguration<Destination>

{

public DestinationConfiguration()

{

Property(d => d.Name).IsRequired();

Property(d => d.Description).HasMaxLength(500);

Property(d => d.Photo).HasColumnType("image");

}

}

public class LodgingConfiguration : EntityTypeConfiguration<Lodging>

{

public LodgingConfiguration()

{

Property(l => l.Name).IsRequired().HasMaxLength(200);

Property(l => l.Owner).IsUnicode(false);

Property(l => l.MilesFromNearestAirport).HasPrecision(8, 1);

}

}

//...

public class BreakAwayContextFluent : DbContext

{

public BreakAwayContextFluent(string name) : base(name) { }

public BreakAwayContextFluent() { }

public DbSet<Destination> Destinations { get; set; }

public DbSet<Lodging> Lodgings { get; set; }

//...

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

modelBuilder.Configurations.Add(new DestinationConfiguration());

modelBuilder.Configurations.Add(new LodgingConfiguration());

// ...

}

}

}

Using data fixtures with Code First

When working Code First, one option for creating data fixtures (to pre-populate the database with) is via the Seed method on the Database Initializer. The following example is quoted from Programming Entity Framework - Code First by Julia Lerman and Rowan Millar):

namespace DataAccess

{

public class DropCreateBreakAwayWithSeedData : DropCreateDatabaseAlways<BreakAwayContext>

{

protected override void Seed(BreakAwayContext context)

{

context.Destinations.Add(new Model.Destination {
                                                      
Name = "Great Barrier Reef" });

context.Destinations.Add(new Model.Destination { Name = "Grand
Canyon"
});

//...

}

}

}

This custom database initializer may be set within the Persistor
property of the Run class, as described earlier in this section, or within the OnModelCreating() method on your DbContext.

Another option is to use Naked Objects‘ own Data Fixtures
pattern, which has the advantage that you can delegate to methods on services
and objects just as if they were created by the user.

Running your
domain model(s) as a Naked Objects MVC application

Follow these steps to run your domain model(s) with the
Naked Objects MVC user interface.

  1. Create a new C# ASP.NET Web
    Application project, specifying MVC as the template,and set this as the Startup
    Project for your solution.
  2. Invoke Manage
    Nuget Packages, find and install the NakedObjects.Mvc-FileTemplates package into this project
  3. Add project reference(s) to
    your Model project(s).
  4. Copy the connection string(s)
    from the App.config file(s) in your Model project(s) into the Web.config
    in your Run project.

There are two web.config
files in a standard MVC project - one within the Views folder (the purpose of
which is to prevent direct access to the views without going through a
controller) and the other at the project root level. The connection string
needs to go into the latter.

    1. Open the RunWeb class
      (within the App_Start folder). Within this Run class, register your services, fixtures (if
      required), and make any other changes you may require to the default run
      configuration.
    2. Run the project as a local app.
      This will launch a web-browser pointing at the start page of your application,
      running within LocalHost.
    3. Even if you selected the Internet
      project template, by default, the application is not initially set up to
      require a log-on. When you are ready to add authorization to your prototype,
      simply un-comment the commented-out the [Authorize] attribute
      on the GenericController and SystemController classes.
    4. When you are ready, you can
      deploy the application to a server running IIS, in exactly the same way as any
      other ASP.NET MVC application. The simplest way is to right-click on the
      project and invoke the Publish action.
时间: 2024-10-09 18:11:03

Naked objects 起步的相关文章

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 版本引

常用-开发框架

常用-开发框架Spring MVC & Spring BootSpring MVC应该最常用的. Spring Web MVC框架( 简称Spring MVC)是一个丰富的模型视图控制Web框架.使用 DispatcherServlet发布请求处理,他使用可配置的处理程序映射,视图解析和主题解决方案.Spring MVC 用户似乎都商量好的,大约40%的开发都使用Spring.Spring Boot让开发团队在创建基于JVM的应用及方案更加有效率.至于Web框架,Spring Boot 为 Sp

Domain Driven Design and Development In Practice--转载

原文地址:http://www.infoq.com/articles/ddd-in-practice Background Domain Driven Design (DDD) is about mapping business domain concepts into software artifacts. Most of the writings and articles on this topic have been based on Eric Evans' book "Domain Dr

Github 的一个免费编程书籍列表

Index Ada Agda Alef Android APL Arduino ASP.NET MVC Assembly Language Non-X86 AutoHotkey Autotools Awk Bash Basic BETA C C# C++ Chapel Cilk Clojure COBOL CoffeeScript ColdFusion Cool Coq D Dart DB2 Delphi / Pascal DTrace Elasticsearch Emacs Erlang F#

计算机类免费电子书共享

列表最早来自stackoverflow上的一个问题:List of freely available programming books 现在在github上进行维护:free-programming-books List of Free Programming Books This list initially was a clone of stackoverflow - List of freely available programming books by George Stocker.

Github上的1000多本免费电子书重磅来袭!

这个GIthub库的免费电子书资源绝对值得你拥有,赶紧收藏吧! 以前 StackOverFlow 也给出了一个免费电子书列表,现在在Github上可以看到时刻保持更新的列表了. 瞥一眼下面的书籍分类目录,你就能知道这个免费电子书库的含金量了吧.记得一定要看几本,千万别下载了大量书籍而束之高阁! 行动重于空想! Github地址:     https://github.com/vhf/free-programming-books/blob/master/free-programming-books

49.Django起步学习

django起步 django安装 pip install django==2.0.4(版本号) pip install django 默认安装最新版本 创建项目 django-admin startproject myproject 开启开发服务器 cd myproject #进入项目目录 python manage.py runserver #开启服务 python manage.py runserver 7000 #改变服务监听端口 python manage.py runserver 0

Python integer objects implementation

http://www.laurentluce.com/posts/python-integer-objects-implementation/ Python integer objects implementation May 15, 2011 This article describes how integer objects are managed by Python internally. An integer object in Python is represented interna

jquery 插件 起步代码

/** * Created by W.J.Chang on 2014/6/25. */ ;(function($) { var methods= { check: function() { return this.each(function() { this.checked = true; }); } }; $.fn.pager = function(method) { if ( methods[method] ) { return methods[ method ].apply( this,