I'm writing this Console app to try out NCommon.
The code below doesn't get me anything back. (This is using AdventureWorks db.)
class Program
{
static void Main(string[] args)
{
ISessionFactory factory = SessionProvider.CreateSessionFactory();
Store.Application.Set("NHibernateSessionFactory", factory);
NHUnitOfWorkFactory.SetSessionProvider(GetSession);
ConfigureContainer();
using (ISession session = factory.OpenSession())
{
IRepository<SalesOrderHeader> orderRepository = new NHRepository<SalesOrderHeader>(session);
using (var scope = new UnitOfWorkScope())
{
List<SalesOrderHeader> orders = new List<SalesOrderHeader>();
orders = (from order in orderRepository
select order).ToList();
foreach (var order in orders)
{
Console.WriteLine(order.DueDate);
}
}
}
Console.WriteLine("[Done]");
Console.ReadLine();
}
/// <summary>
/// Configure the Windsor container.
/// </summary>
private static void ConfigureContainer()
{
var container = new WindsorContainer();
var currentAssembly = typeof(Program).Assembly;
//Register the NHibernate unit of work and repository components
container.Register(Component.For<IUnitOfWorkFactory>().ImplementedBy<NHUnitOfWorkFactory>().LifeStyle.Transient)
.Register(Component.For(typeof(IRepository<>)).ImplementedBy(typeof(NHRepository<>)).LifeStyle.Transient);
//Auto register all service implementations
container.Register(AllTypes.FromAssembly(currentAssembly)
.Where(type => type.Namespace.EndsWith("Services"))
.WithService.FirstInterface()
.Configure(x => x.LifeStyle.Transient));
Store.Application.Set("ApplicationContainer", container);
ServiceLocator.SetLocatorProvider
(
() => new WindsorServiceLocator(Store.Application.Get<IWindsorContainer>("ApplicationContainer"))
);
}
private static ISession GetSession()
{
return Store.Application.Get<ISessionFactory>("NHibernateSessionFactory").OpenSession();
}
}
public class SessionProvider
{
public static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(@"Data Source=MYLAPTOP\SQL2008;Initial Catalog=AdventureWorks;Integrated Security=True;"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SalesOrderHeader>())
.BuildSessionFactory();
}
}
public class ISalesOrderHeaderMap : ClassMap<ISalesOrderHeader>
{
public ISalesOrderHeaderMap()
{
Table("Sales.SalesOrderHeader");
Id(x => x.Id);
References(x => x.Customer);
Map(x => x.OrderDate);
Map(x => x.DueDate);
Map(x => x.ShipDate);
Map(x => x.Status);
HasMany(x => x.Details)
.Inverse()
.Cascade.All();
}
}
[excluding code for POCO SalesOrderHeader and ISalesOrderHeader]
I didn't add Proxy Factory property setting anymore as when I tried adding
.ExposeConfiguration(cfg =>
{
cfg.Properties.Add("proxyfactory.factory_class",
"NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
})
to the SessionProvider's Fluently statement, it said that an item of the same key has already been added.