C# 接口
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# 接口,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4625字,纯文字阅读大概需要7分钟。
内容图文
![C# 接口](/upload/InfoBanner/zyjiaocheng/1233/140607645d0d418c93b75732636d7c84.jpg)
分享自http://www.cnblogs.com/lori/p/3478913.html
接口是一组行为规范,看一个简单仓储接口
/// <summary> /// 基础的数据操作规范 /// </summary> /// <typeparam name="TEntity"></typeparam> public interface IRepository<TEntity> where TEntity : class { ///<summary>/// 添加实体并提交到数据服务器 ///</summary>///<param name="item">Item to add to repository</param>void Insert(TEntity item); ///<summary>/// 移除实体并提交到数据服务器 /// 如果表存在约束,需要先删除子表信息 ///</summary>///<param name="item">Item to delete</param>void Delete(TEntity item); ///<summary>/// 修改实体并提交到数据服务器 ///</summary>///<param name="item"></param>void Update(TEntity item); ///<summary>/// 得到指定的实体集合(延时结果集) /// Get all elements of type {T} in repository ///</summary>///<returns>List of selected elements</returns> IQueryable<TEntity> GetModel(); ///<summary>/// 根据主键得到实体 ///</summary>///<param name="id"></param>///<returns></returns> TEntity Find(paramsobject[] id); }
它会叫每个具体的仓储接口去实现它,如IUserRepository是用户持久化的接口,同时它也可能被持久化基类去实现,如DbContextRepository,它是使用ef来完成持久化的基类,当然你可以使用MemoryContextRepository去实现IRepository这个接口,当然它的功能就是使用内存表来实现持久化的。
抽象类的代码展示:
/// <summary> /// 做为一个持久化机制的实现,它可能是ado.net,linq2sql,entityframeworks,nhibernate,redis,memoryStream,fileStream etc. /// 宗旨:我们不应该将数据持久化的方式暴露到业务(领域)层 /// 建立仓储:为每个聚合根建立仓储接口和实现,而不是为每个实体 /// 使用仓储:应该根据使用场景去声明为仓储,而不是每次都是IExtensionRepository /// </summary> /// <typeparam name="TEntity"></typeparam> public class DemoContextRepository<TEntity> : IExtensionRepository<TEntity> where TEntity : class { #region IExtensionRepository<TEntity>成员 publicvoid Insert(IEnumerable<TEntity> item) { thrownew NotImplementedException(); } publicvoid Update(IEnumerable<TEntity> item) { thrownew NotImplementedException(); } publicvoid Delete(IEnumerable<TEntity> item) { thrownew NotImplementedException(); } publicvoid Update<T>(System.Linq.Expressions.Expression<Action<T>> entity) where T : class { thrownew NotImplementedException(); } public IQueryable<TEntity> GetModel(EntityFrameworks.Entity.Core.Specification.ISpecification<TEntity> specification) { thrownew NotImplementedException(); } public IQueryable<TEntity> GetModel(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) { thrownew NotImplementedException(); } public TEntity Find(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) { thrownew NotImplementedException(); } public TEntity Find(EntityFrameworks.Entity.Core.Specification.ISpecification<TEntity> specification) { thrownew NotImplementedException(); } publicvoid BulkInsert(IEnumerable<TEntity> item, bool isRemoveIdentity) { thrownew NotImplementedException(); } publicvoid BulkInsert(IEnumerable<TEntity> item) { thrownew NotImplementedException(); } publicvoid BulkUpdate(IEnumerable<TEntity> item, paramsstring[] fieldParams) { thrownew NotImplementedException(); } publicvoid BulkDelete(IEnumerable<TEntity> item) { thrownew NotImplementedException(); } publicevent Action<EntityFrameworks.Entity.Core.SavedEventArgs> AfterSaved; publicevent Action<EntityFrameworks.Entity.Core.SavedEventArgs> BeforeSaved; #endregion#region IOrderableRepository成员 public IQueryable<TEntity> GetModel(Action<EntityFrameworks.Entity.Core.Orderable<TEntity>> orderBy) { thrownew NotImplementedException(); } public IQueryable<TEntity> GetModel(Action<EntityFrameworks.Entity.Core.Orderable<TEntity>> orderBy, EntityFrameworks.Entity.Core.Specification.ISpecification<TEntity> specification) { thrownew NotImplementedException(); } public IQueryable<TEntity> GetModel(Action<EntityFrameworks.Entity.Core.Orderable<TEntity>> orderBy, System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) { thrownew NotImplementedException(); } #endregion#region IRepository<TEntity>成员 publicvoid Insert(TEntity item) { thrownew NotImplementedException(); } publicvoid Delete(TEntity item) { thrownew NotImplementedException(); } publicvoid Update(TEntity item) { thrownew NotImplementedException(); } public IQueryable<TEntity> GetModel() { thrownew NotImplementedException(); } public TEntity Find(paramsobject[] id) { thrownew NotImplementedException(); } #endregion }
上面的抽象类只是一个DEMO,所以实现持久化的逻辑我并没有实现,当然这并不是它的重点,重点在于你的具体仓储如果继承了它,将会以DemoContextRepository这种方式去持久化对象。
原文:http://www.cnblogs.com/benlxt/p/3583235.html
内容总结
以上是互联网集市为您收集整理的C# 接口全部内容,希望文章能够帮你解决C# 接口所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。