NCommon 新手帮助
我正在编写这个控制台应用程序来尝试 NCommon。
下面的代码没有给我任何回报。 (这是使用 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();
}
}
[不包括 POCO SalesOrderHeader 和 ISalesOrderHeader 的代码]
我不再添加 Proxy Factory 属性设置,因为当我尝试添加
.ExposeConfiguration(cfg =>
{
cfg.Properties.Add("proxyfactory.factory_class",
"NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
})
到 SessionProvider 的 Fluently 语句时,它说相同键的项目已经被添加额外。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我意识到 Fluent NHibernate 不能很好地处理界面。我根据具体类型进行了映射并且它起作用了。
I realized that Fluent NHibernate doesn't play well with interfaces. I mapped against concrete types and it worked.