2009-09-19 2 views
0

나는이 콘솔 응용 프로그램을 작성하여 NCommon을 시험해 보았습니다.NCommon Newbie Help

아래 코드는 아무 것도받지 못합니다. (이것은하여 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에 대한]

나는

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

를 추가하려고 할 때로 더 이상 설정 프록시 팩토리 속성을 추가하지 않은 SessionProvider의 Fluently 문에는 동일한 키의 항목이 이미 추가되었다고합니다.

답변

2

Fluent NHibernate가 인터페이스와 잘 작동하지 않는다는 것을 깨달았습니다. 콘크리트 유형에 매핑되어 작동했습니다.

+0

임씨는 NCommon과 Fluent nhibernate도 고민하고 있습니다. – Todd