Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit of Work pattern vs. Transaction Scope for Linq-To-Sql?
    primarykey
    data
    text
    <p>I would like to mimic the Repository approach that is widely used in DDD with Linq-2-Sql in my portal app. So far I have this: </p> <pre><code> public class LinqToSqlDal&lt;DC,T&gt;: IDisposable, IRepository&lt;T&gt; where T: LinqEntity, new(), where DC: DataContext, new() { private DC unitOfWork = null; public LinqToSqlDal(string connectionString) { this.unitOfWork = Activator.CreateInstance(typeof(DC), connectionString) as DC; } public LinqToSqlDal(string connectionString, DataLoadOptions loadOptions): this(connectionString) { this.unitOfWork.LoadOptions = loadOptions; } public virtual void SubmitChanges() { this.unitOfWork.SubmitChanges(); } public virtual List&lt;T&gt; Get(Expression&lt;Func&lt;T,bool&gt;&gt; query) { return this.unitOfWork.GetTable&lt;T&gt;().Where(query); } public virtual void Delete(Expression&lt;Funct&lt;T, bool&gt;&gt; query) { this.unitOfWork.GetTable&lt;T&gt;().DeleteAllOnSubmit(this.unitOfWork.GetTable&lt;T&gt;().Where(query)); } public virtual T GetByID&lt;T&gt;(Expression&lt;Funct&lt;T, bool&gt;&gt; query) { return this.unitOfWork.GetTable&lt;T&gt;().Where(query).SingleOrDefault(); } public virtual object Add(T entity, string IDPropertyName) { this.unitOfWork.GetTable&lt;T&gt;().InsertOnSubmit(entity); this.SubmitChanges(); var ID = (string.IsNullOrEmpty(IDPropertyName)) ? null : entity.GetType().GetProperty(IDPropertyName).GetValue(entity, null); return ID; } public virtual void SubmitChanges() { this.unitOfWork.SubmitChanges(); } public void Dispose() { this.unitOfWork.Dispose(); } } </code></pre> <p>So now I can use this with any Entity and DataContext that entity belongs to. My question is - would passing or instantiating a TransactionScope inside this little repository benefit? So far I have only one DataContext but can have multiple going forward, what can be done to the current design to ensure transactions across multiple data contexts?</p> <p>Is this a good approach to wrap the context with using generics and let the clients dispose of it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload