将实体与存储库链接

时间:2014-06-22 08:33:42

标签: c# asp.net-mvc entity-framework asp.net-mvc-4 repository-pattern

我对存储库模式相当新,我有以下项目结构 -

  

DAL - >核心 - >网络

我可以单独列出所有客户和用户的 ASP.NET MVC ,但我需要在用户上分配给每个用户的客户列表(Customers.PrimaryUser)模型。

如何从实体框架中获取关系以在模型中显示?

DAL

包含实体框架模型

UserService.cs

public class UserService : ServiceBase<IUserModel>, IUserService
{
    public UserService()
        : this(new UserRepository())
    {
    }

    private IUserRepository _userRepository;
    public UserService(IUserRepository userRepository)
    {
        _userRepository = userRepository ?? new UserRepository();
    }

    protected override Type LogPrefix
    {
        get { return this.GetType(); }
    }

    public UserListViewModel GetUserList(int PageSize, int CurrentPage)
    {
        try
        {
            if ((CurrentPage == 0) || (PageSize == 0))
                return null;

            IQueryable<User> query = _userRepository.GetQueryable();

            UserListViewModel model = new UserListViewModel();

            if (model.TotalPageCount != 1)
                query = query.OrderBy(x => x.Surname).Skip((CurrentPage - 1) * PageSize).Take(PageSize);

            model.UserList = new List<UserModel>();
            AutoMapper.Mapper.CreateMap<User, UserModel>()
                .ForMember(dest => dest.ID, opt => opt.MapFrom(src => src.ID));
            model.UserList = AutoMapper.Mapper.Map(query.ToList(), model.UserList);

            return model;
        }
        catch (System.Exception e)
        {
            this.LogError("Error getting the user list", e);
            return null;
        }
    }

    public UserModel GetSingle(Expression<Func<UserModel, bool>> whereCondition)
    {
        throw new NotImplementedException();
    }

    public void Add(UserModel entity)
    {
        throw new NotImplementedException();
    }

    public void Delete(UserModel entity)
    {
        throw new NotImplementedException();
    }

    public void Update(UserModel entity)
    {
        throw new NotImplementedException();
    }

    public IList<UserModel> GetAll(Expression<Func<UserModel, bool>> whereCondition)
    {
        throw new NotImplementedException();
    }

    public IList<UserModel> GetAll()
    {
        throw new NotImplementedException();
    }

    public IQueryable<UserModel> Query(Expression<Func<UserModel, bool>> whereCondition)
    {
        throw new NotImplementedException();
    }

    public long Count(Expression<Func<UserModel, bool>> whereCondition)
    {
        throw new NotImplementedException();
    }

    public long Count()
    {
        throw new NotImplementedException();
    }
}

UserModel.cs

public class UserModel : IUserModel
{
    private ICustomerService _customerService;

    public UserModel()
        : this(new CustomerService())
    {

    }

    public UserModel(ICustomerService customerService)
    {
        _customerService = customerService;
    }

    public int ID { get; set; }
    [DisplayName("Employee Number")]
    public string EmployeeNumber { get; set; }
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public string Position { get; set; }
    [DisplayName("Email Address")]
    public string Email { get; set; }
    public string Password { get; set; }
    public UserType UserType { get; set; }
    public UserStatus UserStatus { get; set; }
    public DateTime DateCreated { get; set; }
    public int CreatedBy { get; set; }
    public DateTime LastUpdated { get; set; }
    public int LastUpdateBy { get; set; }

    [DisplayName("Full Name")]
    public string SurnameFirstName
    {
        get { return Surname + ", " + Firstname; }
    }
}

CustomerModel.cs

public class CustomerModel
{
    public int ID { get; set; }
    public string CompanyName { get; set; }
    public int BillingAddress { get; set; }
    public string Notes { get; set; }
    public int CustomerType { get; set; }
    public int RefSource { get; set; }
    public DateTime RefDate { get; set; }
    public string RefPerson { get; set; }
    public int RefType { get; set; }
    public string RefNotes { get; set; }
    public int PrimaryUser { get; set; }
    public DateTime DateCreated { get; set; }
    public int CreatedBy { get; set; }
    public DateTime LastUpdated { get; set; }
    public int LastUpdateBy { get; set; }
}

1 个答案:

答案 0 :(得分:2)

让客户列表成为用户的孩子:

然后您可以在客户服务中提取/映射列表。

首先将CustomerList添加到您的用户ViewModel:

公共类UserModel:IUserModel {     private ICustomerService _customerService;

public UserModel()
    : this(new CustomerService())
{

}

public UserModel(ICustomerService customerService)
{
    _customerService = customerService;
}

public int ID { get; set; }
....
public IEnumerable<CustomerModel> CustomerList { get; set; }

然后在您的UserService中添加客户回购以及用户回购

private IUserRepository _userRepository;
private ICustomerRepository _customerRepository;
public UserService(IUserRepository userRepository)
{
    _userRepository = userRepository ?? new UserRepository();
    _customerRepository = _customerRepository ?? new CustomerRepository();
}

最后,在您拉入用户后,请使用customerRepo

        model.UserList = new List<UserModel>();
        AutoMapper.Mapper.CreateMap<User, UserModel>()
            .ForMember(dest => dest.ID, opt => opt.MapFrom(src => src.ID));
        model.UserList = AutoMapper.Mapper.Map(query.ToList(), model.UserList);


        foreach ( var user in model.UserList )
        {
           var custList = _customerRepository.GetCustomersForUser(user.ID).ToList();
           user.CustomerList  = AutoMapper.Mapper.CreateMap<custList, IEnumerable<CustomerModel>>();             
        }

        return model;

实施GetCustomersForUser(userID)

public IQueryable<Customer> GetCustomersForUser(int userID)
{
   return custCntxt.Customers.Where(c=>c.PrimaryUser == userID);
}