autofac 初步学习
//数据处理接口
public interface IDal<T> where T : class
{
void Insert (T model);
void Update(T model);
void Delete(T model);
T Select(Int32 id);
}
//MsSql接口
public class MsSqlDal<T> : IDal<T> where T:class ,new ()
{
public void Insert(T model)
{
Console.WriteLine("您向MsSql添加了一条记录!" + typeof(T).Name + ";model:" + model.ToString());
}
public void Update(T model)
{
Console.WriteLine("您向MsSql修改了一条记录!" + typeof(T).Name + ";model:" + model.ToString());
}
public void Delete(T model)
{
Console.WriteLine("您向MsSql删除了一条记录!" + typeof(T).Name + ";model:" + model.ToString());
}
public T Select(Int32 id)
{
Console.WriteLine("您向MsSql查询了一条记录!" + typeof(T).Name );
return new T();
}
}
//MySql接口
public class MySqlDal<T> : IDal<T> where T : class ,new()
{
public void Insert(T model)
{
Console.WriteLine("您向MySql添加了一条记录!" + typeof(T).Name + ";model:" + model.ToString());
}
public void Update(T model)
{
Console.WriteLine("您向MySql修改了一条记录!" + typeof(T).Name + ";model:" + model.ToString());
}
public void Delete(T model)
{
Console.WriteLine("您向MySql删除了一条记录!" + typeof(T).Name + ";model:" + model.ToString());
}
public T Select(Int32 id)
{
Console.WriteLine("您向MySql查询了一条记录!" + typeof(T).Name );
return new T();
}
}
//数据仓储接口
public interface IReposity<T> where T:class
{
void Insert(T model);
void Update(T model);
void Delete(T model);
T Select(Int32 id);
}
//
public class Reposity<T> : IReposity<T> where T : class
{
readonly IDal<T> _dal;
public Reposity(IDal<T> dal)
{
this._dal = dal;
}
public void Insert(T model)
{
_dal.Insert(model);
}
public void Update(T model)
{
_dal.Update(model);
}
public void Delete(T model)
{
_dal.Delete(model);
}
public T Select(Int32 id)
{
return _dal.Select(id);
}
}
public class Order
{
public Int32 Id { get; set; }
public String CName { get; set; }
public String CPhone { get; set; }
public override string ToString()
{
return String.Format("Id:{0},CName:{1},CPhone{2}", Id, CName, CPhone);
}
}
public class OrderBll : IDependency
{
private readonly IReposity<Order> _reposity;
public OrderBll(IReposity<Order> reposity)
{
this._reposity = reposity;
}
public void Insert(Order model)
{
_reposity.Insert(model);
}
public void Update(Order model)
{
_reposity.Update(model);
}
public void Delete(Order model)
{
_reposity.Delete(model);
}
public Order Select(Int32 id)
{
return _reposity.Select(id);
}
}
//所有Bll都继承这个接口
public interface IDependency
{
}
static void Main(string[] args)
{
var builder=new Autofac.ContainerBuilder();
//builder.RegisterGeneric(typeof (MsSqlDal<>)).As( typeof(IDal<>)).InstancePerDependency();
builder.RegisterGeneric(typeof(MySqlDal<>)).As(typeof(IDal<>)).InstancePerDependency();
builder.RegisterGeneric(typeof (Reposity<>)).As(typeof (IReposity<>)).InstancePerDependency();
//注册单类
builder.RegisterType < OrderBll>();
//注册程序集中实现所有IDependency的类
var assem = Assembly.GetExecutingAssembly() ;
builder.RegisterAssemblyTypes(assem).Where(a => a.GetInterfaces().Contains(typeof(IDependency)));
//
var container = builder.Build();
var dal = container.Resolve<OrderBll>();
var order=new Order ()
{
Id=1,
CName = "Aven",
CPhone = "13876544567"
};
dal.Insert(order);
Console.Read();
}