I'm working on a MVC4/EF5 project and I really want to invest a lot of time in the design of my application.
I have made one policy/principle for myself. My controllers need to be dedicated, which means they can only interact with one specific repo.
E.g. (I'm just writing some pseudo code)
objects: User, Blog, Post, Comment
UserController -> UserRepo -> Handles the User object
BlogController -> BlogRepo -> Handles the Blog object
etc...
Now I'm looking into the following dilemma. What's the most performant to create/add new objects to the database with EF5.
1st Approach:
Add a function to User to add a Blog or add a Post.
addBlog(Blog b){this.Blog.addBlog(b);}
addPost(int blogid, Post p){this.Blog(b).addPost(p);}
For me this means that every post will initiate a usercontroller, inject a userrepo and will have a read operation in the User table to fetch the object. Then it will perform the addPost function and saves the changes.
2nd Approach:
Give the Blog and Post object some foreign key ids
Blog property: UserId
Post property: UserId, BlogId
This means whenever a blog is created, a blogcontroller will be initiated, a blogrepo will be injected and there is no 'LOOKUP' needed. The controller will just add the new Blog object to the context. (the UserId property is set from the websecurity context)
This also means that whenever a post is created, a postcontroller will be initiated, a postrepo will be injected and there is no 'LOOKUP' needed. The controller will just add the new Post object with the injected UserId and BlogId. (which are parameters of the controller action)
For me the second approach seems to devide the load on different tables instead of one. But the downside of this approach is that you can't really do model testing. Because there would be no function addBlog to the User object to be tested.
Is my question somewhat clear? I really want to build the most performant framework for my application. Also what is the impact of a User object with the following properties.
Virtual ICollection
Virtual IColleciton
They can both be retrieved by fetching the User object. (then there is lazy loading on the virtual objects)
Or is it better to let the dedicated BlogController index the blogs that belong to a certain user id.
Thanks a lot for all the advice and comments!
Kr