【问题标题】:If WCF is in a MVC application, should it use the controller to access the database to keep 'DRY'如果 WCF 在 MVC 应用程序中,是否应该使用控制器访问数据库以保持“DRY”
【发布时间】:2011-07-02 20:09:21
【问题描述】:

我有一个访问 SQL 和 Windows Azure 的 MVC 应用程序。逻辑流程如下所示:

Person <--> View <--> Controller.ConvertPersonHere(x) <--> StorageContext.DoDataAction <--> AzurePersonTableEntity

ConvertPersonH​​ere 是this Stack Overflow question 的答案,它将模型实体转换为存储实体

public class Person
{
    public string Name {get;set;}
    public int ID {get;set;}
}

public class PersonEntity : TableServiceEntity
{
    public string Name {get;set;}
    public int ID {get;set;}

    // Code to set PartitionKey
    // Code to set RowKey
}
  1. 现在我将 WCF 添加到组合中,我应该如何访问数据函数?假设我目前在控制器中有一个 .Save(Person) 的方法,并且想从我的 WCF 调用中 Save(Person)。

  2. 是否需要抽象出控制器中的数据操作?

【问题讨论】:

    标签: wcf model-view-controller rest architecture dry


    【解决方案1】:

    我会像这样重构代码 - 将用于从 Person 转换为 PersonEntity 的功能(反之亦然)移至单独的映射器,将保存功能也移至单独的存储库,并将用于调用映射器和存储库的控制器代码移至单独的服务.
    所以你的控制器中的方法看起来类似于:

    public ActionResult SomeMethod(Person person)
    {
        if (ModelState.IsValid)
        {
            _personService.Save(person)
            return View("Success");
        }
        return View();
    }
    

    在您的 WCF 服务中,您将能够重用代码。为了使用 DataAnnotations 属性验证 WCF 中的类,您可以使用类似于以下的方法 - http://blog.jorgef.net/2011/01/odata-dataannotations.html

    【讨论】:

    • 几个问题:“_personService”是什么样的?我的情况是“存储库” StorageContext 吗?我也很想听听您对此stackoverflow.com/questions/5087183/… 的意见
    • _personService 是一项用于与人员合作的服务。它也可以命名为存储库,但取决于实现。 Service for me 是一个与一个或多个存储库或/和其他服务一起使用的类。 Repository 是一个类,它只存储、加载、删除数据存储中的数据。是的,在您的情况下,它是一个“存储库” StorageContext。
    【解决方案2】:

    在此示例中,如果您的 Mvc 项目已消失并被 Wpf 项目替换,您的其他功能仍然可用。如果您有两个项目,它们可以引用核心功能。在其他项目中有与 UI(MVC 或 WPF)无关的实现。这样那些 UI 项目就可以引用这个功能。

    public interface IConverter<TDataModel, TModel> { TModel MapToDomain(TDataModel source);}
    public interface IPersonConverter : IConverter<PersonEntity, Person> { }
    public interface IPersonRepository { Person GetById(int id); }
    
    public class PersonConverter : IPersonConverter
    {
        public Person MapToDomain(PersonEntity source)
        {
            return new Person { ID = source.ID, Name = source.Name };
            //or use an AutoMapper implementation   
        }
    }
    
    public class PersonRepository : IPersonRepository
    {
        private readonly IPersonConverter _personConverter;
    
        public PersonRepository(IPersonConverter personConverter)
        {
            _personConverter = personConverter;
        }
    
        public Person GetById(int id)
        {
            PersonEntity personEntity = new PersonEntity(); //get from storage
            return _personConverter.MapToDomain(personEntity);
        }
    }
    
    public class MvcController
    {
        private readonly IPersonRepository _personRepository;
    
        public MvcController(PersonRepository personRepository)
        {
            _personRepository = personRepository;
        }
    
        public ActionResult SomeMethod(int id)
        {
            Person person = _personRepository.GetById(id);
    
            //make your view model based on the person domain model
            //with another convert / map, to fit view as personForm
            //(if this is overkill you can use person).
    
            return View(personForm);
        }
    }
    

    Mvc 或 Wpf 项目

    • PersonForm(用户界面模型)
    • 控制器或 Wpf 类
    • Person -> PersonForm 转换器
    • 列表项

    核心项目

    • 人
    • IPersonRepository

    基础设施项目

    • 个人资料库
    • 个人实体
    • Azure 人员表实体
    • 存储上下文

    【讨论】:

    • 谢谢...“数据访问层”在哪里。有人告诉我这是我进行异常处理的地方
    • 我是否也应该将基础设施项目中的对象称为频道as described here?在我看来,Azure 队列与表实体处于同一级别
    • 基础设施项目是数据访问层,具有数据访问实现。至于频道命名约定我不确定。
    • 如果基础设施是 DAL,应该在存储库或 storageContext 中管理异常吗?
    • 这取决于异常发生的位置以及您想用它做什么,由您来设计。您还可以创建自己的异常类型(可能在核心中),并且作为捕获的 DAL 异常的结果,抛出要捕获的异常类型或进一步出错...
    【解决方案3】:

    我知道这是一个切线,但如果你正在混合 WCF 和 ASP.NET MVC,你至少应该知道OpenRasta。一个好的开始是Herding Code podcast with the main contributor。

    (不,这甚至不是为了回答您的实际问题!)

    【讨论】:

    • 我只是快速浏览了一下...它支持 .NET 4 和 MVC 3 吗?我没有看到任何 .NET 4 信息。不确定是否重要
    • @makerofthings:它不支持 MVC3,它取代它作为通过 http 交付东西的一种方式 - 即,以统一的方式交付 RESTful 服务/站点,而不是通过古怪的方式对服务方面进行特殊封装WCF 之类的装置和/或 SOAP 之类的重型协议。如果它不支持.NET 4,我会感到非常惊讶。如果您对链接的关注没有让您走那么远,如果它适合您,我会感到非常惊讶......
    • 我已经从 MVC 路由中排除了 *.svc,我不会忽略它。我会花更多时间在上面,但我正忙于学习 jquery; ScottGu 发布了今晚结束的免费在线课程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 2023-03-18
    • 2010-10-21
    相关资源
    最近更新 更多