Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC pattern differences

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?

like image 504
munkee Avatar asked Dec 15 '12 08:12

munkee


2 Answers

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<> and IReadCreateDelete<>.

interface IRead<T>
{ 
    T FindOne(int id);
    IQueryable<T> GetOne(int id);
    IQueryable<T> FindAll(Expression<Func<T, bool>> predicate);
}

interface IReadCreate<T> : IRead<T>
{ 
    T Create();
    void Create(T entity);
}

interface IReadCreateDelete<T> : IReadCreate<T>
{ 
    void Delete(int id);
    void Delete(T entity);
    void DeleteWhere(Expression<Func<T, bool>> predicate);
}

The all other interfaces look like this:

interface ICategoriesRepository : IReadCreate<Category>
{
    IQueryable<Category> GetAllActive();
}

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:

internal class CategoriesService : ICategoryService
{
    public ICategoriesRepository CategoriesRepository { get; set; }
    public IWorkstationsRepository WorkstationsRepository { get; set; }

    // No constructor injection. I am too lazy for that, so the above properties 
    // are auto-injected with my custom ninject injection heuristic.

    public void ActivateCategory(int categoryId)
    {
        CategoriesRepository.FindOne(categoryId).IsActive = true;
    }
}

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:

public class FooController : Controller
{
     public IMappingEngine Mapping { get; set; } // Thing from automapper.
     public ITicketsRepository TicketsRepository { get; set; }

     public ViewResult Tickes()
     { 
         return View(TicketsRepository.GetAllForToday().Project(Mapping)
             .To<TicketViewModel>().ToArray();
     }
}

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 the Ticket to TicketViewModel 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:

  1. Automapper (mapping);
  2. Ninject (dependency injection);
  3. Repositories (data access);
  4. Data Source (data reads from .. well.. from data source);
  5. Services (data interactivity);
  6. ViewModels (data transfer objects);
  7. Maybe something else I'll edit to add about.
like image 147
AgentFire Avatar answered Sep 21 '22 18:09

AgentFire


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

like image 31
Michael Viktor Starberg Avatar answered Sep 19 '22 18:09

Michael Viktor Starberg