NCommon新手帮助

时间:2009-09-19 01:16:40

标签: fluent-nhibernate ncommon

我正在编写此控制台应用程序以试用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的代码]

当我尝试添加

时,我没有添加Proxy Factory属性设置
            .ExposeConfiguration(cfg =>
                {
                    cfg.Properties.Add("proxyfactory.factory_class",
                              "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
                })

到SessionProvider的Fluently语句,它说已经添加了一个相同键的项目。

1 个答案:

答案 0 :(得分:2)

我意识到Fluent NHibernate在接口方面表现不佳。我根据具体类型进行了映射,并且有效。