将实体与存储库链接起来

本文关键字:链接 起来 存储 实体 | 更新日期: 2023-09-27 18:09:38

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

DAL -> Core -> Web

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

我如何从实体框架的关系显示在模型中?

木豆

包含实体框架模型

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; }
}

将实体与存储库链接起来

将客户列表设置为user:

那么你可以在你的客户服务中拉/映射列表。

首先将CustomerList添加到用户ViewModel:

类UserModel: IUserModel{private iccustomerservice _customerService;

public UserModel()
    : this(new CustomerService())
{
}
public UserModel(ICustomerService customerService)
{
    _customerService = customerService;
}
public int ID { get; set; }
....
public IEnumerable<CustomerModel> CustomerList { get; set; }

然后在你的UserService中添加客户Repo以及用户Repo

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);
}