设计自己的数据访问层(1)抽象数据访问层的功能

本文详细介绍了IDataContext接口的功能,包括如何获取持久化的实例、执行查询、添加、删除和保存对象等操作。此外还提供了关于事务管理和状态检查的方法说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如下IDataDontext接口所示给出了数据访问层的数据上下文接口:

ExpandedBlockStart.gifView Code 
   /// <summary>
    
/// The main runtime interface between the application and the data access layer. This is the central
    
/// API type abstracting the notion of a persistence service.
    
/// </summary>
    public interface IDataContext : IDisposable
    {
        
/// <summary>
        
/// Reports whether this <c>IObjectSpaceServices</c> contain any changes which must be synchronized with the database
        
/// </summary>
        bool IsDirty { get; }

        
/// <summary>
        
/// Reports whether this <c>IObjectSpaceServices</c> is working transactionally
        
/// </summary>
        bool IsInTransaction { get; }

        
/// <summary>
        
/// Retrieves all the persisted instances of a given type
        
/// </summary>
        
/// <typeparam name="T">The type of the object to retrieve</typeparam>
        
/// <returns>The list of persistent objects</returns>
        IList<T> GetAll<T>() where T : classnew();

        
/// <summary>
        
/// Retrieves all the persisted instances of a given type
        
/// </summary>
        
/// <typeparam name="T">The type of the object to retrieve</typeparam>
        
/// <param name="pageIndex">The index of the page to retrieve</param>
        
/// <param name="pageSize">The size of the page to retrieve</param>
        
/// <returns>The list of persistent objects</returns>
        IList<T> GetAll<T>(int pageIndex, int pageSize) where T : classnew();

        
/// <summary>
        
/// Executes a query
        
/// </summary>
        
/// <typeparam name="T">The type of the objects returned</typeparam>
        
/// <param name="query">The query</param>
        
/// <returns>A distinct list of instances</returns>
        IList<T> GetByCriteria<T>(Query query) where T : classnew();

        
/// <summary>
        
/// Executes a query using pagination facilities
        
/// </summary>
        
/// <typeparam name="T">The type of the objects returned</typeparam>
        
/// <param name="query">The query</param>
        
/// <param name="pageIndex">The index of the page to retrieve</param>
        
/// <param name="pageSize">The size of the page to retrieve</param>
        
/// <returns>A distinct list of instances</returns>
        IList<T> GetByCriteria<T>(Query query, int pageIndex, int pageSize) where T : classnew();

        
/// <summary>
        
/// Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
        
/// </summary>
        
/// <typeparam name="T">The type of the object</typeparam>
        
/// <param name="id">The identifier of the object</param>
        
/// <returns>The persistent instance or null</returns>
        T GetById<T>(object id) where T : classnew();

        
/// <summary>
        
/// Returns the amount of objects of a given type
        
/// </summary>
        
/// <typeparam name="T">The type of the object</typeparam>
        
/// <returns>The amount of objects</returns>
        int GetCount<T>();

        
/// <summary>
        
/// Returns the amount of objects of a given type that would be returned by a query
        
/// </summary>
        
/// <typeparam name="T">The type of the object</typeparam>
        
/// <param name="query">The query</param>
        
/// <returns>The amount of objects</returns>
        int GetCount<T>(Query query);

        
/// <summary>
        
/// Adds an object to the repository
        
/// </summary>
        
/// <param name="item">The object to add</param>
        void Add(object item);

        
/// <summary>
        
/// Deletes an object from the repository
        
/// </summary>
        
/// <param name="item">The object to delete</param>
        void Delete(object item);

        
/// <summary>
        
/// Saves an object to the repository
        
/// </summary>
        
/// <param name="item">The object to save</param>
        void Save(object item);

        
/// <summary>
        
/// Begins a transaction
        
/// </summary>
        
/// <exception cref="InvalidOperationException">Thrown if there is an already active transaction</exception>
        void BeginTransaction();

        
/// <summary>
        
/// Commits the active transaction
        
/// </summary>
        
/// <exception cref="InvalidOperationException">Thrown if there isn't an active transaction</exception>
        void Commit();

        
/// <summary>
        
/// Rollbacks the active transaction
        
/// </summary>
        
/// <exception cref="InvalidOperationException">Thrown if there isn't an active transaction</exception>
        void Rollback();

转载于:https://www.cnblogs.com/jeriffe/articles/2110057.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值