Author: marisic.net
(2008/12/23 20:26) About 3 years ago
1
<connectionStrings>
2
<add name="AdventureWorksConnection" connectionString="Integrated Security=SSPI;Persist Security Info=False;Data Source=LocalServer;Initial Catalog=Northwind" providerName="System.Data.SqlClient"/>
<add name="NorthwindConnection" connectionString="Integrated Security=SSPI;Persist Security Info=False;Data Source=LocalServer;Initial Catalog=Northwind" providerName="System.Data.SqlClient"/>
3
</connectionStrings>
#region Using Statements
using System;
4
using System.Diagnostics;
5
6
#endregion
7
8
namespace Domain.Business
18
//public string Suffix { get; set; }
19
public string Phone { get; set; }
20
public string Salutation { get; set; }
21
public string JobTitle { get; set; }
22
}
23
{
public int Id { get; set; }
public string ProductName { get; set; }
public Supplier Supplier { get; set; }
public virtual Supplier Supplier { get; set; }
public Category Category { get; set; }
public virtual Category Category { get; set; }
9
public string QuantityPerUnit { get; set; }
10
public double UnitPrice { get; set; }
11
public int UnitsInStock { get; set; }
namespace Domain.Business.Active
using Domain.Business.Active.Enum;
15
public CustomerMap()
16
17
WithTable("Customers");
Id(x => x.Id, "CustomerID");
base.Id(x => x.Id, "CustomerID");
//non-nullable string with a length of 16
//Map(x => x.).WithLengthOf(16).CanNotBeNull();
//base.Map(x => x.).WithLengthOf(16).CanNotBeNull();
//simple properties
24
Map(x => x.Address).WithLengthOf(15);
base.Map(x => x.Address).WithLengthOf(15);
25
Map(x => x.City).WithLengthOf(60);
base.Map(x => x.City).WithLengthOf(60);
26
Map(x => x.CompanyName).WithLengthOf(40);
base.Map(x => x.CompanyName).WithLengthOf(40);
27
Map(x => x.ContactName).WithLengthOf(30);
base.Map(x => x.ContactName).WithLengthOf(30);
28
Map(x => x.ContactTitle).WithLengthOf(30);
base.Map(x => x.ContactTitle).WithLengthOf(30);
29
Map(x => x.Country).WithLengthOf(15);
base.Map(x => x.Country).WithLengthOf(15);
30
Map(x => x.Fax).WithLengthOf(24);
base.Map(x => x.Fax).WithLengthOf(24);
31
Map(x => x.Phone).WithLengthOf(24);
base.Map(x => x.Phone).WithLengthOf(24);
32
Map(x => x.PostalCode).WithLengthOf(10);
base.Map(x => x.PostalCode).WithLengthOf(10);
33
Map(x => x.Region).WithLengthOf(15);
base.Map(x => x.Region).WithLengthOf(15);
34
35
36
#region IMapGenerator Members
public EmployeeMap()
WithTable("Employees");
Id(x => x.Id, "EmployeeID").GeneratedBy.Identity();
base.Id(x => x.Id, "EmployeeID").GeneratedBy.Identity();
Map(x => x.Salutation, "TitleOfCourtesy").WithLengthOf(25);
base.Map(x => x.Salutation, "TitleOfCourtesy").WithLengthOf(25);
Map(x => x.FirstName).WithLengthOf(10);
base.Map(x => x.FirstName).WithLengthOf(10);
Map(x => x.LastName).WithLengthOf(20);
base.Map(x => x.LastName).WithLengthOf(20);
Map(x => x.JobTitle,"Title").WithLengthOf(30);
base.Map(x => x.JobTitle, "Title").WithLengthOf(30);
// Map(x => x.BirthDate);
// base.Map(x => x.BirthDate);
/* Map(x => x.HireDate);
/* base.Map(x => x.HireDate);
Map(x => x.Phone,"HomePhone").WithLengthOf(24);
base.Map(x => x.Phone,"HomePhone").WithLengthOf(24);
Component<Address>(employee => employee.Address,
address =>
address.Map(a => a.City).WithLengthOf(15);
address.base.Map(a => a.City).WithLengthOf(15);
address.Map(a => a.CountryRegionName).WithLengthOf(15);
address.base.Map(a => a.CountryRegionName).WithLengthOf(15);
37
address.Map(a => a.PostalCode).WithLengthOf(10);
address.base.Map(a => a.PostalCode).WithLengthOf(10);
38
address.Map(a => a.StateProvinceName).WithLengthOf(15);
address.base.Map(a => a.StateProvinceName).WithLengthOf(15);
39
address.Map(a => a.AddressLine1,"Address").WithLengthOf(60);
address.base.Map(a => a.AddressLine1,"Address").WithLengthOf(60);
40
41
); */
42
using System.Xml;
using Domain.Business;
using Domain.Business.Active;
using FluentNHibernate;
using FluentNHibernate.Mapping;
...
public TerritoryMap()
WithTable("Territories");
Id(x => x.Id, "TerritoryID");
base.Id(x => x.Id, "TerritoryID");
SetAttribute("lazy", "false");
base.SetAttribute("lazy", "false");
Map(x => x.Description, "TerritoryDescription");
base.Map(x => x.Description, "TerritoryDescription");
References(x => x.Region, "RegionId")
base.References(x => x.Region, "RegionId")
.Cascade.SaveUpdate();
public class NHibernateSessionManager : INHibernateSessionManager
private static readonly Configuration configuration = MsSqlConfiguration.MsSql2005
.ConnectionString.FromConnectionStringWithKey("AdventureWorksConnection")
.ConnectionString.FromConnectionStringWithKey("NorthwindConnection")
.Dialect("NHibernate.Dialect.MsSql2005Dialect")
.Driver("NHibernate.Driver.SqlClientDriver")
.Provider("NHibernate.Connection.DriverConnectionProvider")
62
.WithConvention(convention => convention.GetTableName = type => type.Name + "s")
63
.WithConvention(convention => convention.GetPrimaryKeyNameFromType = type => type.Name + "ID")
64
.WithConvention(convention => convention.GetForeignKeyName = prop => prop.Name + "ID")
65
.WithConvention(c => c.OneToManyConvention = m =>
66
67
m.Cascade.All();
68
m.LazyLoad();
69
})
.ForTypesThatDeriveFrom<Employee>(autoMap =>
70
71
autoMap.Map(p => p.JobTitle, "Title");
72
74
autoMap.Map(p => p.CompanyName).CanNotBeNull();
79
75
80
76
.ForTypesThatDeriveFrom<Category>(autoMap =>
81
77
78
82
autoMap.WithTable("Categories");
83
autoMap.Map(p => p.Name, "CategoryName");
84
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="UnitTest">
<property name="connection.connection_string_name">AdventureWorksConnection</property>
<property name="connection.connection_string_name">NorthwindConnection</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
_session = sessionManager.GetSession();
#region INHibernateDataProvider Members
#region ICustomerDataProvider Members
IList<Customer> ICustomerDataProvider.GetCustomers()
_session.Flush();
return _session.CreateCriteria(typeof(Customer)).List<Customer>();
return _session.CreateCriteria(typeof (Customer)).List<Customer>();
Customer ICustomerDataProvider.GetCustomer(string id)
return _session.Get<Customer>(id);
void ICustomerDataProvider.AddCustomer(Customer Customer)
#region IEmployeeDataProvider Members
IList<Employee> IEmployeeDataProvider.GetEmployees()
#region Using Statements
using System.Collections.Generic;
namespace Repository.NHibernateDataAccess.Providers.Interfaces
public interface IProductDataProvider
IList<Product> GetProducts();
12
13
Product GetProduct(int id);
14
#region Implementation of IProductDataProvider
public IList<Product> GetProducts()
IList<Product> IProductDataProvider.GetProducts()
return _session.CreateCriteria(typeof (Product)).List<Product>();
return _session.CreateCriteria(typeof (Product))
.SetCacheable(true)
.SetCacheMode(CacheMode.Normal)
.List<Product>();
Product IProductDataProvider.GetProduct(int id)
return _session.Get<Product>(id);
using System.Collections;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
set
IDictionary x = TestContext.Properties;
var x = TestContext.Properties;
_provider = value;
87
[TestMethod]
85
88
public void GetCustomers()
86
89
90
IList<Customer> Customers = _provider.GetCustomers();
var Customers = _provider.GetCustomers();
91
Assert.IsNotNull(Customers);
92
Assert.IsTrue(Customers.Count > 0);
93
Assert.IsFalse(string.IsNullOrEmpty(Customers[0].Id));
98
public void GetCustomer()
96
99
97
100
const string id = "ALFKI";
101
Customer Customer = _provider.GetCustomer(id);
var Customer = _provider.GetCustomer(id);
102
Customer = _provider.GetCustomer(id);
103
104
Assert.IsNotNull(Customer);
using Repository.NHibernateDataAccess;
public void GetEmployees()
IList<Employee> employees = _provider.GetEmployees();
var employees = _provider.GetEmployees();
Assert.IsNotNull(employees);
Assert.IsTrue(employees.Count > 0);
Assert.IsTrue(employees[0].Id > 0);
94
public void GetEmployee()
95
const int id = 9;
Employee employee = _provider.GetEmployee(id);
var employee = _provider.GetEmployee(id);
Assert.IsNotNull(employee);
//Assert.IsTrue(employee.Id.HasValue);
124
122
125
public void GetTerritories()
123
126
127
IList<Territory> territories = _provider.GetTerritories();
var territories = _provider.GetTerritories();
128
129
Assert.IsTrue(territories.Count > 0);
130
Assert.IsFalse(string.IsNullOrEmpty(territories[0].Id));
using NHibernate;
public class NHibernateSessionManagerTest
private static readonly NHibernateSessionManager target = new NHibernateSessionManager();
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
public TestContext TestContext { get; set; }
get { return testContextInstance; }
set { testContextInstance = value; }
#region Additional test attributes
61
public void NHibernateSessionManagerGetSessionTest()
ISession actual = target.GetSession();
var actual = target.GetSession();
Assert.IsNotNull(actual);
73
public void ProductDataProviderGetProducts()
IList<Product> Products = _provider.GetProducts();
var Products = _provider.GetProducts();
Assert.IsNotNull(Products);
Assert.IsTrue(Products.Count > 0);
Assert.IsTrue(Products[0].Id > 0);
Assert.IsTrue(Products[0].Category.Id > 0);
//[TestMethod]
//public void ProductDataProviderGetProduct()
public void ProductDataProviderGetProduct()
//{
// const int id = 9;
// Product Product = _provider.GetProduct(id);
var Product = _provider.GetProduct(id);
// Assert.IsNotNull(Product);
Assert.IsNotNull(Product);
// //Assert.IsTrue(Product.Id.HasValue);
Assert.IsTrue(Product.Id == id);
// Assert.IsTrue(Product.Id == id);
Assert.IsNotNull(Product.Supplier);
// // Assert.IsTrue(!string.IsNullOrEmpty(Product.Address.AddressLine1));
Assert.IsTrue(Product.Supplier.Id > 0);
105
//}
Assert.IsNotNull(Product.Category);
Assert.IsTrue(Product.Category.Id > 0);
106
107
108
//public void ProductDataProviderAddProduct()
<add name="AdventureWorksConnection" connectionString="Integrated Security=SSPI;Persist Security Info=False;Data Source=LocalServer;Initial Catalog=Northwind" providerName="System.Data.SqlClient"/><add name="NorthwindConnection" connectionString="Integrated Security=SSPI;Persist Security Info=False;Data Source=LocalServer;Initial Catalog=Northwind" providerName="System.Data.SqlClient"/>using System;using System.Diagnostics;//public string Suffix { get; set; }//public string Suffix { get; set; }public string Phone { get; set; }public string Phone { get; set; }public string Salutation { get; set; }public string Salutation { get; set; }public string JobTitle { get; set; }public string JobTitle { get; set; }{{public int Id { get; set; }public int Id { get; set; }public string ProductName { get; set; }public string ProductName { get; set; }public Supplier Supplier { get; set; }public virtual Supplier Supplier { get; set; }public Category Category { get; set; }public virtual Category Category { get; set; }public string QuantityPerUnit { get; set; }public string QuantityPerUnit { get; set; }public double UnitPrice { get; set; }public double UnitPrice { get; set; }public int UnitsInStock { get; set; }public int UnitsInStock { get; set; }using System;using System.Diagnostics;using Domain.Business.Active.Enum;{{WithTable("Customers");WithTable("Customers");base.Id(x => x.Id, "CustomerID");//base.Map(x => x.).WithLengthOf(16).CanNotBeNull();base.Map(x => x.Address).WithLengthOf(15);base.Map(x => x.City).WithLengthOf(60);base.Map(x => x.CompanyName).WithLengthOf(40);base.Map(x => x.ContactName).WithLengthOf(30);base.Map(x => x.ContactTitle).WithLengthOf(30);base.Map(x => x.Country).WithLengthOf(15);base.Map(x => x.Fax).WithLengthOf(24);base.Map(x => x.Phone).WithLengthOf(24);base.Map(x => x.PostalCode).WithLengthOf(10);base.Map(x => x.Region).WithLengthOf(15);{{WithTable("Employees");WithTable("Employees");base.Id(x => x.Id, "EmployeeID").GeneratedBy.Identity();//base.Map(x => x.).WithLengthOf(16).CanNotBeNull();base.Map(x => x.Salutation, "TitleOfCourtesy").WithLengthOf(25);base.Map(x => x.FirstName).WithLengthOf(10);base.Map(x => x.LastName).WithLengthOf(20);Map(x => x.JobTitle,"Title").WithLengthOf(30);base.Map(x => x.JobTitle, "Title").WithLengthOf(30);// Map(x => x.BirthDate);// base.Map(x => x.BirthDate);/* Map(x => x.HireDate);/* base.Map(x => x.HireDate);base.Map(x => x.Phone,"HomePhone").WithLengthOf(24);{{address.base.Map(a => a.City).WithLengthOf(15);address.base.Map(a => a.CountryRegionName).WithLengthOf(15);address.base.Map(a => a.PostalCode).WithLengthOf(10);address.base.Map(a => a.StateProvinceName).WithLengthOf(15);address.base.Map(a => a.AddressLine1,"Address").WithLengthOf(60);using Domain.Business;{{WithTable("Territories");WithTable("Territories");base.Id(x => x.Id, "TerritoryID");SetAttribute("lazy", "false");base.SetAttribute("lazy", "false");base.Map(x => x.Description, "TerritoryDescription");base.References(x => x.Region, "RegionId"){{.ConnectionString.FromConnectionStringWithKey("AdventureWorksConnection").ConnectionString.FromConnectionStringWithKey("NorthwindConnection").Dialect("NHibernate.Dialect.MsSql2005Dialect").Dialect("NHibernate.Dialect.MsSql2005Dialect").Driver("NHibernate.Driver.SqlClientDriver").Driver("NHibernate.Driver.SqlClientDriver").Provider("NHibernate.Connection.DriverConnectionProvider").Provider("NHibernate.Connection.DriverConnectionProvider"){{{{{autoMap.WithTable("Categories");autoMap.WithTable("Categories");<property name="connection.connection_string_name">AdventureWorksConnection</property><property name="connection.connection_string_name">NorthwindConnection</property>#region INHibernateDataProvider Members#region ICustomerDataProvider Members{{return _session.CreateCriteria(typeof (Customer)).List<Customer>();{{#region INHibernateDataProvider Members#region IEmployeeDataProvider Members{{{{{{public IList<Product> GetProducts()IList<Product> IProductDataProvider.GetProducts(){{return _session.CreateCriteria(typeof (Product)).List<Product>();.SetCacheable(true).SetCacheMode(CacheMode.Normal).List<Product>();{using System.Collections;using System.Collections.Generic;{{{{IDictionary x = TestContext.Properties;var x = TestContext.Properties;{{IList<Customer> Customers = _provider.GetCustomers();var Customers = _provider.GetCustomers();{{Customer Customer = _provider.GetCustomer(id);var Customer = _provider.GetCustomer(id);using System.Collections.Generic;using Domain.Business.Active;{{IList<Employee> employees = _provider.GetEmployees();var employees = _provider.GetEmployees();{{Employee employee = _provider.GetEmployee(id);var employee = _provider.GetEmployee(id);{{IList<Territory> territories = _provider.GetTerritories();var territories = _provider.GetTerritories();using NHibernate;{{private TestContext testContextInstance;public TestContext TestContext { get; set; }{get { return testContextInstance; }set { testContextInstance = value; }}{{ISession actual = target.GetSession();var actual = target.GetSession();using System.Collections.Generic;using Domain.Business;{{IList<Product> Products = _provider.GetProducts();var Products = _provider.GetProducts();//[TestMethod]//public void ProductDataProviderGetProduct()//{{// const int id = 9;// Product Product = _provider.GetProduct(id);var Product = _provider.GetProduct(id);// Assert.IsNotNull(Product);// //Assert.IsTrue(Product.Id.HasValue);Assert.IsTrue(Product.Id == id);// Assert.IsTrue(Product.Id == id);Assert.IsNotNull(Product.Supplier);// // Assert.IsTrue(!string.IsNullOrEmpty(Product.Address.AddressLine1));Assert.IsTrue(Product.Supplier.Id > 0);//}Assert.IsNotNull(Product.Category);Assert.IsTrue(Product.Category.Id > 0);}<add name="AdventureWorksConnection" connectionString="Integrated Security=SSPI;Persist Security Info=False;Data Source=LocalServer;Initial Catalog=Northwind" providerName="System.Data.SqlClient"/><add name="NorthwindConnection" connectionString="Integrated Security=SSPI;Persist Security Info=False;Data Source=LocalServer;Initial Catalog=Northwind" providerName="System.Data.SqlClient"/>