1

I'm curious what's considered the (best) correct way to pass data to a service layer in ASP.net Core. Say I have a Person entity that has a relation to a Image (profile picture) entity and another relation to a Address entity.

Should the signature of the service method look like this:

public Person CreateCustomer(Person person, Address address, Image profilePicture)

Or would it make more sense to put all the needed data in an interface:

public Person CreateCustomer(ICustomerDetails details)

Using the last approach, I would be able to make my ViewModel inherent from ICustomerDetails and I could pass the ViewModel from the controller directly to the service layer.

What approach would be most advisable in this situation.

  • your CreateCustomer method doesnt make much sense. It returns a Person and take a Person as an input – Ewan Dec 24 '18 at 15:00
  • @Ewan The idea was that the Person entity would contain the person data needed for creating the database record and the method would return the Person created in database. – jerojitov Dec 24 '18 at 15:03
  • @jerojitov this question is impossible to answer because there is no one-size-fits-all correct approach to defining contracts. The contract should be exactly what you need it to be for you software to work. No more, no less. Don't over-design your code. – Ant P Dec 24 '18 at 15:15
  • The best approach is always the one that (in this order): works and meet your needs. As you may guess, we can not answer these questions for you. If you are fine with both, then apply KISS and YAGNI to make a choice – Laiv Dec 24 '18 at 19:37
  • "Using the last approach, I would be able to make my ViewModel inherent from ICustomerDetails and I could pass the ViewModel from the controller directly to the service layer." That's no ViewModel; it's a Model. – John Wu Dec 27 '18 at 10:01
1

The correct way to do this is to:

  1. Name your methods for what they do

    public void Repository.SavePerson(Person person)

  2. When you save a Person object, you only need to send the Person object

  3. Ensure your object has all the properties that are associated with it

eg

public class Person 
{
    public string Id;
    public string Name;
    public Image ProfileImage;
    public Address ContactAddress;
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.