c# - Adding to mapped collection in NHibernate - transaction concern -
c# - Adding to mapped collection in NHibernate - transaction concern -
i utilize nhibernate fluent configuration , have simple entity called administrator
:
public class administrator : entity { public virtual icollection<administratorclientassociation> clientsassociation {get; protected set; } ... public virtual void addclient(client newclient) { var clientassociation = new administratorclientassociation() { associationdate = datetime.now, client = newclient, clientowner = }; clientsassociation.add(clientassociation); } }
collection clientsassociation
1:n
relation mapped foreign key (clientowner
) , has set cascade.onsaveupdate
. question how nhibernate
deals transactional concers in such situation? i'd create transaction in i'd add together client
, , administratorclientassociation
. question - should wrap transaction everywhere phone call addclient
(cause injecting session entity looks horrible) or done automatically in case of mapped, cascaded collection?
if nhibernate flushes relations, in same transaction.
eventhough lot of people claim mutual utilize transaction around entire request, wrong.
all code between start of transaction , end of it, should considered atomic operation. either of succeeds or fails.
if seek add together 2 clients, , sec 1 fails, should first 1 stored anyway? if case, adding client atomic operation , transaction should around piece of code. if none of clients should stored when single 1 fails, transaction should around batch.
the sec question should inquire responsible managing transactions. there no 1 reply question. depends on how application works. example, if utilize cqrs, transaction should managed component responsible executing commands.
other architectures , requirements demand other solutions.
c# nhibernate
Comments
Post a Comment