如何与业务层类实体框架模型类映射到n层架构 - ASP.NET MVC的 [英] How to map Entity Framework model classes with Business Layer class in n-tier architecture - ASP.NET-MVC
问题描述
我工作的MVC框架(ASP.NET MVC5,实体框架6)内的电子三层架构。我的应用程序分为哪些是业务层,数据接入层,信息库三个子项目(这包括仓库,工作单位)和ASP.NET MVC Web的应用程序。我努力理解业务数据和实体框架模型之间的映射。例如,如果我有实体框架模型类用户为
DAL - 用户模型
[表(用户)]
公共类用户
{
公共用户(){} [键]
公众诠释用户名{搞定;组; } [StringLength(250)]
[需要]
公共字符串名字{获得;组; } [StringLength(250)]
[需要]
公共字符串名字{获得;组; } [需要]
公众诠释年龄{搞定;组; } [StringLength(250)]
[需要]
公共字符串EmailAddress的{搞定;组; } 公众的ICollection< UserInGroup> UserInGroup {搞定;组; }
}
和在业务层我有用户类
BLL - 用户类别
公共类用户
{
私人字符串用户ID;
私人字符串的firstName;
私人字符串的lastName;
私人诠释年龄;
私人字符串EMAILADDRESS;
公共字符串用户名
{
{返回用户ID; }
集合{用户ID =价值; }
} 公共字符串名字
{
{返回的firstName; }
集合{=的firstName价值; }
} 公共字符串姓氏
{
得到{lastName的; }
集合{lastName的=价值; }
} 公众诠释年龄
{
{返回周岁; }
集合{年龄=价值; }
} 公共字符串EmailAddress的
{
{返回EMAILADDRESS; }
集合{EMAILADDRESS =价值; }
} 公共无效GetAllUser()
{
清单< App.DAL.Model.User> _user =新的List< DAL.Model.User>(); 使用(VAR _uow =新UserManagement_UnitOfWork())
{ _user =(从u在_uow.User_Repository.GetAll()
选择U).ToList(); }
}
}
我如何映射到一起;其次,参照方法GetAllUser(),我仍然需要使用用户模型类从DAL为了得到所有从数据库中的用户,哪里是我目前的理解;每层应该独立于彼此和我有数据访问层和业务层之间的抽象层即库中。我稍微混淆两者概念一起工作。我是在正确的轨道,或失去了一些东西。还需要对用户业务层的实际的答案。
您的问题更多的是它不具有一个尺寸适合所有人的解决方案设计/建筑。我最多能分享一些suggetions什么将我的一般的做一个相当典型的ASP.NET MVC +实体框架协议栈:
1。确保你的 BLL.User
类服从单一职责原则
您 BLL.User
类不应该如何检索关注自身 DAL.User
从通过实体数据库框架/工作单位。你应该只是有另一个类/层,它负责的是:
公共接口IUserRepository
{
IEnumerable的<使用者> GetAllUsers();
}
接着另外一个类来实现 IUserRepository
:
公共类UserRepository:IUserRepository
{
私人只读UserManagement_UnitOfWork _unitOfWork; 公共UserRepository(UserManagement_UnitOfWork的UnitOfWork)
{
_unitOfWork =的UnitOfWork;
} 公共IEnumerable的<使用者> GetAllUsers()
{
从u在_unitOfWork.User_Repository.GetAll返回()
选择U;
}
}
这样做会删除从 BLL.User
到 UserManagment_UnitOfWork
类,便于测试/嘲讽(即依赖单元测试可以写入嘲笑一个内存 IUserRepository
)
然后从你的控制器,只要有需要取回 BLL.Users
,您只需注入 IUserRepository $ C $的一个实例C>到控制器的构造函数:
公共类UserController的
{
私人只读IUserRepository _userRepository; 公共UserController的(IUserRepository userRepository)
{
_userRepository = userRepository;
} 公众的ActionResult指数()
{
//使用的IEnumerable&LT简单的例子; BLL.User>作为视图模型
返回查看(_userRepository.GetAllUsers()了ToList());
}
}
2。如何映射 DAL.User
到 BLL.User
这其实颇为相似点1号,你可以简单地有另一个接口/类对:
公共接口IUserMapper
{
BLL.User MapUser(DAL.User);
}公共类UserMapper:IUserMapper
{
公共BLL.User MapUser(DAL.User用户)
{
返回新BLL.User
{
名字= user.FirstName,
姓氏= user.LastName,
年龄= user.Age
//等等...
};
}
}
或者,如果你觉得写映射code是单调乏味的,可以考虑使用 AutoMapper 让您的code变成 Mapper.Map< DAL.User,BLL.User>(用户)
加分
- 您可以跳过
BLL.User
的私人
字段并将其转换为自动属性 - 您可以添加从
ValidationAttribute
派生属性来帮助验证在你的ASP.NET MVC应用程序
公共类用户
{
公共字符串userid {搞定;组; } [需要]
公共字符串名字{获得;组; } 公共字符串名字{获得;组; } [范围(0,int.MaxValue)
公众诠释年龄{搞定;组; } [电子邮件地址]
公共字符串EmailAddress的{搞定;组; }
}
I am working on e-tier architecture within MVC framework (ASP.NET MVC5, Entity Framework 6). My application is divided into three sub-projects which are Business-Layer, Data-Access-Layer, Repository (This include repository and Unit of Work) and ASP.NET MVC web-app. I am struggling to understand mapping between business data and entity framework model. for example if I have model class User in entity framework as
DAL - User Model
[Table("User")]
public class User
{
public User() { }
[Key]
public int UserID { get; set; }
[StringLength(250)]
[Required]
public string FirstName { get; set; }
[StringLength(250)]
[Required]
public string LastName { get; set; }
[Required]
public int Age { get; set; }
[StringLength(250)]
[Required]
public string EmailAddress { get; set; }
public ICollection<UserInGroup> UserInGroup { get; set; }
}
and in business layer I have user class
BLL - User Class
public class User
{
private string userID;
private string firstName;
private string lastName;
private int age;
private string emailAddress;
public string UserID
{
get { return userID; }
set { userID = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public string EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
public void GetAllUser()
{
List<App.DAL.Model.User> _user = new List<DAL.Model.User>();
using (var _uow = new UserManagement_UnitOfWork())
{
_user = (from u in _uow.User_Repository.GetAll()
select u).ToList();
}
}
}
How I map together and secondly; referring to method GetAllUser(), I still need to use User model class from DAL in order to get all the users from database, where is my current understanding is; each layer should be independent to each other and I have abstraction layer i.e. repository between data access layer and business layer. I am slightly confused of both concept working together. Am I on right track or missing something. also need practical answer on User business layer.
Your question is more about design/architecture which does not have a 'one-size-fits-all' solution. I can at most share some suggetions and what will I usually do in a fairly typical ASP.NET MVC + Entity Framework stack:
1. Make sure your BLL.User
class obeys the Single Responsibility Principle
Your BLL.User
class should not concern itself with how to retrieve DAL.User
from the database through Entity Framework/Unit of Work. You should simply have another class/layer that will be responsible for that:
public interface IUserRepository
{
IEnumerable<User> GetAllUsers();
}
Then another class to implement IUserRepository
:
public class UserRepository : IUserRepository
{
private readonly UserManagement_UnitOfWork _unitOfWork;
public UserRepository(UserManagement_UnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IEnumerable<User> GetAllUsers()
{
return from u in _unitOfWork.User_Repository.GetAll()
select u;
}
}
Doing so removes the dependency from your BLL.User
to the UserManagment_UnitOfWork
class and facilitates testing/mocking (i.e. unit tests can be written to mock an in-memory IUserRepository
)
Then from your controller, whenever there is a need to retrieve BLL.Users
, you simply inject an instance of IUserRepository
to the controller's constructor:
public class UserController
{
private readonly IUserRepository _userRepository;
public UserController(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public ActionResult Index()
{
// Simple example using IEnumerable<BLL.User> as the view model
return View(_userRepository.GetAllUsers().ToList());
}
}
2. How to map DAL.User
to BLL.User
It's actually quite similar to point number 1, you can simply have another interface/class pair:
public interface IUserMapper
{
BLL.User MapUser(DAL.User);
}
public class UserMapper : IUserMapper
{
public BLL.User MapUser(DAL.User user)
{
return new BLL.User
{
FirstName = user.FirstName,
LastName = user.LastName,
Age = user.Age
// etc...
};
}
}
Or, if you think writing mapping code is tedious, consider using AutoMapper so that your code becomes Mapper.Map<DAL.User, BLL.User>(user)
Bonus points
- You can skip those
private
fields inBLL.User
and convert them to auto-properties - You can add attributes that are derived from
ValidationAttribute
to aid validation in your ASP.NET MVC application
public class User
{
public string UserId { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
[Range(0, int.MaxValue)]
public int Age { get; set; }
[EmailAddress]
public string EmailAddress { get; set; }
}
这篇关于如何与业务层类实体框架模型类映射到n层架构 - ASP.NET MVC的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!