IntroductionTo learn more about Smart Code please read the next article, by clicking here. The sample code and templates, are based in the next article NHibernate Best Practices with ASP.NET . In this excellent article, Billy McCafferty, describes best practices for leveraging the benefits of NHibernate 1.2, ASP.NET. For this article we'll try to be original.....and we'll use the Northwind database as sample, but these examples will work with any database Code Generation, a brief overviewThe Smart Code tool makes possible the automatic generation of programs and components that interact with database systems. It works by reading a database schema, allowing the user to set certain extended properties for tables and columns, and applying templates to each table in order to generate code. The templates are themselves programs that access the Smart Code Object Model to produce tailored programs and components. Although Smart Code comes with a set of basic templates, users may edit these or create their own to suit their particular project needs. Moreover, templates may be written in C# or VB.NET (or theoretically in any .NET language that supports the creation of dynamic-link libraries). Quick Start GuideSmart Code is a very powerful tool, and perhaps the best way to learn how to use it is to see a sample application running and then go back and examine which features of Smart Code were responsible for the application's functionality.
The NHibernate Template LibrariesThe Sample package comes with one set of template libraries for generating 3- tier, web-form-based solutions that use NHibernate to access and modify data. This set of libraries is for generating code in C#; (future releases will support generating code in VB.NET). In addition, the Sample package includes sample Northwind project in Visual Studio.NET to work with these sets of libraries, so that the generated code may simply be dragged-and-dropped into them. The Domain LayerFor a description about the domain layer, click here The Domain layer describing the data access objects and the classes of objects used to transport data between the tiers Generating the Separated Interface, IDAOFactoryThe IDAOFactory Interface provide methods to get objects as DAOs. This class serves as the abstract data. The template to generate the IDAOFactory for all entities in the Smart Code project is the IDaoFactory class in the NHibernateTemplates project. Notice, if you check the Item it Template run only one time, because it is an project level template. The Code Generated for the IDAOFactory look like this: using System;
using System.Collections.Generic;
namespace Northwind.Core.DataInterfaces
{
public interface IDaoFactory
{
ICategoryDao GetCategoryDao();
ICustomerCustomerDemoDao GetCustomerCustomerDemoDao();
ICustomerDemographicDao GetCustomerDemographicDao();
ICustomerDao GetCustomerDao();
IEmployeeDao GetEmployeeDao();
IEmployeeTerritoryDao GetEmployeeTerritoryDao();
IOrderDetailDao GetOrderDetailDao();
IOrderDao GetOrderDao();
IProductDao GetProductDao();
IRegionDao GetRegionDao();
IShipperDao GetShipperDao();
ISupplierDao GetSupplierDao();
ITerritoryDao GetTerritoryDao();
}
}
The Data InterfacesThe template DataInterfaces generate code for supporting each one concrete DAO factory. Each factory is an is a database-independent implementation, where the implementations will be database specific. The Template used to generate the DataInterfaces is shwon in the next figure: And the code generated, for the Product Table look like this using System;
using System.Collections.Generic;
using Northwind.Core.Domain;
namespace Northwind.Core.DataInterfaces
{
/// Since this extends the IDao{TypeOfListItem, IdT} behavior, it's a good idea to
/// place it in its own file for manageability. In this way, it can grow further without
/// cluttering up IDaoFactory.
public interface IProductDao : IDao<Product, System.Int32>
{
}
}
Mapping the Domain to the Database, HBM-XML filesThe NHibernateTemplates project provide a class NHibernateHbm, to generate mapping files for NHibernate This Template generate the HBM files, for tables with single and composite Key and linking tables which will be recognised and the appropriate many-to-one or many-to-many mapping created Hbm for Table with single key:
About Danilo Mendez |
相关文章: