public void Delete(Persion p) { _repository.Delete(p); }
public void Insert(Persion p) { _repository.Insert(p); }
public void Update(Persion p) { _repository.Update(p); }
static void Main(string[] args) { #region Demo1 Console.WriteLine("==========> Demo1 <=============="); var builder = new ContainerBuilder(); builder.RegisterType <DataSourceManager>(); builder.RegisterType <Sqlserver>().As <IDataSource>(); using (var container = builder.Build()) { var manager = container.Resolve <DataSourceManager>(); Console.WriteLine(manager.GetData()); } var builder2 = new ContainerBuilder(); //builder2.RegisterType<DataSourceManager>(); builder2.RegisterType <Sqlserver>().As <IDataSource>(); using (var container = builder2.Build()) { var manager = container.Resolve <IDataSource>(); Console.WriteLine(manager.GetData()); } var builder3 = new ContainerBuilder(); builder3.RegisterType <Sqlserver>().Named <IDataSource>("SqlServer"); builder3.RegisterType <Oracle>().Named <IDataSource>("Oracel"); using (var container = builder3.Build()) { var manager = container.ResolveNamed <IDataSource>("Oracel"); Console.WriteLine(manager.GetData()); } var builder4 = new ContainerBuilder(); builder4.RegisterType <DataSourceManager>(); builder4.RegisterType <Sqlserver>().As <IDataSource>(); //builder4.RegisterType<Sqlserver>().Named<IDataSource>("SqlServer"); // builder4.RegisterType<Oracle>().Named<IDataSource>("Oracel"); using (var container = builder4.Build()) { var manager = container.Resolve <DataSourceManager>(new NamedParameter("name", "AAAAAA"), new NamedParameter("ds", new Oracle())); Console.WriteLine(manager.GetData()); } #endregion #region Demo2 Console.WriteLine("==========> Demo2 <=============="); var builder5 = new ContainerBuilder(); builder5.RegisterGeneric(typeof(Dal <>)).As(typeof(Idal <>)).InstancePerDependency(); builder5.RegisterGeneric(typeof(Repository <>)).As(typeof(IRepository <>)).InstancePerDependency(); builder5.Register(c => new PersionBll((IRepository <Persion>)c.Resolve(typeof(IRepository <Persion>)))); builder5.Register(c => new CustomBll((IRepository <Custom>)c.Resolve(typeof(IRepository <Custom>)))); //var container = builder.Build()教程里都是使用这行代码, //我本地测试需要加入ContainerBuildOptions枚举选项。 using (var container = builder5.Build(ContainerBuildOptions.None)) { // var repository= container.Resolve(typeof(IRepository<Persion>),new TypedParameter()); // IRepository<Persion> _repository = repository as Repository<Persion>; // var m = new PersionBll(_repository); Persion p = new Persion(); p.Name = "小人"; p.Age = 27; var m = container.Resolve <PersionBll>(); m.Insert(p); Custom c = new Custom(); c.CustomName = "小小"; c.CustomID = 10; var cc = container.Resolve <CustomBll>(); cc.Update(c); } #endregion Console.ReadLine(); }