I just need a few links to articles I can read up on or some basic explanations regarding the different patterns used in MVC (C#).
At present I tend to build my web apps using a view model pattern. For every view I have one view model. I like this approach purely because there can be so much junk that is not needed from the model and I can use some basic data annotations here.
I also now construct my viewmodels within the view model itself (Unsure if this is correct?) so that I can keep my controllers as simple as possible.
There are times however I have found myself adding in a lot of logic within my controller, I would assume this is fine also as to me that is what the controller is there for.
Now based on the above as I said I can quite happily build my apps without any major issues. However whilst doing my normal browsing of code examples etc I often find that there are so many other ways out there used by different developers to do essentially what I am doing above and I would like an explanation of they all fit together.
I often see mentioned "use your repository to do blah blah".. I do use repositorys "sometimes" but this is mainly for model querying that I know I will re-use in the future and it always turns in to a bit of a dumping ground. What is best practice here?
I also see mentioned "interfaces" and "service layers" I am totally lost here.. most examples to me seem to just be adding more and more steps to achieve the same goal. How/why are they used?
When I started reading your post I was thinking that maybe what you are looking for is an understanding of SOLID principles. And then you end by mentioning interfaces and service layers. Interesting.
There are plenty of articles celebrating the holy grail of SOLID and DRY (Many without understanding what DRY advocates are really proposing). But the general idea in the .NET world, is NOT to go to the autogenerated Page_Load in an aspx and start typing away all willy nilly until a page does what it is supposed to do. MVC to the rescue.
You say you have a model for each view. I would call that sound. Even if two models are identical, they are only equal, not the same. For example: A NewsItem is not an EventItem. If you want to expand on one, it should not effect the other.
Then you continue with saying you are producing your models in the view model itself. That sounds backwards. But you say you do so in order to keep your controller clean. Good! What is missing in your mindset, is services.
What you want to do is to move all code that actually perform any kind of work into services. A service can be based on an aspect, or on a feature or why not a control. Looking at one web project now, I see: VisitorService, NewsfeedService, CalendarService, CachingService, MainMenuService, HeaderService, FooterService etc etc ad infinitum.
In this scenario, the controller is only responsible for asking a service (or services), that performs some work, for a model. And then forward that model to a view.
Once you got 'business logic' into services you can easily apply IoC (Inversion of Control) to your projects if that makes you happy. I have not cast my vote on IoC yet. I have the eerie the benefits is not as great as advertized, and you can do without the code bloat for sure. But IoC do ask of you to think before you code.
For a very easy going tutorial on IoC, I recommend Ninject. Not only does it feature Ninjas, but samurais, swords and shuriken as well. That's a lot cooler than cars and animals.
https://github.com/ninject/ninject/wiki/Dependency-Injection-By-Hand
Controller:
In theory your controller should be only handling "data". Moving pieces of information from one place to another.
Little example:
All the business logic "in theory" should be behind some service layer. That way you can easilly test everything. Logic in controller makes some tests more difficult.
Interfaces:
Interface based design is very popular now. Especially with all the IOC Containers handling dependancy injection. But if you are starting with this concept don't bother about these keywords. If you know the Repository pattern, then try at first with IRepository interface and instead of accesing repository by concrete class, use IRepository. (Just change field in controller from Repository to IRepository).
Generaly about interfaces
You will see the benefit of interfaces in more complex scenarios, but there is one technique that will show you all the glory of this approach. Unit Testing + Mocking.
I can't say this is the best practice, but this is what I use, and why, and here we go:
1. The repositories.
They are structured this way:
There are three basic interfaces,
IRead<>
,IReadCreate<>
andIReadCreateDelete<>
.The all other interfaces look like this:
And all of them provides additional usefull functionality on the data source they depend on. It means, I cannot reach other typed repositories in my implementation repository. That should be done on Services. (Look below.)
The main goal of this approach is to show the calling code (from another assembly, because all my repositories, services and other contracts are defined (as interfaces) in separate DLL project) what it can do (like reading and creating items) and what it cannot do (like deleting items).
2. Services
Services and best way to implement your business logic. They should implement all your vital logic methods. To achive that kind of implementation they will need some repositories depency, and here it comes the
Dependency Injector
. I prefer to use Ninject, because it allows me to inject dependency properties like this:The goal of services is to eliminate business logic from controllers and from repositories.
3. ViewModels
Cool thing, as you told, but the reason is why are you building them up in theirselves is the thing I can't get. I am using the automapper for that (with its queryable extensions), which allows me to create views like this:
Let's say I have a view which needs an
IEnumerable<TicketViewModel>
model. What I do is:That's it. Simple call to repository, which makes calls to underlying data source (another pattern. I wont write about it, because its abstraction is needed only for testing.), which makes calls to database (or whatever you implement
IDataSource<T>
). Automapper automappically maps theTicket
toTicketViewModel
and form database I retrive the only needed for my ViewModel columns, including the cross-table in a single request.Conclusion
There are much to say more, but I hope this will give you some food for thought. All the patterns and programs I use are: