c# - Using TransactionScope in Service Layer for UnitOfWork operations -
is approach right bundle 3 dataprovider.getxxx methods in transactionscope in service layer unitofwork?
would different?
from transactionscpe ts know concrete connectionstring?
should transaction object connection , pass transaction objekt constructor of transactionscope ?
service layer administrationservice.cs
private list<schoolclass> getadministrationdata() { list<schoolclass> schoolclasses = null using (transactionscope ts = new transactionscope()) { schoolclasses = _admindataprovider.getschoolclasses(); foreach (var s in schoolclasses) { list<pupil> pupils = _admindataprovider.getpupils(s.id); s.pupils = pupils; foreach (var p in pupils) { list<document> documents = _documentdataprovider.getdocuments(p.id); p.documents = documents; } } ts.complete(); } return schoolclasses; }
sample how of 3 methods in dataprovider like:
public list<schoolclass> getschoolclasslist() { // used formerly without transactionscope => using (var trans = dataaccess.connectionmanager.begintransaction()) using (var com = new sqlitecommand(dataaccess.connectionmanager)) { com.commandtext = "select * schoolclass"; var schoolclasses = new list<schoolclass>(); using (var reader = com.executereader()) { schoolclass schoolclass = null; while (reader.read()) { schoolclass = new schoolclass(); schoolclass.schoolclassid = convert.toint32(reader["schoolclassid"]); schoolclass.schoolclasscode = reader["schoolclasscode"].tostring(); schoolclasses.add(schoolclass); } } // used formerly without transactionscope => trans.commit(); return schoolclasses; } }
this looks fine - that's transactionscope
there for, provide transaction control in code (and common pattern uow).
from transactionscpe ts know concrete connectionstring?
it doesn't. depends on data access layer , doesn't mean transactionscope
. transactionscope
create transaction (which default light-weight one) - if data access spans several databases, transaction automatically escalated distributed transaction. uses msdtc under hood.
should transaction object connection , pass transaction objekt constructor of transactionscope ?
no, no, no. see above. doing now. there no harm in nesting transactionscope
s.
Comments
Post a Comment