I'm coming from a WebForms world where all logic is located in the codebehind of the aspx-pages. After reading a few books on ASP.NET MVC, listening to some podcast and watching some videos on Tekpub, I've decided that it is time to approach things a bit differently.
Unfortunately, I'm already stuck.
I'm trying to build some sort of small and basic CMS in which I can add multiple websites.
I know I should keep my controllers thin, so I guess I should use some sort of service class (let's call it WebsiteService) to do this. I'm using Entity Framework for data access and my Views all use specific ViewModels. When I Create or Edit a Website, these four things should happen:
- Validate the input
- Add information about the Website to the database (Update information if it's an Edit)
- Create a directory on disk (Possibly rename the directory if it's an Edit)
- Add a host header to an IIS Website (Possible remove the old host header and a new one if it's an Edit)
Basically, I guess the WebsiteService should perform more advanced validation, write to the database, create/edit a directory, add/remove host headers and return something to the controller to indicate if it succeeded or not.
What should this class look like? I have a few questions to which I don't know the answer.
- Should the WebsiteService also translate the CreateWebsite ViewModel to the actual Website class or should something else do this so that the WebsiteService accepts an actual Website object?
- Basic input validation is done by using Validation attributes on the ViewModel. More extensive validation ("Is there already a Website with this domain name in the database?") should also be done. Should the WebsiteService do this as well?
- Should all 3 steps (save to database, create directory, add host header to IIS) be done in one public method (
WebsiteService.SaveWebsite(ViewModels.CreateWebsite website)
) or should I provide separate methods which the controller has to call? (I guess not because I suppose the call order is important.)