How do I handle table relationships with the repository pattern?

后端 未结 2 601
旧时难觅i
旧时难觅i 2021-02-07 14:08

I\'m implementing the repository pattern as part of an ASP.NET MVC site. Most examples I\'ve seen of repositories are fairly simple. For example here\'s a typical abstract repos

2条回答
  •  礼貌的吻别
    2021-02-07 14:35

    How does your app handle assigning a category to an item?

    Does it:

    1. Allows the user to view the item then assign it a category
    2. Allows the user to view the category then add items to it

    Let your app dictate which method you pick.

    This brings up the idea of having a Business Logic/Services layer and dealing with aggregate roots. In my applications, I always have a services layer where all the business logic is at. I found that doing this has made my application easier to understand and maintain/refactor. (Note: I also use generic repositories and put the complex repository look ups in separate functions in the proper service class)

    In your services layer, you'll have a function called AssignCategoryToItem which would then take the category (the aggregate root) and you would then add the item to that category and save the changes - although I would prefer passing in the IDs of the category and pulling it from the database before updating.

提交回复
热议问题