The structure of the work model and transaction volume for Linq-To-Sql?

I would like to emulate the Repository approach, which is widely used in DDD with Linq-2-Sql in my portal application. So far I have this:

     public class LinqToSqlDal<DC,T>: IDisposable, IRepository<T> 
        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<T> Get(Expression<Func<T,bool>> query) 
              {
                   return this.unitOfWork.GetTable<T>().Where(query);
              }

              public virtual void Delete(Expression<Funct<T, bool>> query) 
              {
                   this.unitOfWork.GetTable<T>().DeleteAllOnSubmit(this.unitOfWork.GetTable<T>().Where(query));
              }

              public virtual T GetByID<T>(Expression<Funct<T, bool>> query) 
              {
                     return this.unitOfWork.GetTable<T>().Where(query).SingleOrDefault();
              }

              public virtual object Add(T entity, string IDPropertyName) 
              {
                this.unitOfWork.GetTable<T>().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();
              }


     }

So now I can use this with any Entity and DataContext to which the object belongs. My question is: Would TransactionScope transmit or broadcast inside this small repository? So far I have only one DataContext, but I have several options for moving forward, what can be done for the current design to ensure transactions in multiple data contexts?

Is it a good approach to wrap generic contexts and let customers dispose of them?

+3
1

- , . UnitOfWork, , , .

. , unitOfWork DataContext Linq2SQL. , UnitOfWork . , , , , .

UnitOfWork ; - , . UnitsOfWork DataContext Context UnitOfWork , ( ). UnitOfWork , DataContext. , , - . , Linq2SQL , NHibernate, ; , UnitOfWork DataContext ISession.

+3

Source: https://habr.com/ru/post/1780763/


All Articles