2013-10-23 2 views
1

나는 NHibernate와 Ninject를 사용하는 ASP.NET MVC 애플리케이션을 개발 중이다.NHibernate 잘못된 인덱스 예외

문제는 다음과 같은 컨트롤러에 의해 발생 :

public class ShoppingCartController : Controller 
{ 
    private readonly Data.Infrastructure.IShoppingCartRepository _shoppingCartRepository; 
    private readonly Data.Infrastructure.IShopItemRepository _shopItemRepository; 

    public ShoppingCartController(Data.Infrastructure.IShoppingCartRepository shoppingCartController, 
     Data.Infrastructure.IShopItemRepository shopItemRepository) 
    { 
     _shoppingCartRepository = shoppingCartController; 
     _shopItemRepository = shopItemRepository; 
    } 

    public ActionResult AddToShoppingCart(FormCollection formCollection) 
    { 
     var cartItem = new Data.Models.ShoppingCartItem(); 
     cartItem.ChangeDate = DateTime.Now; 
     cartItem.ShopItem = _shopItemRepository.GetShopItem(SessionData.Data.Info, Convert.ToInt32(formCollection["shopItemId"])); 
     //IF I DONT´T CALL THE METHOD ABOVE, AddToCart works 

     _shoppingCartRepository.AddToCart(SessionData.Data.Info, cartItem); 
     //BUT IF I CALL THE GetShopItem METHOD I GET THE EXCEPTION HERE! 
     return RedirectToAction("Index", "Shop"); 
    } 
} 

나는이 예외가 잘못된 매핑에 의해 발생 시간의 대부분을 알고 있지만, 미안 꽤 내 매핑해야합니다 것을 바로 AddToCart와-방법 때문에 내가 GetShopItem를 호출하지는한다면 ... 여기

작동되는 ShopItemRepository의 코드 :

public class ShopItemRepository : ReadOnlyRepository<ShopItem>, IShopItemRepository 
{ 
    public ShopItemRepository(IUnitOfWork uow) : base(uow) 
    { 

    } 

    public ShopItem GetShopItem(SessionParams param, int id) 
    { 
     return CurrentSession.QueryOver<ShopItem>() 
         .Where(x => x.ProcessId == param.ProcessId && 
            x.CatalogueId == param.CatalogueId && 
            x.Id == id) 
         .SingleOrDefault(); 
    } 

    public IList<ShopItem> GetShopItems(SessionParams param) 
    { 
     return CurrentSession.GetNamedQuery("GetShopItems") 
         .SetParameter("requestor_id", param.RequestorId) 
         .SetParameter("recipient_id", param.RecipientId) 
         .SetParameter("process_id", param.ProcessId) 
         .SetParameter("catalogue_id", param.CatalogueId) 
         .List<ShopItem>(); 
    } 
} 

그리고 내의 UnitOfWork의 마지막 코드 (기본적으로 내가 내가) 내 MVC 프로젝트에 자 NHibernate를 참조 할하지는 때문에 세션을 위해 단지 래퍼이야

public class UnitOfWork : IUnitOfWork, IDisposable 
{ 
    private NHibernate.ISession _currentSession; 
    public NHibernate.ISession CurrentSession 
    { 
     get 
     { 
      if(_currentSession == null) 
      { 
       _currentSession = SessionFactoryWrapper.SessionFactory.OpenSession(); 
      } 
      return _currentSession; 
     } 
    } 

    public void Dispose() 
    { 
     if(_currentSession != null) 
     { 
      _currentSession.Close(); 
      _currentSession.Dispose(); 
      _currentSession = null; 
     } 
     GC.SuppressFinalize(this); 
    } 
} 

부록 :
내 NinjectWebCommon 클래스 IUnitOfWork가 너무 RequestScope로 설정되어

public static class NinjectWebCommon 
{ 
    private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

    /// <summary> 
    /// Starts the application 
    /// </summary> 
    public static void Start() 
    { 
     DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); 
     DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); 
     bootstrapper.Initialize(CreateKernel); 
    } 

    /// <summary> 
    /// Stops the application. 
    /// </summary> 
    public static void Stop() 
    { 
     bootstrapper.ShutDown(); 
    } 

    /// <summary> 
    /// Creates the kernel that will manage your application. 
    /// </summary> 
    /// <returns>The created kernel.</returns> 
    private static IKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
     kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

     RegisterServices(kernel); 
     return kernel; 
    } 

    /// <summary> 
    /// Load your modules or register your services here! 
    /// </summary> 
    /// <param name="kernel">The kernel.</param> 
    private static void RegisterServices(IKernel kernel) 
    { 
     kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope(); 

     kernel.Bind<Data.Infrastructure.ICatalogueRepository>().To<Data.Repositories.CatalogueRepository>(); 
     kernel.Bind<Data.Infrastructure.ICategoryRepository>().To<Data.Repositories.CategoryRepository>(); 
     kernel.Bind<Data.Infrastructure.IContactRepository>().To<Data.Repositories.ContactRepository>(); 
     kernel.Bind<Data.Infrastructure.IProcessRepository>().To<Data.Repositories.ProcessRepository>(); 
     kernel.Bind<Data.Infrastructure.IShopItemRepository>().To<Data.Repositories.ShopItemRepository>(); 
     kernel.Bind<Data.Infrastructure.IShoppingCartRepository>().To<Data.Repositories.ShoppingCartRepository>(); 
    }   
} 

ShoppingCartController의 경우 두 저장소가 동일한 UOW 권한을 공유합니까?

아마도이 문제가 발생할 수 있습니까?

+0

"문제가 발생할 수 있습니까?" 어쩌면 두 저장소를 병합하여 확인할 수 있습니다. 'ReadOnlyRepository '도 정확히 무엇입니까 (이 이름은 수수께끼인가요?). – jbl

답변

1

잘못된 매핑으로 인해 발생하지 않았습니까? 나는 같은 문제를 가지고 있었고 나의 매핑을 다시 점검함으로써 그것을 해결할 수 있었다!

+0

네, 이것이 문제였습니다. 며칠 전에 해결했고 내 질문을 업데이트하는 것을 잊었습니다 ... 어쨌든 고마워요. – makim