C#.NET:TransactionScope对象是否在多个用户线程之间同步对代码的访问?
TransactionScope对象是否也同步多个用户线程之间的代码访问?或者,它只将代码(业务操作)声明为原子(单个事务) 详情: 1.我正在为基础结构层中的存储库实现UnitOfWork类,该层本身被定义为类库项目(dll)C#.NET:TransactionScope对象是否在多个用户线程之间同步对代码的访问?,c#,transactionscope,unitofworkapplication,C#,Transactionscope,Unitofworkapplication,TransactionScope对象是否也同步多个用户线程之间的代码访问?或者,它只将代码(业务操作)声明为原子(单个事务) 详情: 1.我正在为基础结构层中的存储库实现UnitOfWork类,该层本身被定义为类库项目(dll) 存储库包含对UnitOfWork对象的引用,以调用其方法,这些方法维护已添加、更改或更新的实体的目录/集合 工作类单元有一个成员函数Commits(),该函数将代码包装在TransactionScope对象中 假设有多个用户访问域/业务对象,那么我假设每个用户都有自己的
public class UnitOfWork
{
private Dictionary<EntityBase, IUnitOfWorkRepository> addedEntities;
private Dictionary<EntityBase, IUnitOfWorkRepository> changedEntities;
private Dictionary<EntityBase, IUnitOfWorkRepository> deletedEntities;
public UnitOfWork()
{
this.addedEntities = new Dictionary<EntityBase, IUnitOfWorkRepository>();
this.changedEntities = new Dictionary<EntityBase, IUnitOfWorkRepository>();
this.deletedEntities = new Dictionary<EntityBase, IUnitOfWorkRepository>();
}
#region IUnitOfWork Members
public void RegisterAdded(EntityBase entity, IUnitOfWorkRepository repository)
{
this.addedEntities.Add(entity, repository);
}
public void RegisterChanged(EntityBase entity, IUnitOfWorkRepository repository)
{
this.changedEntities.Add(entity, repository);
}
public void RegisterRemoved(EntityBase entity, IUnitOfWorkRepository repository)
{
this.deletedEntities.Add(entity, repository);
}
public void Commit()
{
using (TransactionScope scope = new TransactionScope())
{
foreach (EntityBase entity in this.deletedEntities.Keys)
{
this.deletedEntities[entity].PersistDeletedItem(entity);
}
foreach (EntityBase entity in this.addedEntities.Keys)
{
this.addedEntities[entity].PersistDeletedItem(entity);
}
foreach (EntityBase entity in this.changedEntities.Keys)
{
this.changedEntities[entity].PersistDeletedItem(entity);
}
scope.Complete();
}
this.deletedEntities.Clear();
this.addedEntities.Clear();
this.changedEntities.Clear();
}
#endregion
}
公共类UnitOfWork
{
私人词典附录;
私人词典的变化;
私人身份;
公共工作单元()
{
this.addedEntities=新字典();
this.changedEntities=新字典();
this.deletedEntities=新字典();
}
#区域I工作组成员
public void RegisterAdded(EntityBase实体、IUnitOfWorkRepository存储库)
{
this.addedEntities.Add(实体、存储库);
}
已更改公共无效注册表(EntityBase实体、IUnitOfWorkRepository存储库)
{
this.changedEntities.Add(实体、存储库);
}
public void RegisterRemoved(EntityBase实体、IUnitOfWorkRepository存储库)
{
this.deletedEntities.Add(实体、存储库);
}
公共无效提交()
{
使用(TransactionScope范围=新TransactionScope())
{
foreach(此.deletedEntities.Keys中的EntityBase实体)
{
this.deletedEntities[entity].PersistDeletedItem(entity);
}
foreach(此.addedEntities.Keys中的EntityBase实体)
{
this.addedEntities[entity].PersistDeletedItem(entity);
}
foreach(此.changedEntities.Keys中的EntityBase实体)
{
this.changedEntities[entity].PersistDeletedItem(entity);
}
scope.Complete();
}
此.deletedEntities.Clear();
此.addedEntities.Clear();
this.changedEntities.Clear();
}
#端区
}
使用系统;
使用System.Collections.Generic;
使用智能卡基础设施;
使用SmartCA.Infrastructure.DomainBase;
使用SmartCA.Infrastructure.RepositoryFramework.Configuration;
使用系统配置;
命名空间SmartCA.Infrastructure.RepositoryFramework
{
公共静态类RepositoryFactory
{
//字典来强制执行单例模式
私有静态字典<字符串,对象>存储库=新建
字典<字符串,对象>();
///
///获取或创建请求接口的实例
///存储库被创建和初始化,它被缓存,所有
///将来对存储库的请求将来自缓存。
///
///存储库的接口
///创建。
///实体库的类型
///存储库用于。
///请求的接口实例。
公共静态存储库GetRepository()
其中TRepository:类,IRepository
其中tenty:EntityBase
{
//初始化提供程序的默认值
TRepository repository=默认值(TRepository);
string interfaceShortName=typeof(TRepository).Name;
//查看提供程序是否已创建且在缓存中
if(!RepositoryFactory.repositories.ContainsKey(interfaceShortName))
{
//不在那里,所以创建它
//获取repositoryMappingsConfiguration配置部分
存储设置=
(RepositorySettings)ConfigurationManager.GetSection(RepositoryMappingConstants
.repositoryMappings配置节名称);
//创建存储库,并将其强制转换到指定的接口
存储库=
Activator.CreateInstance(Type.GetType(settings.RepositoryMappings[interfaceShortName]
.RepositoryFullTypeName)作为存款人;
//将新的提供程序实例添加到缓存
RepositoryFactory.repositories.Add(interfaceShortName,repository);
}
其他的
{
//提供程序在缓存中,因此请检索它
存储库=
(TRepository)RepositoryFactory.repositories[interfaceShortName];
}
返回存储库;
}
}
}
--代码
此方法的签名很有趣,因为它使用了两个泛型类型参数TRepository和TEntity,限制条件是TRepository是一个类并实现了IRepositoryusing System;
using System.Collections.Generic;
using SmartCA.Infrastructure;
using SmartCA.Infrastructure.DomainBase;
using SmartCA.Infrastructure.RepositoryFramework.Configuration;
using System.Configuration;
namespace SmartCA.Infrastructure.RepositoryFramework
{
public static class RepositoryFactory
{
// Dictionary to enforce the singleton pattern
private static Dictionary < string, object > repositories = new
Dictionary < string, object > ();
/// < summary >
/// Gets or creates an instance of the requested interface. Once a
/// repository is created and initialized, it is cached, and all
/// future requests for the repository will come from the cache.
/// < /summary >
/// < typeparam name=”TRepository” > The interface of the repository
/// to create. < /typeparam >
/// < typeparam name=”TEntity” > The type of the EntityBase that the
/// repository is for. < /typeparam >
/// < returns > An instance of the interface requested. < /returns >
public static TRepository GetRepository < TRepository, TEntity > ()
where TRepository : class, IRepository < TEntity >
where TEntity : EntityBase
{
// Initialize the provider’s default value
TRepository repository = default(TRepository);
string interfaceShortName = typeof(TRepository).Name;
// See if the provider was already created and is in the cache
if (!RepositoryFactory.repositories.ContainsKey(interfaceShortName))
{
// Not there, so create it
// Get the repositoryMappingsConfiguration config section
RepositorySettings settings =
(RepositorySettings)ConfigurationManager.GetSection(RepositoryMappingConstants
.RepositoryMappingsConfigurationSectionName);
// Create the repository, and cast it to the interface specified
repository =
Activator.CreateInstance(Type.GetType(settings.RepositoryMappings[interfaceShortName]
.RepositoryFullTypeName)) as TRepository;
// Add the new provider instance to the cache
RepositoryFactory.repositories.Add(interfaceShortName, repository);
}
else
{
// The provider was in the cache, so retrieve it
repository =
(TRepository)RepositoryFactory.repositories[interfaceShortName];
}
return repository;
}
}
}