Not sure how to register controller and repository correctly

514 views
Skip to first unread message

bioluminescence

unread,
Dec 7, 2010, 8:36:35 AM12/7/10
to Autofac
Hi,

I am using MVC 2 and I am new to Autofac. I'm not sure what I am
doing wrong, I know it is something. I want to register my
controllers and repository.

My NewsController constructor receives an INewsRepository parameter.
NewsPository implements INewsRepository. How would I register this?
This is what I currently have (I know it is wrong):

var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.Register<NewsRepository>().As<INewsRepository>().SingletonScoped();

The compiler error is: No overload for method 'Register' takes 0
arguments

I assume that I am doing it wrong in registering all the controllers
as once like I did seeing that the NewsController constructor requires
a parameter?

Please can someone help me with the correct code?

Thanks
Brendan

Nicholas Blumhardt

unread,
Dec 7, 2010, 5:45:10 PM12/7/10
to aut...@googlegroups.com
Hi,

The method you're looking for is RegisterType(), e.g.:

  builder.RegisterType<NewsRepository>()

Cheers,
Nick


--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.


Aaron Powell

unread,
Dec 7, 2010, 5:46:48 PM12/7/10
to aut...@googlegroups.com
You want to use builder.RegisterType<NewsRepository>(). Use the builder.Register<T> method if you want to handle construction of the object a bit more custom, ie:
builder.Register<NewsRepository>(x => new NewsRepository(SomeMethodToGetConfig(()).As<INewsRepository>()

Aaron Powell
Umbraco Ninja

http://www.aaron-powell.com | http://twitter.com/slace | Skype: aaron.l.powell | MSN: aaz...@hotmail.com


bioluminescence

unread,
Dec 10, 2010, 11:16:21 AM12/10/10
to Autofac
Hi,

Thanks. Isn't this where I tell it to inject NewsRepository for
INewsRepository? By using builder.RegisterType<NewsRepository>() how
would it know to inject NewsRepository for INewsRepository?

Brendan



On Dec 8, 12:46 am, Aaron Powell <m...@aaron-powell.com> wrote:
> You want to use builder.RegisterType<NewsRepository>(). Use the
> builder.Register<T> method if you want to handle construction of the object
> a bit more custom, ie:
> builder.Register<NewsRepository>(x => new
> NewsRepository(SomeMethodToGetConfig(()).As<INewsRepository>()
>
> Aaron Powell
> Umbraco Ninja
>
> http://www.aaron-powell.com|http://twitter.com/slace| Skype:
> aaron.l.powell | MSN: aaz...@hotmail.com
>
> On Wed, Dec 8, 2010 at 12:36 AM, bioluminescence <brendan.v...@gmail.com>wrote:
>
> > Hi,
>
> > I am using MVC 2 and I am new to Autofac.  I'm not sure what I am
> > doing wrong, I know it is something.  I want to register my
> > controllers and repository.
>
> > My NewsController constructor receives an INewsRepository parameter.
> > NewsPository implements INewsRepository.  How would I register this?
> > This is what I currently have (I know it is wrong):
>
> > var builder = new ContainerBuilder();
> > builder.RegisterControllers(Assembly.GetExecutingAssembly());
> > builder.Register<NewsRepository>().As<INewsRepository>().SingletonScoped();
>
> > The compiler error is: No overload for method 'Register' takes 0
> > arguments
>
> > I assume that I am doing it wrong in registering all the controllers
> > as once like I did seeing that the NewsController constructor requires
> > a parameter?
>
> > Please can someone help me with the correct code?
>
> > Thanks
> > Brendan
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Autofac" group.
> > To post to this group, send email to aut...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > autofac+u...@googlegroups.com<autofac%2Bunsu...@googlegroups.com>
> > .

bioluminescence

unread,
Dec 10, 2010, 10:46:48 AM12/10/10
to Autofac
Hi,

Thanks. Isn't this where I tell it to inject NewsRepository for
INewsRepository? By using builder.RegisterType<NewsRepository>() how
would it know to inject NewsRepository for INewsRepository?

Brendan



On Dec 8, 12:46 am, Aaron Powell <m...@aaron-powell.com> wrote:
> You want to use builder.RegisterType<NewsRepository>(). Use the
> builder.Register<T> method if you want to handle construction of the object
> a bit more custom, ie:
> builder.Register<NewsRepository>(x => new
> NewsRepository(SomeMethodToGetConfig(()).As<INewsRepository>()
>
> Aaron Powell
> Umbraco Ninja
>
> http://www.aaron-powell.com|http://twitter.com/slace| Skype:
> aaron.l.powell | MSN: aaz...@hotmail.com
>
> On Wed, Dec 8, 2010 at 12:36 AM, bioluminescence <brendan.v...@gmail.com>wrote:
>
> > Hi,
>
> > I am using MVC 2 and I am new to Autofac.  I'm not sure what I am
> > doing wrong, I know it is something.  I want to register my
> > controllers and repository.
>
> > My NewsController constructor receives an INewsRepository parameter.
> > NewsPository implements INewsRepository.  How would I register this?
> > This is what I currently have (I know it is wrong):
>
> > var builder = new ContainerBuilder();
> > builder.RegisterControllers(Assembly.GetExecutingAssembly());
> > builder.Register<NewsRepository>().As<INewsRepository>().SingletonScoped();
>
> > The compiler error is: No overload for method 'Register' takes 0
> > arguments
>
> > I assume that I am doing it wrong in registering all the controllers
> > as once like I did seeing that the NewsController constructor requires
> > a parameter?
>
> > Please can someone help me with the correct code?
>
> > Thanks
> > Brendan
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Autofac" group.
> > To post to this group, send email to aut...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > autofac+u...@googlegroups.com<autofac%2Bunsu...@googlegroups.com>
> > .

Nicholas Blumhardt

unread,
Dec 12, 2010, 1:47:55 AM12/12/10
to aut...@googlegroups.com
Hi Brendan,

The same configuration syntax works - 

   builder.RegisterType<NewsRepository>().As<INewsRepository>();

The method you're using, "Register(Type)", was renamed in v. 2.0 to: "RegisterType(Type)".

Cheers,
Nick

To unsubscribe from this group, send email to autofac+u...@googlegroups.com.

bioluminescence

unread,
Dec 15, 2010, 11:39:18 AM12/15/10
to Autofac
Will be sufficient?

var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<NewsService>().As<INewsService>();

So as I understand it, where ever INewsService is used (like in my
example in the controllers constructor) NewsService will be injected?



On Dec 12, 8:47 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Hi Brendan,
>
> The same configuration syntax works -
>
>    builder.RegisterType<NewsRepository>().As<INewsRepository>();
>
> The method you're using, "Register(Type)", was renamed in v. 2.0 to:
> "RegisterType(Type)".
>
> Cheers,
> Nick
>
> > <autofac%2Bunsu...@googlegroups.com<autofac%252Buns...@googlegroups.com>

bioluminescence

unread,
Dec 15, 2010, 11:45:50 AM12/15/10
to Autofac
I tried to implement it in my previous email, but then I get an
error. I am using MVC 3 with the assemblies that were loaded on 16
October. Can this be the reason why? I don't know how to get the
latest build for MVC 3. I don't understand the client software, too
complicated:

None of the constructors found with 'Public binding flags' on type
'Nedbank.Recognition.Core.Services.NewsService' can be invoked with
the available services and parameters:
Cannot resolve parameter
'Nedbank.Recognition.Core.Repositories.INewsRepository newsRepository'
of constructor
'Void .ctor(Nedbank.Recognition.Core.Repositories.INewsRepository)'.



On Dec 12, 8:47 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Hi Brendan,
>
> The same configuration syntax works -
>
>    builder.RegisterType<NewsRepository>().As<INewsRepository>();
>
> The method you're using, "Register(Type)", was renamed in v. 2.0 to:
> "RegisterType(Type)".
>
> Cheers,
> Nick
>
> > <autofac%2Bunsu...@googlegroups.com<autofac%252Buns...@googlegroups.com>

bioluminescence

unread,
Dec 15, 2010, 11:22:33 AM12/15/10
to Autofac
Thanks.

Just another question. How would I register a specific controller
that received a specific parameter? My controller looks like this, so
I will need to specify the parameter some how:

public NewsController(INewsService newsService)
{
this.newsService = newsService;
}

In this case do I need to register the controller individually, or
should I still register it all together?



On Dec 12, 8:47 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Hi Brendan,
>
> The same configuration syntax works -
>
>    builder.RegisterType<NewsRepository>().As<INewsRepository>();
>
> The method you're using, "Register(Type)", was renamed in v. 2.0 to:
> "RegisterType(Type)".
>
> Cheers,
> Nick
>
> > <autofac%2Bunsu...@googlegroups.com<autofac%252Buns...@googlegroups.com>

Nicholas Blumhardt

unread,
Dec 15, 2010, 5:06:14 PM12/15/10
to aut...@googlegroups.com
Register it all together - the default behaviour is what you're after.

BTW, Stack Overflow (we have an Autofac tag over there) is generally a better place for basic usage questions as answers over there are easier for others to find via Google. There's also less moderation delay :)

Good luck!
Nick

To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages