0

我正在编写这个控制台应用程序来试用 NCommon。

下面的代码没有让我得到任何回报。(这是使用 AdventureWorks 数据库。)

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 的代码]

当我尝试添加时,我不再添加代理工厂属性设置

            .ExposeConfiguration(cfg =>
                {
                    cfg.Properties.Add("proxyfactory.factory_class",
                              "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
                })

在 SessionProvider 的 Fluently 语句中,它表示已经添加了相同键的项。

4

1 回答 1

2

我意识到 Fluent NHibernate 不能很好地处理接口。我针对具体类型进行了映射并且它起作用了。

于 2009-09-19T02:54:40.857 回答