views:

116

answers:

5

hi. i use nhibernate. i got a Customer and Customer got a IList.. now when i add a new Customer and CustomerUser i do like this.

var customer = new Customer {
    Name = txtCustomerName.Text,
    OrgNumber = txtOrgNumber.Text
};
var customerUser = new CustomerUser {
    Email = txtUserMail.Text,
    Password = password.Sha1()
};

customer.CustomerUsers.Add(customerUser);

Now i want to update the customerUser but how should i do that?

A: 

Are you asking how you can save your changes? If so you need to use the flush keyword.

Jason Irwin
i am not sure what you mean with flush the keyword here? I want to update the customerUser, like change the Email to it or so. Do i need to get the customerUser by id or is it possible to get the customer by id and update the customerUser throught there, if you know what i mean?
Dejan.S
A: 

i am not sure what you mean with flush the keyword here? I want to update the customerUser, like change the Email to it or so. Do i need to get the customerUser by id or is it possible to get the customer by id and update the customerUser throught there, if you know what i mean?

Dejan.S
A: 

Use the code below. If the Id equal the unsaved-value (in your mapping file) an INSERT is done if the Id is not equal the the unsaved-value an UPDATE is done. Of course, this collection is may be not saved, if the setting in the mapping file is not correct.

using (ITransaction tx = session.BeginTransaction())
{
    try
    {
     session.SaveOrUpdate(customer);
     tx.Commit();

    }
    catch (NHibernate.HibernateException ex)
    {
     tx.Rollback();
     throw new Exception(ex.Message);
    }
}
Kris-I
A: 

thx for reply Kris.I

i get that part its ok. before i used to have a repository for all my classes, now i only use one for customer eaven if customer got 4list(that would mean 4 repositories). so what i used to do is i got the CustomerUser by id and updated it throught its own repository.

like

var customerUser = objectFactory.Getinstance<CustomerUserService>().GetById(4);
customerUser.Email = email;
customerUser.Password = password;

objectFactory.GetInstance<CustomerUserService>().SaveOrUpdate(customerUser);

now i only got the customer repository. so when i add a customerUser to the customer i do like

Customer.CustomerUser.Add(customerUser);

so i want code so i can update the customerUser in kinda the same way if you know what i mean? something like

var customer = objectFactory.Getinstance<CustomerService>().GetById(4);
customer.customerUser.where(i => i.Id == id);

but im not sure how this is done correctly.. hope i been clear on how i want it to be done

Dejan.S
If I understand you don't want the Customer repository manage (Save/update) the CustomerUser collection right ? Then you can use the code I gave you to save the CustomerUser, not all the collection but the one return by your first line. You do session.SaveOrUpdate(customerUser).
Kris-I
A: 

No i want the Customer repository to handle the Save/Update. lets say i got this code

var customer = objectFactory.Getinstance<CustomerService>().GetById(4);
var customerUser = Customer.CustomerUser.where(i => i.Id == id);

the customerUser is a IEnumerable. so i cant access the properties to it like email and password i only get count an so. so thats what i want to know, that middle step how i should do it.. cause at the end and update i thought i do like this

customer.CustomerUser.Add(customerUser)
objectFactory.Getinstance<CustomerService>().SaveOrUpdate(customer)
Dejan.S
you can access to email property :customer.CustomerUser[0].Email = " .... ";
Kris-I