跟我学MVC系列(Repository模式、LINQ、EF、IOC框架Castle、JQuery、AJAX)(五)Models(ORM、Repository模式、分页)
现在是凌晨12点半拉,夜深人静,趁我还没有睡意,现在继续。
上篇我们完成了Resposity 的很大一部分啦,现在我们需要创建一个实体工厂,什么是实体工厂呢?就是能够生产实体的工厂呗,呵呵。你看看代码就知道啦。我们在Models文件夹下建立一个EntityFactory.cs 类和它的接口类IEntityFactory.cs,代码如下:
现在是凌晨12点半拉,夜深人静,趁我还没有睡意,现在继续。
上篇我们完成了Resposity 的很大一部分啦,现在我们需要创建一个实体工厂,什么是实体工厂呢?就是能够生产实体的工厂呗,呵呵。你看看代码就知道啦。我们在Models文件夹下建立一个EntityFactory.cs 类和它的接口类IEntityFactory.cs,代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBAccount.Repository;
namespace PBAccount.Models
{
public interface IEntityFactory
{
IUser CreateUser();
IAccount CreateAccount();
IAccountType CreateAccountType();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PBAccount.Repository;
namespace PBAccount.Models
{
public class EntityFactory:IEntityFactory
{
#region IEntityFactory 成员
public IUser CreateUser()
{
return new SQLUser();
}
public IAccount CreateAccount()
{
return new SQLAccount();
}
public IAccountType CreateAccountType()
{
return new SQLAccountType();
}
#endregion
}
}
是不是很简单,至于为什么要创建这个实体工厂,我在以后会慢慢的解释,其实,不用我解释,你慢慢也会发现它的用途啦。
以上呢,我们就把底层(所有的功能)都给实现啦,如果在以后发现还需要新的功能,到时候再添加吧。根据Resposity 模式的描述,我们还要继续写向Controller 曾服务的接口,也就是Service 层啦。这样,就再一次实现了与数据库的分离,上下层之间的耦合也非常小。好,现在我们与Models平级创建一个新文件夹Service,在Service 下定义三个接口,IUserService, IAccountService, IAccountTypeService 这三个接口。代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
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);
}
}
下面是对这三个接口的实现:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PBAccount.Repository;
using PBAccount.Models;
namespace PBAccount.Service
{
public class AccountService:IAccountService
{
IEntityFactory factory;
IAccountRepository accountRepository;
public AccountService(IEntityFactory factory, IAccountRepository accountRepository)
{
this.factory = factory;
this.accountRepository = accountRepository;
}
#region IAccountService 成员
public IAccount Create()
{
//现在明白IEntityFactory 的作用了吧
return factory.CreateAccount();
}
//我这儿只是简单的进行添加操作,如果在添加的时候想加入其他的东西,比如验证等,你完全可以在这儿丰富这些代码
public void Add(IAccount account)
{
try
{
accountRepository.Add(account);
}
catch (Exception ex)
{
throw ex;
}
}
public void Update(IAccount account)
{
try
{
accountRepository.Update(account);
}
catch (Exception ex)
{
throw ex;
}
}
public void Del(IAccount account)
{
try
{
accountRepository.Del(account);
}
catch (Exception ex)
{
throw ex;
}
}
public IAccount Get(Guid userId)
{
try
{
return accountRepository.Find(userId);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IAccount> GetAll()
{
try
{
return accountRepository.FindAll();
}
catch (Exception ex)
{
throw ex;
}
}
public int CountUsers(ICriteria criteria)
{
try
{
return accountRepository.Count(criteria);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IAccount> GetUsers(int pageIndex, int pageSize, ICriteria criteria, Order orderCriteria)
{
try
{
return accountRepository.Find(criteria, pageIndex, pageSize, orderCriteria);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PBAccount.Repository;
using PBAccount.Models;
namespace PBAccount.Service
{
public class AccountTypeService:IAccountTypeService
{
IEntityFactory factory;
IAccountTypeRepository accountTypeRepository;
public AccountTypeService(IEntityFactory factory, IAccountTypeRepository accountTypeRepository)
{
this.factory = factory;
this.accountTypeRepository = accountTypeRepository;
}
#region IAccountTypeService 成员
public IAccountType Create()
{
//现在明白IEntityFactory 的作用了吧
return factory.CreateAccountType();
}
//我这儿只是简单的进行添加操作,如果在添加的时候想加入其他的东西,比如验证等,你完全可以在这儿丰富这些代码
public void Add(IAccountType accountType)
{
try
{
accountTypeRepository.Add(accountType);
}
catch (Exception ex)
{
throw ex;
}
}
public void Update(IAccountType accountType)
{
try
{
accountTypeRepository.Update(accountType);
}
catch (Exception ex)
{
throw ex;
}
}
public void Del(IAccountType accountType)
{
try
{
accountTypeRepository.Del(accountType);
}
catch (Exception ex)
{
throw ex;
}
}
public IAccountType Get(Guid userId)
{
try
{
return accountTypeRepository.Find(userId);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IAccountType> GetAll()
{
try
{
return accountTypeRepository.FindAll();
}
catch (Exception ex)
{
throw ex;
}
}
public int CountUsers(ICriteria criteria)
{
try
{
return accountTypeRepository.Count(criteria);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IAccountType> GetUsers(int pageIndex, int pageSize, ICriteria criteria, Order orderCriteria)
{
try
{
return accountTypeRepository.Find(criteria, pageIndex, pageSize, orderCriteria);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PBAccount.Repository;
using PBAccount.Models;
namespace PBAccount.Service
{
public class UserService:IUserService
{
IEntityFactory factory;
IRepository<IUser> userRepository;
public UserService(IEntityFactory factory,IUserRepository userRepository)
{
this.factory = factory;
this.userRepository = userRepository;
}
#region IUserService 成员
public IUser Create()
{
//现在明白IEntityFactory 的作用了吧
return factory.CreateUser();
}
//我这儿只是简单的进行添加操作,如果在添加的时候想加入其他的东西,比如验证等,你完全可以在这儿丰富这些代码
public void Add(IUser user)
{
try
{
userRepository.Add(user);
}
catch (Exception ex)
{
throw ex;
}
}
public void Update(IUser user)
{
try
{
userRepository.Update(user);
}
catch (Exception ex)
{
throw ex;
}
}
public void Del(IUser user)
{
try
{
userRepository.Del(user);
}
catch (Exception ex)
{
throw ex;
}
}
public IUser Get(Guid userId)
{
try
{
return userRepository.Find(userId);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IUser> GetAll()
{
try
{
return userRepository.FindAll();
}
catch (Exception ex)
{
throw ex;
}
}
public int CountUsers(ICriteria criteria)
{
try
{
return userRepository.Count(criteria);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IUser> GetUsers(int pageIndex, int pageSize, ICriteria criteria, Order orderCriteria)
{
try
{
return userRepository.Find(criteria,pageIndex,pageSize,orderCriteria);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
好啦,现在Models 层基本完成啦,下篇就开始Controlers 和 Views 。
上篇我们完成了Resposity 的很大一部分啦,现在我们需要创建一个实体工厂,什么是实体工厂呢?就是能够生产实体的工厂呗,呵呵。你看看代码就知道啦。我们在Models文件夹下建立一个EntityFactory.cs 类和它的接口类IEntityFactory.cs,代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBAccount.Repository;
namespace PBAccount.Models
{
public interface IEntityFactory
{
IUser CreateUser();
IAccount CreateAccount();
IAccountType CreateAccountType();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PBAccount.Repository;
namespace PBAccount.Models
{
public class EntityFactory:IEntityFactory
{
#region IEntityFactory 成员
public IUser CreateUser()
{
return new SQLUser();
}
public IAccount CreateAccount()
{
return new SQLAccount();
}
public IAccountType CreateAccountType()
{
return new SQLAccountType();
}
#endregion
}
}
以上呢,我们就把底层(所有的功能)都给实现啦,如果在以后发现还需要新的功能,到时候再添加吧。根据Resposity 模式的描述,我们还要继续写向Controller 曾服务的接口,也就是Service 层啦。这样,就再一次实现了与数据库的分离,上下层之间的耦合也非常小。好,现在我们与Models平级创建一个新文件夹Service,在Service 下定义三个接口,IUserService, IAccountService, IAccountTypeService 这三个接口。代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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);
}
}
下面是对这三个接口的实现:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PBAccount.Repository;
using PBAccount.Models;
namespace PBAccount.Service
{
public class AccountService:IAccountService
{
IEntityFactory factory;
IAccountRepository accountRepository;
public AccountService(IEntityFactory factory, IAccountRepository accountRepository)
{
this.factory = factory;
this.accountRepository = accountRepository;
}
#region IAccountService 成员
public IAccount Create()
{
//现在明白IEntityFactory 的作用了吧
return factory.CreateAccount();
}
//我这儿只是简单的进行添加操作,如果在添加的时候想加入其他的东西,比如验证等,你完全可以在这儿丰富这些代码
public void Add(IAccount account)
{
try
{
accountRepository.Add(account);
}
catch (Exception ex)
{
throw ex;
}
}
public void Update(IAccount account)
{
try
{
accountRepository.Update(account);
}
catch (Exception ex)
{
throw ex;
}
}
public void Del(IAccount account)
{
try
{
accountRepository.Del(account);
}
catch (Exception ex)
{
throw ex;
}
}
public IAccount Get(Guid userId)
{
try
{
return accountRepository.Find(userId);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IAccount> GetAll()
{
try
{
return accountRepository.FindAll();
}
catch (Exception ex)
{
throw ex;
}
}
public int CountUsers(ICriteria criteria)
{
try
{
return accountRepository.Count(criteria);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IAccount> GetUsers(int pageIndex, int pageSize, ICriteria criteria, Order orderCriteria)
{
try
{
return accountRepository.Find(criteria, pageIndex, pageSize, orderCriteria);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PBAccount.Repository;
using PBAccount.Models;
namespace PBAccount.Service
{
public class AccountTypeService:IAccountTypeService
{
IEntityFactory factory;
IAccountTypeRepository accountTypeRepository;
public AccountTypeService(IEntityFactory factory, IAccountTypeRepository accountTypeRepository)
{
this.factory = factory;
this.accountTypeRepository = accountTypeRepository;
}
#region IAccountTypeService 成员
public IAccountType Create()
{
//现在明白IEntityFactory 的作用了吧
return factory.CreateAccountType();
}
//我这儿只是简单的进行添加操作,如果在添加的时候想加入其他的东西,比如验证等,你完全可以在这儿丰富这些代码
public void Add(IAccountType accountType)
{
try
{
accountTypeRepository.Add(accountType);
}
catch (Exception ex)
{
throw ex;
}
}
public void Update(IAccountType accountType)
{
try
{
accountTypeRepository.Update(accountType);
}
catch (Exception ex)
{
throw ex;
}
}
public void Del(IAccountType accountType)
{
try
{
accountTypeRepository.Del(accountType);
}
catch (Exception ex)
{
throw ex;
}
}
public IAccountType Get(Guid userId)
{
try
{
return accountTypeRepository.Find(userId);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IAccountType> GetAll()
{
try
{
return accountTypeRepository.FindAll();
}
catch (Exception ex)
{
throw ex;
}
}
public int CountUsers(ICriteria criteria)
{
try
{
return accountTypeRepository.Count(criteria);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IAccountType> GetUsers(int pageIndex, int pageSize, ICriteria criteria, Order orderCriteria)
{
try
{
return accountTypeRepository.Find(criteria, pageIndex, pageSize, orderCriteria);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PBAccount.Repository;
using PBAccount.Models;
namespace PBAccount.Service
{
public class UserService:IUserService
{
IEntityFactory factory;
IRepository<IUser> userRepository;
public UserService(IEntityFactory factory,IUserRepository userRepository)
{
this.factory = factory;
this.userRepository = userRepository;
}
#region IUserService 成员
public IUser Create()
{
//现在明白IEntityFactory 的作用了吧
return factory.CreateUser();
}
//我这儿只是简单的进行添加操作,如果在添加的时候想加入其他的东西,比如验证等,你完全可以在这儿丰富这些代码
public void Add(IUser user)
{
try
{
userRepository.Add(user);
}
catch (Exception ex)
{
throw ex;
}
}
public void Update(IUser user)
{
try
{
userRepository.Update(user);
}
catch (Exception ex)
{
throw ex;
}
}
public void Del(IUser user)
{
try
{
userRepository.Del(user);
}
catch (Exception ex)
{
throw ex;
}
}
public IUser Get(Guid userId)
{
try
{
return userRepository.Find(userId);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IUser> GetAll()
{
try
{
return userRepository.FindAll();
}
catch (Exception ex)
{
throw ex;
}
}
public int CountUsers(ICriteria criteria)
{
try
{
return userRepository.Count(criteria);
}
catch (Exception ex)
{
throw ex;
}
}
public IList<IUser> GetUsers(int pageIndex, int pageSize, ICriteria criteria, Order orderCriteria)
{
try
{
return userRepository.Find(criteria,pageIndex,pageSize,orderCriteria);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
好啦,现在Models 层基本完成啦,下篇就开始Controlers 和 Views 。