分享

跟我学MVC系列(Repository模式、LINQ、EF、IOC框架Castle、JQuery、AJAX)(五)Models(ORM、Repository模式、分页)

 软件技术与教学 2014-06-15
现在是凌晨12点半拉,夜深人静,趁我还没有睡意,现在继续。
上篇我们完成了Resposity 的很大一部分啦,现在我们需要创建一个实体工厂,什么是实体工厂呢?就是能够生产实体的工厂呗,呵呵。你看看代码就知道啦。我们在Models文件夹下建立一个EntityFactory.cs 类和它的接口类IEntityFactory.cs,代码如下:
Code
是不是很简单,至于为什么要创建这个实体工厂,我在以后会慢慢的解释,其实,不用我解释,你慢慢也会发现它的用途啦。
以上呢,我们就把底层(所有的功能)都给实现啦,如果在以后发现还需要新的功能,到时候再添加吧。根据Resposity  模式的描述,我们还要继续写向Controller 曾服务的接口,也就是Service 层啦。这样,就再一次实现了与数据库的分离,上下层之间的耦合也非常小。好,现在我们与Models平级创建一个新文件夹Service,在Service 下定义三个接口,IUserService, IAccountService, IAccountTypeService 这三个接口。代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBAccount.Repository;
using PBAccount.Models;

namespace PBAccount.Service
{
   
public interface IAccountService
    {
       IAccount Create();
       
void Add(IAccount account);

       
void Update(IAccount account);

       
void Del(IAccount account);

       IAccount Get(Guid userId);

       IList
<IAccount> GetAll();

        
int CountAccounts(ICriteria criteria);

        IList
<IAccount> GetAccounts(int pageIndex, int pageSize, ICriteria criteria, Order orderCriteria);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBAccount.Repository;
using PBAccount.Models;

namespace PBAccount.Service
{
    
interface IAccountTypeService
    {
        IAccountType Create();
        
void Add(IAccountType accountType);

        
void Update(IAccountType accountType);

        
void Del(IAccountType accountType);

        IAccountType Get(Guid userId);

        IList
<IAccountType> GetAll();

        
int CountAccountType(ICriteria criteria);

        IList
<IAccountType> GetEquipments(int pageIndex, int pageSize, ICriteria criteria, Order orderCriteria);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBAccount.Repository;
using PBAccount.Models;

namespace PBAccount.Service
{
   
public interface IUserService
    {
       IUser Create();
       
void Add(IUser user);

       
void Update(IUser user);

       
void Del(IUser user);

       IUser Get(Guid userId);

       IList
<IUser> GetAll();

       
int CountUsers(ICriteria criteria);

       IList
<IUser> GetUsers(int pageIndex, int pageSize, ICriteria criteria, Order orderCriteria);
    }
}

下面是对这三个接口的实现:
Code

好啦,现在Models 层基本完成啦,下篇就开始Controlers 和 Views 。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多