Autofac 依赖注入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac.Builder;
using Autofac.Configuration;
using Autofac;
/// by rhythmk
namespace AutofacApp
{
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterType<DBManage>();
builder.RegisterType<SQLDB>().As<IDataBase>();
//using (var container=builder.Build(ContainerBuildOptions.Default))
//{
var container = builder.Build(ContainerBuildOptions.Default);
var manage = container.Resolve<DBManage>();
manage.Select();
manage.Update();
//}
Console.ReadKey();
}
}


public interface IDataBase
{
void Update();
void Select();
}

public class SQLDB : IDataBase
{

#region IDataBase 成员

public void Update()
{
Console.WriteLine("sqlDB Update");
}

public void Select()
{
Console.WriteLine("sqlDB Select");
}

#endregion
}
public class MySqlDB : IDataBase
{

#region IDataBase 成员

public void Update()
{
Console.WriteLine("MySqlDB Update");
}

public void Select()
{
Console.WriteLine("MySqlDB Select");
}

#endregion
}

public class DBManage
{
private IDataBase _dataBase;

public DBManage(IDataBase m_dataBase)
{
this._dataBase = m_dataBase;
}

public void Update()
{
this._dataBase.Update();
}

public void Select()
{
this._dataBase.Select();
}

}



}

 

你可能感兴趣的:(Autofac 依赖注入)