MVC Onion architecture, some questions

I am creating a project with Asp.net MVC 5, Web Api 2 and Entity Framework. I design it using the Onion architecture, so I have DAL, Service, and UI layers.

My DAL layer contains UnitOfWork and Repositories, my service layer contains services for business cases.

But I have the following questions:

  • Where can I use the method of saving (or fixing) work at the service level or in the user interface layer? if I use it at the service level, how do I handle cases that span multiple services?

  • I use DTO for webapi operations, should the service layer return a DTO or should the mapping be done in the user interface layer?

  • If the DTOs are in a separate project or in a user interface project? If they are in a separate project, should I use MVC attributes for validation?

+4
source share
1 answer

Your unit of work must exist at your service level. Each service call contains a business transaction within one unit of work.

    public ServiceResponse<Patient> Save(Patient patient, string userName)
    {
        Func<Patient> func = delegate
        {
            Authorize(patient, userName);
            Validate(patient, new PatientValidator());

            using (var context = _contextFactory())
            {
                return context.Save(patient, userName);
            }
        };
        return this.Execute(func);
    }

The service level should return your business entities, any mapping for network communication / json formatting should be done in the web api. This allows you to make the most of your services.

DTO , wire/json, , Web Api. , . Web Api, , FluentValidation.

https://github.com/carbonrobot/FullStackEF #, EF, Web Api

+6

All Articles