1

I have a web forms application (myWebForms)

I have a class library project (myClassLibrary) that has a class called "myClass"

WebForms references myClassLibrary

I have added Autofac references to the web forms application, set the global asax to resolve "myClass" etc...

I can see that in my aspx code behind, the public property I added for AutoFac, is instantiated correctly by AutoFac.

All this is great so far, however, my actual project is a lot more complex than this, and what I need to do is have access to the resolved "myClass" From within myClassLibrary

How do I achive this? (do I inject the container into myClass from the web forms project?, do I somehow reference the web forms global property, or do I build the container again within myClassLibrary?)

treendy
  • 443
  • 3
  • 16

1 Answers1

0

First, I'd recommend checking out the Autofac documentation on web forms and quick starts. I think you may have a lot of questions answered by doing that, though I understand it's a lot. DI is complicated, and I'm afraid that providing just a "quick answer" here may lead you to an incorrect understanding of what's going on.

In general...

  • You register types with Autofac that you want to inject. This includes all the types your web forms will need as well as all the dependencies those types will need.
  • Autofac, via its integration, will resolve dependencies and put them in your web forms. If those objects also have dependencies (e.g., constructor parameters), then Autofac will also figure those out automatically and plug them in.

Say your web form needs a property called IEmailSender...

public IEmailSender EmailSender { get; set; }

Your email sender object may need some other dependency, like a network socket factory or something.

public EmailSender(ISocketFactory socketFactory)

You would register both of these in the container. It doesn't matter which assembly they come from. You have to register them into the Autofac container for it to work.

builder.RegisterType<EmailSender>().As<IEmailSender>();
builder.RegisterType<TcpFactory>().As<ISocketFactory>();

When your web form gets the IEmailSender, Autofac will resolve the TcpFactory first, then provide that in the constructor of EmailSender, then that will be handed to your web form.

Again, a lot of this is covered in the docs and examples. While I realize there's a lot and it can be overwhelming, I strongly urge you to walk through that info because it can save you a lot of time and pain in the long run.

Travis Illig
  • 23,195
  • 2
  • 62
  • 85
  • Thanks you, I have read up a lot now and managed to get Autofac working for a few areas, however, im still unsure of the best approach for this. the only option i see so far is to adjust the constructor to be "dumb", then can just inject that in. And where I use to instantiate the object, i replace that with an "myObject.initiate(a,b)" method that i added to myObject (which replaces the constructor). But i heard this is a code smell, and i also dont want to pass the contain from my webform to my library to allow resolve within library (which i heard also is a code smell) – treendy Apr 02 '19 at 11:19