コード例 #1
0
        public void AddPaymentMethodWithNhibernate()
        {
            ISession session = ServiceLocator.Factory.OpenSession();

            var method = new Domain.CreditCardPaymentMethod { RegistrationDate = DateTime.Now, Status = 2, CardHolderName = "Roman Jendrusz", CardNumber = "1234", CardType = "Visa" };
            var method2 = new Domain.DirectDebitPaymentMethod
                              {
                                  RegistrationDate = DateTime.Now,
                                  Status = 1,
                                  BankAccountNumber = "123123",
                                  HolderName = "sciemniacz"
                              };

            var customer = new Domain.Customer{Name = "Roman"};
            method.AssignedTo =  customer;
            method2.AssignedTo = customer;
            customer.UsedPaymentMethod = method2;
            var customer2 = new Domain.Customer { Name = "Marcin" };
            customer2.UsedPaymentMethod = method2;
            using(ITransaction transaction = session.BeginTransaction())
            {
                session.Save(customer);
                session.Save(method);
                session.Save(method2);
                transaction.Commit();
            }
            //IList<Domain.PaymentMethod> methods = session.CreateQuery("from PaymentMethod pm where pm.AssignedTo = :customer")
            //    .SetParameter("customer", customer)
            //    .List<Domain.PaymentMethod>();
            session = ServiceLocator.Factory.OpenSession();
            IList<Domain.PaymentMethod> methods =
                session.Linq<Domain.PaymentMethod>().Where(pm => pm.AssignedTo == customer).ToList();
            foreach (var paymentMethod in methods)
            {
                Console.WriteLine(paymentMethod.GetType().ToString());
            }
        }
コード例 #2
0
ファイル: RelationshipTests.cs プロジェクト: jenrom/Spikes
 public void ShouldRetrieveAllCustomerRelatioinshipsWithAPerformantQuery_NH()
 {
     using (new TransactionScope())
     {
         ISession session = ServiceLocator.Factory.OpenSession();
         var customer = new Domain.Customer {Name = "Test Customer"};
         var paymentMethod = new Domain.DirectDebitPaymentMethod
                                 {
                                     AssignedTo = customer,
                                     BankAccountNumber = "1234",
                                     HolderName = "Test Customer",
                                     RegistrationDate = DateTime.Today,
                                     Status = 1
                                 };
         customer.UsedPaymentMethod = paymentMethod;
         customer.Services = new HashSet<Domain.Service>();
         customer.Services.Add(new Domain.Service {BoughtBy = customer, MonthlyFee = 20, Name = "Voip"});
         using (ITransaction transaction = session.BeginTransaction())
         {
             session.Save(customer);
             session.Save(paymentMethod);
             transaction.Commit();
         }
         session.Clear();
         IEnumerable<Domain.Customer> customers = session.CreateCriteria(typeof(Domain.Customer))
             .SetFetchMode("Services", FetchMode.Eager)
             .Add(Expression.Eq("Name", "Test Customer")).Future<Domain.Customer>();
         session.CreateCriteria(typeof(Domain.Customer))
             .SetFetchMode("UsedPaymentMethod", FetchMode.Eager)
             .Add(Expression.Eq("Name", "Test Customer")).Future<Domain.Customer>();
         customers = customers.ToList();
         Assert.That(customers, Has.Count.EqualTo(1));
         Assert.That(customer.Services, Has.Count.EqualTo(1));
         Assert.That(customer.UsedPaymentMethod, Is.Not.Null);
     }
 }