Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chester89 committed Mar 24, 2010
0 parents commit a7a0423
Show file tree
Hide file tree
Showing 16 changed files with 434 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin
obj
*.csproj.user
*.suo
*.cache
*.swp
20 changes: 20 additions & 0 deletions FluentNHSample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentNHSample", "FluentNHSample\FluentNHSample.csproj", "{16510957-E7E9-49AE-B4FB-289747ED59C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{16510957-E7E9-49AE-B4FB-289747ED59C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{16510957-E7E9-49AE-B4FB-289747ED59C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{16510957-E7E9-49AE-B4FB-289747ED59C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{16510957-E7E9-49AE-B4FB-289747ED59C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions FluentNHSample/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Database" connectionString="Data Source=MESI-K02611584\MSSQL2008;Initial Catalog=FluentNH;Integrated Security=True;"/>
</connectionStrings>
</configuration>
11 changes: 11 additions & 0 deletions FluentNHSample/Conventions/CustomFKConvention.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Reflection;
using FluentNHibernate.Conventions;

namespace FluentNHSample.Conventions {
public class CustomForeignKeyConvention: ForeignKeyConvention {
protected override String GetKeyName(PropertyInfo property, Type type) {
return property == null ? type.Name + "Id": property.Name + "Id";
}
}
}
15 changes: 15 additions & 0 deletions FluentNHSample/Conventions/CustomManyToManyConvention.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions;

namespace FluentNHSample.Conventions {
public class CustomManyToManyConvention : ManyToManyTableNameConvention {
protected override String GetBiDirectionalTableName(IManyToManyCollectionInspector collection, IManyToManyCollectionInspector otherSide) {
return collection.EntityType.Name + otherSide.EntityType.Name;
}

protected override String GetUniDirectionalTableName(IManyToManyCollectionInspector collection) {
throw new NotImplementedException();
}
}
}
13 changes: 13 additions & 0 deletions FluentNHSample/Entities/Employee.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FluentNHSample.Entities {
public class Employee {
public virtual Int32 Id { get; private set; }
public virtual String FirstName { get; set; }
public virtual String LastName { get; set; }
public virtual Store Store { get; set; }
}
}
17 changes: 17 additions & 0 deletions FluentNHSample/Entities/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FluentNHSample.Entities {
public class Product {
public virtual Int32 Id { get; private set; }
public virtual String Name { get; set; }
public virtual Double Price { get; set; }
public virtual IList<Store> StoresStockedIn { get; private set; }

public Product() {
StoresStockedIn = new List<Store>();
}
}
}
28 changes: 28 additions & 0 deletions FluentNHSample/Entities/Store.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FluentNHSample.Entities {
public class Store {
public virtual Int32 Id { get; private set; }
public virtual String Name { get; set; }
public virtual IList<Product> Products { get; set; }
public virtual IList<Employee> Staff { get; set; }

public Store() {
Products = new List<Product>();
Staff = new List<Employee>();
}

public virtual void addProduct(Product product) {
product.StoresStockedIn.Add(this);
Products.Add(product);
}

public virtual void addEmployee(Employee employee) {
employee.Store = this;
Staff.Add(employee);
}
}
}
102 changes: 102 additions & 0 deletions FluentNHSample/FluentNHSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{16510957-E7E9-49AE-B4FB-289747ED59C8}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FluentNHSample</RootNamespace>
<AssemblyName>FluentNHSample</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=1.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\dev\.NET 3rd party assemblies\NHibernate\Castle.Core.dll</HintPath>
</Reference>
<Reference Include="Castle.DynamicProxy2, Version=2.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\dev\.NET 3rd party assemblies\NHibernate\Castle.DynamicProxy2.dll</HintPath>
</Reference>
<Reference Include="FluentNHibernate, Version=1.0.0.593, Culture=neutral, PublicKeyToken=8aa435e3cb308880, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\dev\.NET 3rd party assemblies\FluentNhibernate\FluentNHibernate.dll</HintPath>
</Reference>
<Reference Include="LinFu.DynamicProxy, Version=1.0.3.14911, Culture=neutral, PublicKeyToken=62a6874124340d6e, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\dev\.NET 3rd party assemblies\NHibernate\LinFu.DynamicProxy.dll</HintPath>
</Reference>
<Reference Include="NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\dev\.NET 3rd party assemblies\NHibernate\NHibernate.dll</HintPath>
</Reference>
<Reference Include="NHibernate.ByteCode.Castle, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\dev\.NET 3rd party assemblies\NHibernate\NHibernate.ByteCode.Castle.dll</HintPath>
</Reference>
<Reference Include="NHibernate.ByteCode.LinFu, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\dev\.NET 3rd party assemblies\NHibernate\NHibernate.ByteCode.LinFu.dll</HintPath>
</Reference>
<Reference Include="NHibernate.Linq, Version=1.0.0.4000, Culture=neutral, PublicKeyToken=444cf6a87fdab271, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\dev\.NET 3rd party assemblies\NHibernate\NHibernate.Linq.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Conventions\CustomFKConvention.cs" />
<Compile Include="Conventions\CustomManyToManyConvention.cs" />
<Compile Include="Entities\Employee.cs" />
<Compile Include="Entities\Product.cs" />
<Compile Include="Entities\Store.cs" />
<Compile Include="Mappings\EmployeeMap.cs" />
<Compile Include="Mappings\ProductMap.cs" />
<Compile Include="Mappings\StoreMap.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
17 changes: 17 additions & 0 deletions FluentNHSample/Mappings/EmployeeMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHSample.Entities;
using FluentNHibernate.Mapping;

namespace FluentNHSample.Mappings {
public class EmployeeMap: ClassMap<Employee> {
public EmployeeMap() {
Id(x => x.Id);
Map(x => x.FirstName);
Map(x => x.LastName);
References(x => x.Store);
}
}
}
17 changes: 17 additions & 0 deletions FluentNHSample/Mappings/ProductMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHSample.Entities;
using FluentNHibernate.Mapping;

namespace FluentNHSample.Mappings {
public class ProductMap: ClassMap<Product> {
public ProductMap() {
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Price);
HasManyToMany(x => x.StoresStockedIn).Cascade.All().Inverse();
}
}
}
17 changes: 17 additions & 0 deletions FluentNHSample/Mappings/StoreMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHSample.Entities;
using FluentNHibernate.Mapping;

namespace FluentNHSample.Mappings {
public class StoreMap: ClassMap<Store> {
public StoreMap() {
Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.Staff).Inverse().Cascade.All();
HasManyToMany(x => x.Products).Cascade.All();
}
}
}
92 changes: 92 additions & 0 deletions FluentNHSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHSample.Entities;
using NHibernate;
using FluentNHSample.Conventions;
using FluentNHibernate.Conventions.Helpers;

namespace FluentNHSample {
class Program {
static void Main(String[] args) {
ISessionFactory sessionFactory = null;
try {
sessionFactory = CreateSessionFactory();
}
catch (Exception e) {
Console.WriteLine("Exception occurred: " + e.Message);
}
Console.WriteLine("Session factory was built");

using (var session = sessionFactory.OpenSession()) {
using (var transaction = session.BeginTransaction()) {
// create a couple of Stores each with some Products and Employees
var barginBasin = new Store { Name = "Bargin Basin" };
var superMart = new Store { Name = "SuperMart" };

var potatoes = new Product { Name = "Potatoes", Price = 3.60 };
var fish = new Product { Name = "Fish", Price = 4.49 };
var milk = new Product { Name = "Milk", Price = 0.79 };
var bread = new Product { Name = "Bread", Price = 1.29 };
var cheese = new Product { Name = "Cheese", Price = 2.10 };
var waffles = new Product { Name = "Waffles", Price = 2.41 };

var daisy = new Employee { FirstName = "Daisy", LastName = "Harrison" };
var jack = new Employee { FirstName = "Jack", LastName = "Torrance" };
var sue = new Employee { FirstName = "Sue", LastName = "Walkters" };
var bill = new Employee { FirstName = "Bill", LastName = "Taft" };
var joan = new Employee { FirstName = "Joan", LastName = "Pope" };

// add products to the stores, there's some crossover in the products in each
// store, because the store-product relationship is many-to-many
AddProductsToStore(barginBasin, potatoes, fish, milk, bread, cheese);
AddProductsToStore(superMart, bread, cheese, waffles);

// add employees to the stores, this relationship is a one-to-many, so one
// employee can only work at one store at a time
AddEmployeesToStore(barginBasin, daisy, jack, sue);
AddEmployeesToStore(superMart, bill, joan);

// save both stores, this saves everything else via cascading
session.SaveOrUpdate(barginBasin);
session.SaveOrUpdate(superMart);

transaction.Commit();
}

// retreive all stores and display them
using (session.BeginTransaction()) {
var stores = session.CreateCriteria<Store>().List<Store>();

foreach (Store store in stores)
Console.WriteLine("id: {0} name: {1} employee count: {2}", store.Id, store.Name, store.Staff.Count);
}

Console.WriteLine("Press any key to quit");
Console.ReadKey();
}
}

private static ISessionFactory CreateSessionFactory() {
//ConventionBuilder.Id.Always(x => x.Column("Id"));
Console.WriteLine("Building session factory");
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("Database"))).
Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Program>()
.Conventions.AddFromAssemblyOf<CustomForeignKeyConvention>()
)
.BuildSessionFactory();
}

public static void AddProductsToStore(Store store, params Product[] products) {
foreach (var product in products)
store.addProduct(product);
}

public static void AddEmployeesToStore(Store store, params Employee[] employees) {
foreach (var employee in employees)
store.addEmployee(employee);
}
}
}
Loading

0 comments on commit a7a0423

Please sign in to comment.