If the WCF is in the MVC application, should it use the controller to access the database in order to preserve "DRY",

I have an MVC application that accesses SQL and Windows Azure. The logical flow is as follows:

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

ConvertPersonHere is the answer to this stack overflow question and converts the Model object to a storage object

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
}
  • Now that I am adding WCF to the mix, how do I access data functions? Suppose I have a method .Save(Person)in the controller and want Save(Person)from my WCF call.

  • Do I need to abstract data actions in the controller?

+2
source share
3 answers

: Person PersonEntity mapper, , mapper .
:

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

WCF . WCF DataAnnotations, , : http://blog.jorgef.net/2011/01/odata-dataannotations.html

+3

, Mvc Wpf, . , . , UI (MVC WPF) . , .

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 ( ui)
  • Wpf
  • → PersonForm

  • Person
  • IPersonRepository

  • Azure Person
+2

I know this is tangent, but if you are mixing WCF and ASP.NET MVC, you should at least know OpenRasta . A good start is a podcast with patterns with the main player code .

(No, this is not even intended to answer your question!)

0
source

Source: https://habr.com/ru/post/1787262/


All Articles