• 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();
    }

  • 相关阅读:
    JAVA编程规则【转自java编程思想】
    诊断 Java 代码: 轻松掌握 Java 泛型
    Linux开启telnet远程登录服务全攻略
    TCP详解
    UNIX环境高级编程文件描述符浅析
    DHCP与BOOTP有什么区别
    Linux 多播(组播)例程
    你所不知道的传输层
    虚电路方式,数据报方式
    java foreach 使用
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5473918.html
Copyright © 2020-2023  润新知