zoukankan      html  css  js  c++  java
  • autofac 切换接口小例子

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using Autofac;
    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("现在演示切换数据库的情况");
                //使用sql server
                var t1 = SwitchRespority("sql");
                IPerson tt = t1.Resolve<IPerson>();
                tt.Show();
                var t2 = SwitchRespority("oracle");
                IPerson tt1 = t2.Resolve<IPerson>();
                tt1.Show();
                Console.Read();
            }
            /// <summary>
            /// 切换数据库
            /// </summary>
            private static IContainer SwitchRespority(string name)
            {
                var builder = new ContainerBuilder();
                Type t = null;
                switch (name)
                {
                    case "sql": t = typeof(ISql); break;
                    case "oracle": t = typeof(IOracle); break;
                }
                var types = AppDomain.CurrentDomain.GetAssemblies()
                         .SelectMany(a => a.GetTypes().Where(t1 => t1.GetInterfaces().Contains(t)))
                         .Where(d => !d.IsGenericType)
                         .ToArray();
                builder.RegisterTypes(types).AsImplementedInterfaces();
                return builder.Build();
            }
        }
    
        public abstract class BaseEntiry
        {
            public abstract int Id { get; set; }
        }
        public interface ISql
        {
    
        }
        public interface IOracle
        {
    
        }
        /// <summary>
        /// 通用存储接口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public interface IRepository<T> where T : BaseEntiry
        {
            T GetById(int id);
            IEnumerable<T> List();
            IEnumerable<T> List(Expression<Func<T, bool>> predicate);
            void Add(T entity);
            void Delete(T entity);
            void Edit(T entity);
        }
        /// <summary>
        /// 通用存储库实现
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class Repository<T> : ISql, IRepository<T> where T : BaseEntiry
        {
            public void Add(T entity)
            {
                throw new NotImplementedException();
            }
    
            public void Delete(T entity)
            {
    
            }
    
            public void Edit(T entity)
            {
                throw new NotImplementedException();
            }
    
            public T GetById(int id)
            {
                throw new NotImplementedException();
            }
    
            public IEnumerable<T> List()
            {
                throw new NotImplementedException();
            }
    
            public IEnumerable<T> List(Expression<Func<T, bool>> predicate)
            {
                throw new NotImplementedException();
            }
        }
    
        public class OracleRepository<T> : IOracle, IRepository<T> where T : BaseEntiry
        {
            public void Add(T entity)
            {
                throw new NotImplementedException();
            }
    
            public void Delete(T entity)
            {
    
            }
    
            public void Edit(T entity)
            {
                throw new NotImplementedException();
            }
    
            public T GetById(int id)
            {
                throw new NotImplementedException();
            }
    
            public IEnumerable<T> List()
            {
                throw new NotImplementedException();
            }
    
            public IEnumerable<T> List(Expression<Func<T, bool>> predicate)
            {
                throw new NotImplementedException();
            }
        }
        public class Person : BaseEntiry
        {
            public override int Id { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        }
        public interface IPerson : IRepository<Person>
        {
            void Show();
        }
        public class PersonRepository : Repository<Person>, IPerson
        {
            public void Show()
            {
                Console.WriteLine("sql server 123123");
            }
        }
        public class OraclePersonRepository : OracleRepository<Person>, IPerson
        {
            public void Show()
            {
                Console.WriteLine("oracle 123123");
            }
        }
    }

  • 相关阅读:
    Java时间和时间戳的相互转换
    linux 通过pid 寻找程序路径的最简单命令(pwdx)
    Oracle--存储过程学习进阶
    经典sql总结(2)
    经典sql总结(1)
    类的初始化
    StringBuffer和String 的例子
    i=i++
    一个异常学习的好例子
    有空研究这篇http://blog.csdn.net/studyvcmfc/article/details/7720258 研究后写篇记录
  • 原文地址:https://www.cnblogs.com/student-note/p/9027963.html
Copyright © 2011-2022 走看看