kevinius
kevinius

Reputation: 4618

ASP.NET MVC: Controller constructors

i'm just starting out with asp.net mvc. It's a long way before you can really get to a live project. At the moment i'm working to build a blog using the asp.net mvc unleashed book.

However, i don't understand the 2 constructors in the BlogController (see question below)

Thx...

FIRST

The BlogController has a private variable '_repository'

Private _repository As BlogRepositoryBase

Public MustInherit Class BlogRepositoryBase
    'blog entry methods
    Public MustOverride Function ListBlogEntries() As List(Of BlogEntry)
    Public MustOverride Sub CreateBlogEntry(ByVal BlogEntryToCreate As BlogEntry)
    Public MustOverride Function QueryBlogEntries() As IQueryable(Of BlogEntry)
End Class

NEXT

The controller has 2 constructors 'new' and 'new with a parameter'

Public Sub New()
    Me.New(New EntityFrameworkBlogRepository())
End Sub

Public Sub New(ByVal repository As BlogRepositoryBase)
    _repository = repository
End Sub

QUESTIONS

Upvotes: 1

Views: 8576

Answers (3)

takepara
takepara

Reputation: 10443

Coding custom IControllerFactory or DefaultControllerFactory inherits class. And SetControllerFactory global.asax.

Haaked becomes reference very much.

TDD and Dependency Injection with ASP.NET MVC

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532755

The default constructor is calling the constructor with a parameter with a new instance of a particular type of BlogRepositoryBase class. EntityFrameworkBlogRepository must derive from this base class. The reason that you specify the base class (I would have used an interface, but I digress) is so in your tests you can specify a different type of repository -- one, perhaps, that doesn't even connect to a database by instantiating it directly via the non-default constructor. The framework wiil always use the default constructor, thus you have to both provide it and provide a suitable implementation of the repository using it.

FWIW -- this is how I would do it (in C# -- my brain isn't working well enough to translate into VB, yet).

 protected IBlogRepository Repository { get; set; }

 public BlogController() : this( null ) {}

 public BlogController( IBlogRepository repository )
 {
      this.Repository = repository ?? new EntityFrameworkBlogRepository();
      ...
 }

Tested as

 public void Test()
 {
      var repository = MockRepository.GenerateMock<IBlogRepository>();

      var controller = new BlogController( repository );

      ...

      repository.VerifyAllExpectations();
 }

Upvotes: 3

Daniel Elliott
Daniel Elliott

Reputation: 22887

EntityFrameworkBlogRepository is derived from BlogRepositoryBase

The 'magic' in the constructors is called Dependency Injection. (Wiki has more on that here.) In short, it is a way of making your code more maintainable and testable by passing it it's dependencies ... if you change the repository type you need not rip out most of your code.

Kindness,

Dan

Upvotes: 0

Related Questions