NCommon 新手帮助

发布于 2024-08-05 08:41:19 字数 3770 浏览 3 评论 0原文

我正在编写这个控制台应用程序来尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2024-08-12 08:41:19

我意识到 Fluent NHibernate 不能很好地处理界面。我根据具体类型进行了映射并且它起作用了。

I realized that Fluent NHibernate doesn't play well with interfaces. I mapped against concrete types and it worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文