MVC仓储模式的相关代码

大致架构
MVC仓储模式的相关代码 -  我要我的快乐 - 我要我的快乐
 
 底层部分代码:
0.ContextHelper创建:
 
  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPW.Hotel.DomainModel
{
//单例模式
public sealed class HotelEntitesHelper
{
private static HotelEntities hotelEntities;
static HotelEntitesHelper()
{
hotelEntities = new HotelEntities();
}

public static HotelEntities GetCurrentEntities()
{
if (hotelEntities == null)
{
hotelEntities = new HotelEntities();
}
return hotelEntities;
}
}
}



1.IDAL层仓储接口
 
  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPW.Hotel.IDAO
{
public interface IRepository<T> where T : class ,new()//约束T是个class,并且有构造参数
{
bool Add(T entity);
bool Update(T entity);
bool DeleteById(object id);
T GetEntity(object id);
IList<T> GetByPage(Func<T, bool> whereLambda, int? pageSize, int? pageIndex);
IList<T> GetEntitys(Func<T, bool> whereLambda);//在参数中放了一个泛型委托
IList<T> GetAll();
}
}

2.建一个ICustomerRepository 接口继承
 
   

public interface ICustomerRepository : IRepository<Customer>
{
}

3.DalImplement实现类
 
  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPW.Hotel.IDAO;
using SPW.Hotel.DomainModel;
using System.Data.Entity;


namespace SPW.Hotel.EFDaoImpl
{
public class CustomerRepository : ICustomerRepository
{
public HotelEntities hotel;
public bool Add(DomainModel.Customer entity)
{
hotel = HotelEntitesHelper.GetCurrentEntities();
hotel.Customer.AddObject(entity);
int changeNums = hotel.SaveChanges();
if (changeNums > 0)
{
return true;
}
return false;
}

public bool Update(DomainModel.Customer entity)
{
hotel = HotelEntitesHelper.GetCurrentEntities();
hotel.Customer.Attach(entity);
hotel.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
int changeNums = hotel.SaveChanges();
if (changeNums > 0)
{
return true;
}
return false;
}

public bool DeleteById(object id)
{
hotel = HotelEntitesHelper.GetCurrentEntities();
int key = (int)id;
Customer c = hotel.Customer.Where<Customer>(q => q.ID == key).SingleOrDefault<Customer>();
if (c != null)
{
hotel.Customer.DeleteObject(c);
if (hotel.SaveChanges() > 0)
{
return true;
}
}
return false;
}

public IList<DomainModel.Customer> GetAll()
{
hotel = HotelEntitesHelper.GetCurrentEntities();
//return hotel.Customer.ToList();
return (from q in hotel.Customer
select q).ToList<Customer>();
}

public IList<DomainModel.Customer> GetByPage(Func<DomainModel.Customer, bool> whereLambda, int? pageSize, int? pageIndex)
{
hotel = HotelEntitesHelper.GetCurrentEntities();
pageIndex = pageIndex ?? 1;
pageSize = pageSize ?? 2;
return hotel.Customer
.Where<Customer>(whereLambda)
.OrderBy(c => c.ID)
.Skip<Customer>((pageIndex.Value - 1) * pageSize.Value)
.Take<Customer>(pageSize.Value)
.ToList();
}

public IList<DomainModel.Customer> GetEntitys(Func<DomainModel.Customer, bool> whereLambda)
{
hotel = HotelEntitesHelper.GetCurrentEntities();
return hotel.Customer.Where(whereLambda).ToList();
}


public Customer GetEntity(object id)
{
hotel = HotelEntitesHelper.GetCurrentEntities();
int key = (int)id;
return hotel.Customer.Where(q => q.ID == key).SingleOrDefault();
}
}
}


4.bll层接口:
 
   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPW.Hotel.DomainModel;
using SPW.Hotel.DTO;

namespace SPW.Hotel.IBLL
{
public interface ICustomerServices
{
bool UpdateCustomer(Customer customer);
bool DeleteCustomer(Customer customer);
bool AddCustomer(Customer customer);
Customer GetCustomer(object id);
IList<Customer> GetPageCustomers(Func<Customer, bool> whereLamabda, int? pageSize, int? pageIndex);
IList<Customer> GetCustomers(Func<Customer, bool> whereLamabda);
IList<Customer> GetAllCustomers();
IList<CustomerDTO> GetAllCustomersDTO();
}
}


5.Bll层实现
 
   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPW.Hotel.IBLL;
using SPW.Hotel.IDAO;
using System.Data.Entity;
using SPW.Hotel.DTO;

namespace SPW.Hotel.BLL
{
public class CustomerServices : ICustomerServices
{
//ICustomerRepository CustomerRepository { get; set; }
//public CustomerServices()
//{
// CustomerRepository = new EFDaoImpl.CustomerRepository();//如果不使用工厂模式和app配置,则在这里更改所使用的Dao类型
//}
private ICustomerRepository CustomerRepository = (ICustomerRepository)DaoAbstractFactory.DaoFactory.CreateInstances("Customer");
//private ICustomerRepository CustomerRepository = DaoAbstractFactory.DaoFactory.CreateCustomDaoInstances();//通过配置文件创建dao实例
public bool UpdateCustomer(SPW.Hotel.DomainModel.Customer customer)
{
return CustomerRepository.Update(customer);
}

public bool DeleteCustomer(DomainModel.Customer customer)
{
return CustomerRepository.DeleteById(customer.ID);
}

public bool AddCustomer(SPW.Hotel.DomainModel.Customer customer)
{
return CustomerRepository.Add(customer);
}

public IList<SPW.Hotel.DomainModel.Customer> GetPageCustomers(Func<SPW.Hotel.DomainModel.Customer, bool> whereLamabda, int? pageSize, int? pageIndex)
{
return CustomerRepository.GetByPage(whereLamabda, pageSize, pageIndex);
}

public IList<SPW.Hotel.DomainModel.Customer> GetCustomers(Func<SPW.Hotel.DomainModel.Customer, bool> whereLamabda)
{
return CustomerRepository.GetEntitys(whereLamabda);
}

public IList<CustomerDTO> GetAllCustomersDTO()
{
var temp = CustomerRepository.GetAll();
var result = from q in temp
select new CustomerDTO() { ID = q.ID, Name = q.Name, Phone = q.Phone };
return result.ToList<CustomerDTO>();
}

public SPW.Hotel.DomainModel.Customer GetCustomer(object id)
{
return CustomerRepository.GetEntity(id);
}


public IList<DomainModel.Customer> GetAllCustomers()
{
return CustomerRepository.GetAll();
}
}
}


6.工厂创建
 
   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using SPW.Hotel.IDAO;

namespace SPW.Hotel.DaoAbstractFactory
{
public class DaoFactory
{
public static readonly string daoPath = System.Configuration.ConfigurationSettings.AppSettings["DaoProvider"];
public static readonly string daoType = System.Configuration.ConfigurationSettings.AppSettings["DaoType"];

//获取dao的key对应的实例(通过缓存key)
public static object GetInstance(string cacheKey)
{
//处理缓存Cache:有缓存直接用,没缓存就创建一个
object cacheType = DataCache.GetCache(cacheKey);
if (cacheType == null)
{
try
{
cacheType = Assembly.Load(daoPath).CreateInstance(cacheKey);//创建一个配置文件中对应的Dao类型
DataCache.SetCache(cacheKey, cacheType);
}
catch (Exception ex)
{
throw new Exception("反射失败!" + ex.ToString());
}
}
return cacheType;
}

//创建custom实体类
public static ICustomerRepository CreateCustomDaoInstances()
{
//用接口创建实例实现多态,因为不管是sqlDao还是EFdao都继承自ICustomerRepository,所以创建他的实例就可以实现解耦
string cacheKey = daoPath + ".CustomerRepository";// +daoType;//这些是根据要创建实例的命名规则修改后的。因为我规定每个DaoImpl下的名字都一样,所以不需要类型了
return (ICustomerRepository)DaoFactory.GetInstance(cacheKey);
}

//创建order实体类
public static object CreateInstances(string entityName)
{
//用接口创建实例实现多态,因为不管是sqlDao还是EFdao都继承自ICustomerRepository,所以创建他的实例就可以实现解耦
string cacheKey = daoPath + "." + entityName + daoType;// +daoType;//这些是根据要创建实例的命名规则修改后的。因为我规定每个DaoImpl下的名字都一样,所以不需要类型了
return DaoFactory.GetInstance(cacheKey);
}
}
}


 
   

using System.Linq;
using System.Text;
using System.Web;

namespace SPW.Hotel.DaoAbstractFactory
{
public class DataCache
{
// 根据key返回缓存中的dao实例
public static object GetCache(string key)
{
System.Web.Caching.Cache dataCache = HttpRuntime.Cache;
return dataCache["key"];
}
//设置缓存信息,如果相同key,则覆盖value
public static void SetCache(string key, object value)
{
System.Web.Caching.Cache dataCache = HttpRuntime.Cache;
dataCache.Insert(key, value);
}

}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值