Jackal
Jackal

Reputation: 3521

Razor Pages with Services and Repositories

I'm trying to understand how to separate concerns in my application and I have used Repository pattern before,

Problem is that i don't understand where the service comes in.

Let's say i have a simple CreateModel

public class CreateModel : PageModel
{
    private readonly IGenericRepository _genereicRepository;

    public CreateModel(IGenericRepository genericRepository)
    {
        _genereicRepository = genericRepository;
    }

    [BindProperty]
    public Entity Entity { get;set; }

    public void OnGet()
    {
        var entities = _genericRepository.GetEntities();
    }

    public void OnPost()
    {
        _genereicRepository.AddEntity(Entity);
        _genereicRepository.SaveChanges();
    }
}

and the rest is up to the repository to do the database calls.

Now why would i need a service in here and what would it exactly handle or abastract even more since there is nothing else to abstract?

Upvotes: 0

Views: 923

Answers (1)

zmbq
zmbq

Reputation: 39029

You may not need services, it all depends on how complex your system is. If all your controller methods do is done with one object and one repository call, I wouldn't add a service.

I add services when the operations the controllers want to perform are getting too complicated to keep them in the controller. For instance, if one controller method updates multiple objects and requires some logic (sometimes this group of objects is updated, sometimes that group) - I put that logic in a service.

Upvotes: 2

Related Questions