Simple use of Autofac and simple use of Autofac

Source: Internet
Author: User

Simple use of Autofac and simple use of Autofac

Record the process of learning Autofac today.

I have been confused about IoC and DI before. I have a preliminary understanding of IoC and DI only after studying the articles of my predecessors. Thank you for your selfless dedication!

Article address:

The understanding of dependency injection and control inversion is too good.

Chapter 2 IoC 2.1 IoC basics -- Spring3

What? It is clearly the basic use of recording Autofac...

Okay, go back to the topic.

First, build a time output program:

public interface IOutput{    void Write(string content);}
public class ConsoleOutput : IOutput{    public void Write(string content)    {        Console.WriteLine(content);    }}    
public interface IDateWrite{    void WriteDate();}
public class TodayWrite : IDateWrite{    private IOutput _output;    public TodayWrite(IOutput output)    {        this._output = output;    }    public void WriteDate()    {        this._output.Write(DateTime.Now.ToShortDateString());    }}    
Class Program {static void Main (string [] args ){
// Create an output object leleoutput ConsoleOutput = new consoleOutput ();
// Create a date output object and inject the output object TodayWrite todayWrite = new TodayWrite (consoleOutput) on which the date output object depends by using the constructor );
// Execute the output date method todayWrite. WriteDate (); Console. Read ();}}

The above is the original implementation method. Now we can see that it has a reasonable dependency structure, so we can use it together with Autofac.

First, add the reference of Autofac.

Open tool> choose NuGet Package Manager> Manage NuGet packages for the solution

Do not reference Autofac after installation

using Autofac;

Next, refactor the Program class.

class Program{    private static IContainer Container { get; set; }    static void Main(string[] args)    {        var builder = new ContainerBuilder();        builder.RegisterType<ConsoleOutput>().As<IOutput>();        builder.RegisterType<TodayWrite>().As<IDateWrite>();        Container = builder.Build();        WriteDate();        Console.Read();    }    static void WriteDate()    {        using (ILifetimeScope scope = Container.BeginLifetimeScope())        {            var write = scope.Resolve<IDateWrite>();            write.WriteDate();        }    }}

Finally, paste the Autofac document address: Autofac

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.