您的位置:首页 > 编程语言 > ASP

扩展ASP.NET MVC三层框架并使用StructureMap实现依赖注入2-Repository层的实现

2013-04-06 15:50 1206 查看
接着上篇文章扩展ASP.NET MVC三层框架并使用StructureMap实现依赖注入1-Model层的实现,这篇文章主要介绍一下代码中个和Model层打交道的Repository数据库持久层。之所以建立Repository这一层是为了防止Service业务逻辑层直接和Model数据库层打交道,这样将来如果我们的系统数据库由于各种原因更换,这时我们只需要修改Repository层和Model层,对于Service层我们是不需要修改的。

这时你会想我在Service层调用了Repository层的代码,为什么不需要修改Service层?这要得益于我们使用的StructureMap这个依赖注入框架,不同层之间,我们的在代码中是不会自己去new创建其它层的对象的,创建实例的事情统统交给StructureMap去做,通过接口调用不同层的方法,你会发现本系统使用了非常神秘的面向接口编程~ 嘿嘿。 StructureMap的配置和使用,这篇文章不作介绍,等我们完成了Repository和service工程,再加上一个Interface工程之后,我们再去实现StructureMap。因为将来会使用StructureMap所以Interface工程将会和Repository工程一起介绍。

首先是TYStudioDemo.Repositories和TYStudioDemo.Interfaces两个工程的截图,没错就是明了直观的截图兄。





为了方便维护所有的接口都放在TYStudioDemo.Interfaces工程里面,由于本系统只是一个Demo,所以只对数据库中Supplier这个表进行简单的曾删改查操作,起到抛砖引玉的作用,接下来俺要开始扔砖了~

由于增删改查是每个表都有的操作,所以这里抽取出来一个Repository的顶级接口ITYRepository,我们使用泛型来约束处理的Entity。这里面定义了一些基础的linq操作方法,相信使用过Linq和EntityFramework的人一眼就能看出这里声明的方法的作用,so不在一一说明(不明白的百度一下,你就知道)。

View Codes





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace TYStudioDemo.Interfaces

{

public interface ITYRepository<T> where T
: class

{

IQueryable<T> Fetch();

IEnumerable<T> GetAll();

IEnumerable<T> Find(Func<T,
bool> predicate);

T Single(Func<T,
bool> predicate);

T SingleOrDefault(Func<T,
bool> predicate);

T First(Func<T,
bool> predicate);

void Create(T entity);

void Delete(T entity);

void Delete(Func<T,
bool> predicate);

void Attach(T entity);

}

}

接下来是接口ITYRepository的实现类TYRepository,实现顶级接口中的每个方法。在这里我们就用到了上篇文章定义的TYEntities.Current了,保证了多个Repository的线程安全和事务处理。实际使用时,有时你需要多个Entity的Repository,我们可以一个一个的操作,但是我们只调用一次TYEntities.Current.SaveChanges()方法,着所有的操作都在一个datacontext里面了,保证了事务处理。具体的使用方法,将在Service篇介绍给大家。下面是TYRepository的代码实现


View Codes





using System;

using System.Collections.Generic;

using System.Data.Objects;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using TYStudioDemo.Interfaces;

using TYStudioDemo.Models;

namespace TYStudioDemo.Repositories

{

public class TYRepository<T>
: ITYRepository<T> where T
: class

{

protected ObjectContext _context;

private IObjectSet<T> _objectSet;

public TYRepository()

{

//总是返回当前的datacontext,详细说明请看教程的Models篇

_context = TYEntities.Current;

_objectSet = _context.CreateObjectSet<T>();

}

public IQueryable<T> Fetch()

{

return _objectSet.AsQueryable<T>();

}

public IEnumerable<T> GetAll()

{

return Fetch().AsEnumerable<T>();

}

public IEnumerable<T> Find(Func<T,
bool> predicate)

{

return _objectSet.Where<T>(predicate);

}

public T Single(Func<T,
bool> predicate)

{

return _objectSet.Single<T>(predicate);

}

public T SingleOrDefault(Func<T,
bool> predicate)

{

return _objectSet.SingleOrDefault<T>(predicate);

}

public T First(Func<T,
bool> predicate)

{

return _objectSet.First<T>(predicate);

}

public void Delete(T entity)

{

if (entity
== null)

{

throw
new ArgumentNullException("entity");

}

_objectSet.DeleteObject(entity);

}

public void Delete(Func<T,
bool> predicate)

{

IEnumerable<T> records
= from x in _objectSet.Where<T>(predicate) select x;

foreach (T record
in records)

{

_objectSet.DeleteObject(record);

}

}

public void Create(T entity)

{

if (entity
== null)

{

throw
new ArgumentNullException("entity");

}

_objectSet.AddObject(entity);

}

public void Attach(T entity)

{

_objectSet.Attach(entity);

}

}

}

现在我们有了顶级的ITYRepository接口和它的实现类TYRepository,接下来就可以实现个体Entity的repository了。

首先我们建立一个ISupplierRepository接口,为什么要建立这个接口呢,因为我们是面向接口编程,将来需要用StructureMap实现将它的实现类SupplierRepository注入到Service层,并且这里可以声明除了ITYRepository中方法以外的方法,也就是对ITYRepository一个扩展,属于SupplierRepository自己的方法,这里我们简单的声明一个GetByCompanyName(string companyName)方法,在实际使用中,如果需要自己的处理操作,就将方法声明在这里,而不是ITYRepository,如果不需要可以不声明任何方法,但是这个接口是一定要有的。我们需要注入到Service的。

下面是ISupplierRepository代码:

View Codes





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace TYStudioDemo.Interfaces

{

public interface ISupplierRepository<T>
: ITYRepository<T> where T
: class

{

//声明一个通过companyName获得supplier的方法

//注意这个方法是ITYRepository没有声明的,也就是我们扩展的业务方法。

//其实这个方法完全可以放到Service,这里只是介绍一下ISupplierRepository接口的用法

T GetByCompanyName(string companyName);

}

}

接下来是ISupplierRepository的实现类SupplierRepository,这里需要注意的是,SupplierRepository同时需要继承顶级接口ITYRepository的实现类TYRepository,这样ITYRepository声明的公共方法就不需要在SupplierRepository里实现了,因为我们已经在TYRepository实现了。所以这只需要实现ISupplierRepository声明的方法GetByCompanyName(string companyName)

View Codes





using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using TYStudioDemo.Interfaces;

using TYStudioDemo.Models;

namespace TYStudioDemo.Repositories

{

public class SupplierRepository
: TYRepository<Supplier>, ISupplierRepository<Supplier>

{

//实现ISupplierRepository中的方法

#region ISupplierRepository<Supplier> Members

public Supplier GetByCompanyName(string companyName)

{

return TYEntities.Current.Suppliers.SingleOrDefault(e=>e.CompanyName
== companyName);

}

#endregion

}

}

到这里SupplierRepository全部实现了。

总结:至此Repository数据库持久层就介绍完了。Repository层主要搞清楚顶级接口ITYRepository以及它的实现类TYRepository和ISupplierRepository以及实现类SupplierRepository之间的继承关系,如果不明白补一补面向对象的基础吧。

下篇文章将介绍Service业务逻辑层的实现。

相关文章

扩展ASP.NET MVC三层框架并使用StructureMap实现依赖注入1-Model层的实现
MVC4 Simplemembership实现用户权限管理-权限控制
Simplemembership实现用户权限管理-扩展数据库
Simplemembership实现用户权限管理-初步认识
扩展ASP.NET MVC三层框架并使用Enterprise Library实现异常与日志记录(6)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐