Step 4 从实体设计代码生成实体代码、实体配置文件
4.1 至此,实体的设计就完毕了。编译EntityDesigns工程。下面我们将从设计实体生成实际的实体代码和配置文件。注意,这里和之前的ORM教程不同的是,我们不生成数据库创建脚本,而直接使用一个已经存在的数据库Northwind。
4.2 运行dist目录中的NBear.Tools.EntityDesignToEntity.exe工具,载入EntityDesigns工程编译生成的EntityDesigns.dll。
4.3 点击Generate Entities按钮,将生成的代码保存到Entities工程中的一个名叫Entities.cs的新代码文件。并为Entities工程添加到dist\NBear.Common.Common.dll的引用。
4.4 点击Generate Configuration按钮,将生成的代码保存到website工程下的名为EntityConfig.xml的新文件中。
Step 5 使用实体及NBear.Data.Gateway访问数据库
5.1 现在我们就可以使用前面生成的实体了。我们先要让website工程引用Entities工程,以及dist/NBear.Data.dll。
5.2 我们还需要设置website的Web.config文件,添加一个entityConfig section以包含EntityConfig.xml这个实体配置文件,并设置数据库连接字串。下面是设置完的Web.config,注意,粗体的部分都是我们添加的代码(注意修改数据库登录密码。):
<?xml version="1.0"?> <configuration> <configSections> <section name="entityConfig" type="NBear.Common.EntityConfigurationSection, NBear.Common" /> </configSections> <entityConfig> <includes> <add key="Sample Entity Config" value="~/EntityConfig.xml" /> </includes> </entityConfig> <appSettings/> <connectionStrings> <add name=" Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/> </connectionStrings> <system.web> <compilation debug="false" /> <authentication mode="Windows" /> </system.web> </configuration>
5.3 好了,到目前为止,实体设置和配置完毕了。下面我们将开始讨论IoC模块的使用。
Step 6 定义Service接口和Service实现
6.1 下面我们开始定义一个基于NBear.IoC的Service。我们先要为ServiceInterfaces工程添加到dist\NBear.Common.dll和dist\NBear.IoC.dll的引用。一个Service由一个接口定义。我们这个Service的功能很简单,就是我们想获得一些需要的Category和Product。所以,我们还需要为ServiceInterfaces工程添加到Entities工程的引用。在ServiceInterfaces工程中定义接口ICategoryService和IProductService如下:
using System; using NBear.IoC.Service; using Entities; namespace ServiceInterfaces { public interface ICategoryService : IService { Category[] GetAllCategories(); Category GetCategoryByID(int categoryID); } } using System; using NBear.IoC.Service; using Entities; namespace ServiceInterfaces { public interface IProductService { Product[] GetAllProducts(); Product GetProductByID(int productID); } }
注意,基于NBear.IoC的Service,必须从NBear.IoC.Service.IServiceInterface这个接口继承。
6.2 定义完Service接口,我们还需要实现它。在ServiceImpls工程中,添加到Entities,ServiceInterfaces和到dist\NBear.Common.dll,dist\NBear.Data.dll和dist\NBear.IoC.dll的引用,分别实现这两个接口如下:
using System; using NBear.Common; using NBear.Data; using Entities; using ServiceInterfaces; namespace ServiceImpls { public class CategoryService : ICategoryService { ICategoryService Members#region ICategoryService Members public Category[] GetAllCategories() { return Gateway.Default.FindArray<Category>(WhereClip.All, OrderByClip.Default); } public Category GetCategoryByID(int categoryID) { return Gateway.Default.Find<Category>(categoryID); } #endregion } } using System; using NBear.Common; using NBear.Data; using Entities; using ServiceInterfaces; namespace ServiceImpls { public class ProductService : IProductService { IProductService Members#region IProductService Members public Product[] GetAllProducts() { return Gateway.Default.FindArray<Product>(WhereClip.All, OrderByClip.Default); } public Product GetProductByID(int productID) { return Gateway.Default.Find<Product>(productID); } #endregion } }