If WCF is in a MVC application, should it use the controller to access the database to keep 'DRY'

后端 未结 3 1740
谎友^
谎友^ 2021-01-14 20:07

I have an MVC application that accesses SQL and Windows Azure. The logical flow looks like this:

Person <--> View <--> Controller.ConvertPerson         


        
3条回答
  •  悲&欢浪女
    2021-01-14 20:27

    I would refactor the code like this - move the functionality to convert from Person to PersonEntity and vice versa to a separate mapper, move saving functionality to separate repository as well, and move controller's code for invoking mapper and repository to separate service too.
    So methods in your controller will look similar to:

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

    And in your WCF service you'll be able to reuse the code. In order to validate the classes in WCF using DataAnnotations attributes, you can use the approach similar to the following - http://blog.jorgef.net/2011/01/odata-dataannotations.html

提交回复
热议问题