1、通过配置的方式使用Autofac
<configSections>
<section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
</configSections>
<autofac >
<components>
<component type="AutofacDll.SqlDAL, AutofacDll" service="AutofacDll.IDAL,AutofacDll" />
</components>
</autofac>
2、通过RegisterModule方式使用配置文件中的信息
static void Main(string[] args) { ContainerBuilder builder = new ContainerBuilder();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
using (IContainer container = builder.Build())
{
IDAL dal = container.Resolve<IDAL>();
dal.Insert("111");
}
Console.ReadKey(); }
3、类库:
namespace AutofacDll
{
public interface IDAL
{
void Insert(string commandText);
}
public class SqlDAL : IDAL
{
public void Insert(string commandText)
{
Console.WriteLine("使用sqlDAL添加相关信息");
}
}
public class OracleDAL : IDAL
{
public void Insert(string commandText)
{
Console.WriteLine("使用OracleDAL添加相关信息");
}
}
}
联系客服