Skip to content

Instantly share code, notes, and snippets.

@alextrofymenko
Created January 5, 2017 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alextrofymenko/ada9819f92c8e1a8ef5531798d7ed829 to your computer and use it in GitHub Desktop.
Save alextrofymenko/ada9819f92c8e1a8ef5531798d7ed829 to your computer and use it in GitHub Desktop.
Casting to a class without using a transformer
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentNHibernate" version="2.0.3.0" targetFramework="net452" />
<package id="Iesi.Collections" version="4.0.0.4000" targetFramework="net452" />
<package id="NHibernate" version="4.1.0.4000" targetFramework="net452" />
<package id="System.Data.SQLite.Core" version="1.0.104.0" targetFramework="net452" />
</packages>
using System;
using System.Collections.Generic;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Tool.hbm2ddl;
namespace NhCastWithoutTransformer
{
public class MyClassB
{
public string Name { get; set; }
public string Address { get; set; }
public MyClassB(string name, string address)
{
this.Name = name;
this.Address = address;
}
public override string ToString()
{
return String.Format("Name: {0}, Address: {1}", this.Name, this.Address);
}
}
public class MyTable
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Address { get; set; }
}
public class MyTableMap : ClassMap<MyTable>
{
public MyTableMap()
{
this.Id(x => x.Id)
.GeneratedBy.Native();
this.Map(x => x.Name);
this.Map(x => x.Address);
}
}
public class Program
{
static void Main(string[] args)
{
Configuration _configuration = null;
FluentConfiguration fluentConfig = Fluently.Configure()
.Database(SQLiteConfiguration.Standard
.ConnectionString("data source=:memory:")
.Driver<SQLite20Driver>()
.Dialect<SQLiteDialect>())
.Mappings(m => m.FluentMappings.Add<MyTableMap>())
.ExposeConfiguration(c =>
{
c.SetProperty(NHibernate.Cfg.Environment.ReleaseConnections, "on_close");
c.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, "thread_static");
c.Imports.Add("MyClassB", "NhCastWithoutTransformer.MyClassB, NhCastWithoutTransformer");
_configuration = c;
});
using (var sessionFactory = fluentConfig.BuildSessionFactory())
using (var session = sessionFactory.OpenSession())
{
new SchemaExport(_configuration).Execute(false, true, false, session.Connection, null);
session.Save(new MyTable { Name = "AName", Address = "AAddress" });
session.Save(new MyTable { Name = "BName", Address = "BAddress" });
IList<MyClassB> classListB = session.CreateQuery("SELECT new MyClassB(t.Name, t.Address) FROM MyTable t WHERE t.Name='AName'").List<MyClassB>();
foreach (var classB in classListB)
{
Console.WriteLine(classB);
}
Console.ReadLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment