ASP.NET WebApplication中的DataMappers存储

时间:2012-01-10 14:18:22

标签: asp.net database datamapper poeaa identity-map

Martin Fowler的“企业应用架构模式” 描述了用于组织DAL的方法,如用于实体的一组映射器。每个都有自己的IdentityMap存储特定实体。

例如在我的ASP.NET WebApplication中:

//AbstractMapper - superclass for all mappers in DAL
public abstract class AbstractMapper
{
    private readonly string _connectionString;
    protected string ConnectionString
    {
      get { return _connectionString; }  
    }

    private readonly DbProviderFactory _dbFactory;
    protected DbProviderFactory DBFactory
    {
        get { return _dbFactory; }
    } 

    #region LoadedObjects (IdentityMap)
    protected Hashtable LoadedObjects = new Hashtable();

    public void RegisterObject(long id, DomainObject obj)
    {
        LoadedObjects[id] = obj;            
    }

    public void UnregisterObject(long id)
    {
        LoadedObjects.Remove(id);
    }
    #endregion

    public AbstractMapper(string connectionString, DbProviderFactory dbFactory)
    {
        _connectionString = connectionString;
        _dbFactory = dbFactory;
    }

    protected virtual string DBTable
    {
        get
        {
            throw new NotImplementedException("database table is not defined in class " + this.GetType()); 
        }
    }

    protected virtual T Find<T>(long id, IDbTransaction tr = null) where T : DomainObject
    {
        if (id == 0)
            return null;
        T result = (T)LoadedObjects[id];
        if (result != null)
            return result;
        IDbConnection cn = GetConnection(tr);
        IDbCommand cmd = CreateCommand(GetFindStatement(id), cn, tr);
        IDataReader rs = null;
        try
        {
            OpenConnection(cn, tr);
            rs = cmd.ExecuteReader(CommandBehavior.SingleRow);
            result = (rs.Read()) ? Load<T>(rs) : null;                  
        }
        catch (DbException ex)
        {
            throw new DALException("Error while loading an object by id in class " + this.GetType(), ex);
        }
        finally
        {
            CleanUpDBResources(cmd, cn, tr, rs);
        }
        return result;
    }

    protected virtual T Load<T>(IDataReader rs) where T : DomainObject
    {
        long id = GetReaderLong(rs["ID"]);
        T result = (T)LoadedObjects[id];
        if (result != null) 
            return result;

        result = (T)DoLoad(id, rs);
        RegisterObject(id, result);
        return result;
    }

    // another CRUD here ...
}

// Specific Mapper for entity Account
public class AccountMapper : AbstractMapper
{
internal override string DBTable
{
    get { return "Account"; }
}

public AccountMapper(string connectionString, DbProviderFactory dbFactory) : base(connectionString, dbFactory) { }

public Account Find(long id)
{
    return Find<Account>(id);
}

public override DomainObject DoLoad(long id, IDataReader rs)
{
    Account account = new Account(id);
    account.Name = GetReaderString(rs["Name"]);
    account.Value = GetReaderDecimal(rs["Value"]);
    account.CurrencyID = GetReaderLong(rs["CurrencyID"]);
    return account;
}

// ...

}

问题是:在哪里存储这些映射器?系统服务(实体)应该如何调用映射器?

我决定创建包含所有映射器的MapperRegistry。所以服务可以调用像这样的映射器:

    public class AccountService : DomainService
{
    public static Account FindAccount(long accountID)
    {
        if (accountID > 0)
            return MapperRegistry.AccountMapper.Find(accountID);
        return null;
    }
    ...
}

但是我可以在哪里存储MapperRegistry实例?我看到以下变体,但不喜欢它们中的任何一种:

  1. MapperRegistry是应用程序的全局(Singleton)

    • 不适用,因为在多线程ASP.NET应用程序中需要同步(至少Martin说只有疯狂可以选择这种变体)
  2. 每个会话的MapperRegistry

    • 似乎也不太好。所有ORM(NHibernate,LINQ to SQL,EntityFramework)都建议每个请求使用DataContext(NHibernateSession,ObjectContext),而不是在Session中存储上下文。
    • 同样在我的WebApp中,几乎所有请求都是对EntityController.asmx(带有属性ScriptService)的AJAX请求,返回JSON。并且不允许进行会话。
  3. 每个请求的MapperRegistry

    • 有很多单独的AJAX调用。在这种情况下,MapperRegistry的生命周期将太小。因此,几乎总是会从数据库中检索数据,结果是性能低下。
  4. 尊敬的各位专家,请帮我解决一下建筑解决方案。

0 个答案:

没有答案