Having the following classes:
Person.cs
namespace Project.Domain
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Money { get; set; }
public decimal CreditScore { get; set; }
public Person()
{
Money = 0;
CreditScore = 0;
}
public void RandomCreditScore()
{
CreditScore = Random.GetRandomDecimal();
}
}
}
PersonService.cs
namespace Project.Service
{
public class PersonService
{
public void Save(Person person)
{
personRepository.Save(person);
}
public void Save(PersonViewModel personViewModel)
{
Person person = PersonAssembler.Assemble(personViewModel);
if (personViewModel.randomCreditScore)
person.RandomCreditScore();
personRepository.Save(person);
}
}
}
PersonController
namespace Project.Controllers
{
public class PersonController
{
public ActionResult Create(PersonViewModel personViewModel)
{
Person person = PersonAssembler.Assemble(personViewModel);
if (personViewModel.randomCreditScore)
person.RandomCreditScore();
personService.Save(person);
}
public ActionResult CreateAlternative(PersonViewModel personViewModel)
{
personService.Save(personViewModel);
}
}
}
And having a view in which a user can input the name, money and checkbox "set a random credit score"; which of the two controller methods would be preferred? Create or CreateAlternative?
In Create we manage the conversion from the view-model to the model in the controller and we avoid the service layer to have to know the view-model, which I think is a good thing. But on the other hand we are giving the controller the responsibility to know when to set a random credit score, given the user input.
In CreateAlternative well.. It's the opposite.
Another alternative would be to make the assembler handle the logic of when to calculate a random credit score, but I've haven't seen this much.
Is one of this alternatives a better option? Is there any added advantage of using any of them?