【问题标题】:Linking entities with repositories将实体与存储库链接
【发布时间】:2014-06-22 08:33:41
【问题描述】:

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

DAL -> 核心 -> 网络

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

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

DAL

包含实体框架模型

用户服务.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; }
}

【问题讨论】:

  • 代码太多了..你能把它缩小到你遇到问题的具体部分
  • 这已被编辑 - 现在显示用户服务和用户/客户模型。关系应该是一个用户有很多客户 (Customer.PrimaryUser)。

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


【解决方案1】:

将客户列表设为用户的子级:

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

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

公共类用户模型:IUserModel { 私有 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);
}

【讨论】:

  • 这看起来像是前进的方向,但是我正在努力实现 _customerRepository.GetCustomersForUser(user.ID) 方法。你能举个例子吗?
  • @MattBaughan,实施回购行动是一个挑战,因为您没有透露太多关于您的回购的信息,但我猜它看起来像我更新的答案。这是一个指导方针......
  • 完美,我能看到的最后一个问题是 GetUserList 方法中的变量 custList 有以下错误 - '无法解析符号 custList'。
  • @MattBaughan,我认为 linq 查询应该返回一个 tolist()... 错误更新...这是因为 var anonymous 需要一个具体的类型来处理。
  • @MattBaughan,我在错误的区域添加了列表。它不应该在被调用的函数中,而应该在调用者的内部...这样 var 就明白要获取一个列表...
猜你喜欢
  • 2016-10-08
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 2014-11-12
相关资源
最近更新 更多