WCF 和 MVC3,系统架构。使用 WCF 传递视图模型?
我正在使用实体框架、Linq、WCF 和 MVC3。该系统已分为分层方法,我使用 WCF,因为多个系统将使用相同的数据访问和业务逻辑。
数据访问 – 业务逻辑 – API (WCF) – 服务类 – MVC
对于 MVC 应用程序中的每个视图,需要编写 LINQ 查询来访问数据库中的不同表并显示摘要,如下所示。
var users =
(from u in _userRepository.All
from upd in u.UserPropertyData.DefaultIfEmpty()
from upd1 in u.UserPropertyData.DefaultIfEmpty()
where (upd.UserPropertyType.Alias == "Forename") && (upd1.UserPropertyType.Alias == "Surname")
select new UserModel
{
Id = u.Id,
Forename = upd.Value,
Surname = upd1.Value,
EmailAddress = u.Email
});
该查询位于我的业务逻辑部分,并通过我的 WCF API 将 UserModel 传递到我的服务类,该服务类将模型传递到我的控制器,然后控制器显示在视图中。
这种方法似乎并不正确,因为数据访问和业务逻辑是通过传回“视图模型”耦合到 MVC 的。如果有人能想到更好的方法,我将不胜感激。
I am using Entity Framework, Linq, WCF and MVC3. The system has been split into a tier approach and I am using WCF because multiple systems will be using the same Data Access and Business Logic.
Data Access – Business Logic – API (WCF) – Service Classes – MVC
For each View in my MVC application, LINQ queries need to be wrote which access different tables in the database and show summaries as seen below.
var users =
(from u in _userRepository.All
from upd in u.UserPropertyData.DefaultIfEmpty()
from upd1 in u.UserPropertyData.DefaultIfEmpty()
where (upd.UserPropertyType.Alias == "Forename") && (upd1.UserPropertyType.Alias == "Surname")
select new UserModel
{
Id = u.Id,
Forename = upd.Value,
Surname = upd1.Value,
EmailAddress = u.Email
});
This query is in my Business Logic section and is passing a UserModel across my WCF API to my Service class which passes the Model to my Controller which is then displayed in a View.
This approach doesn’t seem correct because the Data Access and Business Logic are being coupled to MVC by passing back a ‘View Model’. If anyone can think of a better approach I would appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您是通过 WCF 进行数据访问吗?如果是这样,我将通过 WCF 服务返回业务数据(类),通过 MVC 应用程序的服务类映射到 ViewModel 类,然后将所有视图模型传递到控制器/视图。
如果我误解了你的问题,请纠正我。
If I understand it correctly, you're doing your data access through WCF? If so I would return the business data (classes) through the WCF services, do the mapping to ViewModel classes via the services classes of the MVC app and then pass all the view models through to the controllers/views.
Please correct me if I've misunderstood your question.