public ControllerX(IServiceA serviceA, IServiceB serviceB, INumberService numberService) { _n = numberService.GetNumber(); Console.WriteLine($"ctor {nameof(ControllerX)}, {_n}"); _serviceA = serviceA; _serviceB = serviceB; }
public ServicesViewModel(BaseServices baseServices, IServiceA serviceA, IServiceB serviceB, IServiceC serviceC, IServiceD serviceD) : base(baseServices) { ServiceAId = serviceA.Id; ServiceBId = serviceB.Id; ServiceCId = serviceC.Id; ServiceDId = serviceD.Id; }
public void GetServiceB() { var app = new AppSetup().Start(new APlusBAppModule()); var container = app.GetAppContainer <IComponentContainer>(); IServiceB serviceA = container.CreateComponent <IServiceB>(); Assert.IsInstanceOfType(serviceA, typeof(ServiceB)); }
public ClassA(IServiceA serviceA, IServiceB serviceB, SomeUnityComponent component) { this.serviceA = serviceA; this.serviceB = serviceB; this.component = component; this.serviceAProperty = ServiceA.GoodLocalDefault; }
public WeatherForecastController(ILogger <WeatherForecastController> logger, IServiceB serviceB, IConsulServiceB consulServiceB) { _logger = logger; //config if necessary _serviceB = serviceB; _consulServiceB = consulServiceB; }
public ServiceD(ServiceA serviceA, IServiceB serviceB, ServiceY serviceY) { if(serviceA == null) throw new ArgumentNullException("serviceA"); if(serviceB == null) throw new ArgumentNullException("serviceB"); this.serviceA = serviceA; this.serviceB = serviceB; this.ServiceY = serviceY; }
private static void UsingScoped() { Console.WriteLine(nameof(UsingScoped)); ServiceProvider RegisterServices() { IServiceCollection services = new ServiceCollection(); services.AddSingleton <INumberService, NumberService>(); services.AddTransient <ControllerX>(); services.AddSingleton <IServiceB, ServiceB>(); services.AddScoped <IServiceA, ServiceA>(); services.AddTransient <IServiceC, ServiceC>(); return(services.BuildServiceProvider()); } using (ServiceProvider container = RegisterServices()) { using (IServiceScope scope1 = container.CreateScope()) { IServiceA a1 = scope1.ServiceProvider.GetRequiredService <IServiceA>(); a1.A(); IServiceA a2 = scope1.ServiceProvider.GetRequiredService <IServiceA>(); a2.A(); IServiceB b1 = scope1.ServiceProvider.GetRequiredService <IServiceB>(); b1.B(); IServiceB b2 = scope1.ServiceProvider.GetRequiredService <IServiceB>(); b2.B(); IServiceC c1 = scope1.ServiceProvider.GetRequiredService <IServiceC>(); c1.C(); IServiceC c2 = scope1.ServiceProvider.GetRequiredService <IServiceC>(); c2.C(); } Console.WriteLine("end of scope 1"); using (IServiceScope scope2 = container.CreateScope()) { IServiceA a1 = scope2.ServiceProvider.GetRequiredService <IServiceA>(); a1.A(); IServiceA a2 = scope2.ServiceProvider.GetRequiredService <IServiceA>(); a2.A(); IServiceB b1 = scope2.ServiceProvider.GetRequiredService <IServiceB>(); b1.B(); IServiceB b2 = scope2.ServiceProvider.GetRequiredService <IServiceB>(); b2.B(); IServiceC c1 = scope2.ServiceProvider.GetRequiredService <IServiceC>(); c1.C(); IServiceC c2 = scope2.ServiceProvider.GetRequiredService <IServiceC>(); c2.C(); } Console.WriteLine("end of scope 2"); } }
static void Main() { IServiceLocator serviceLocator = new ServiceLocator(); IServiceA serviceA = serviceLocator.GetService <IServiceA>(); IServiceB serviceB = serviceLocator.GetService <IServiceB>(); IServiceC serviceC = serviceLocator.GetService <IServiceC>(); // Задержка. Console.ReadKey(); }
public IActionResult Index2() { IServiceA serviceA = DependencyResolver.Current.GetService <IServiceA>(); IServiceB serviceB = DependencyResolver.Current.GetService <IServiceB>(); IServiceC serviceC = DependencyResolver.Current.GetService <IServiceC>(); ViewBag.ServiceA = serviceA.Run(); ViewBag.ServiceB = serviceB.Run(); ViewBag.ServiceC = serviceC.Run(); return(View("Index")); }
static void Main(string[] args) { ServiceLocator loc = new ServiceLocator(); IServiceA Aservice = loc.GetService <IServiceA>(); Aservice.Execute(); IServiceB Bservice = loc.GetService <IServiceB>(); Bservice.Execute(); Console.ReadLine(); }
private static void UsingScoped() { Console.WriteLine(nameof(UsingScoped)); ServiceProvider RegisterServices() { var services = new ServiceCollection(); services.AddSingleton <INumberService, NumberService>(); services.AddScoped <IServiceA, ServiceA>(); services.AddSingleton <IServiceB, ServiceB>(); services.AddTransient <IServiceC, ServiceC>(); return(services.BuildServiceProvider()); } using (ServiceProvider container = RegisterServices()) { //调用ServiceProvider的CreateScope()方法创建一个作用域 //返回实现接口IServiceScope的作用域对象,在其中可以访问属于这个作用域的ServiceProvider using (IServiceScope scope1 = container.CreateScope()) { //因为IServiceA注册为Scoped,所以在同一作用域下返回的实例时一样的 IServiceA a1 = scope1.ServiceProvider.GetService <IServiceA>(); a1.A(); //A, 1 IServiceA a2 = scope1.ServiceProvider.GetService <IServiceA>(); a2.A(); //A, 1 //scope末尾不会释放IServiceB因为IServiceB是注册为单例,需要在作用域的末尾也是存活的,在程序结束时才会释放 IServiceB b1 = scope1.ServiceProvider.GetService <IServiceB>(); b1.B(); IServiceC c1 = scope1.ServiceProvider.GetService <IServiceC>(); c1.C(); IServiceC c2 = scope1.ServiceProvider.GetService <IServiceC>(); c2.C(); } Console.WriteLine("end of scope1"); using (IServiceScope scope2 = container.CreateScope()) { IServiceA a3 = scope2.ServiceProvider.GetService <IServiceA>(); a3.A(); IServiceB b2 = scope2.ServiceProvider.GetService <IServiceB>(); b2.B(); IServiceC c3 = scope2.ServiceProvider.GetService <IServiceC>(); c3.C(); } Console.WriteLine("end of scope2"); } Console.WriteLine(); }
public ServiceCImpl(IServiceA serviceA, IServiceB serviceB) { if (serviceA == null) { throw new ArgumentNullException(nameof(serviceA)); } if (serviceB == null) { throw new ArgumentNullException(nameof(serviceB)); } _serviceA = serviceA; _serviceB = serviceB; }
public ServiceD(ServiceA serviceA, IServiceB serviceB, ServiceY serviceY) { if (serviceA == null) { throw new ArgumentNullException("serviceA"); } if (serviceB == null) { throw new ArgumentNullException("serviceB"); } this.serviceA = serviceA; this.serviceB = serviceB; this.ServiceY = serviceY; }
public Form1() { InitializeComponent(); ChannelFactory <IServiceA> factoryA = new ChannelFactory <IServiceA>(""); m_proxyA = factoryA.CreateChannel(); ChannelFactory <IAdmin> factoryAAdmin = new ChannelFactory <IAdmin>("TCP_IAdmin"); m_proxyAdminA = factoryAAdmin.CreateChannel(); ChannelFactory <IServiceB> factoryB = new ChannelFactory <IServiceB>(""); m_proxyB = factoryB.CreateChannel(); ChannelFactory <IAdmin> factoryBAdmin = new ChannelFactory <IAdmin>("IPC_IAdmin"); m_proxyAdminB = factoryBAdmin.CreateChannel(); }
static void Main(string[] args) { using (ServiceProvider container = RegisterServices()) { for (int i = 0; i < 3; i++) { using (IServiceScope scope1 = container.CreateScope()) { IServiceA a1 = scope1.ServiceProvider.GetService <IServiceA>(); a1.A(); IServiceA a2 = scope1.ServiceProvider.GetService <IServiceA>(); a2.A(); IServiceB b1 = scope1.ServiceProvider.GetService <IServiceB>(); b1.B(); } Console.WriteLine($"End of scope {i}"); } } Console.Read(); }
public ServiceA(ILogger <ServiceA> logger, IServiceB serviceB) { _logger = logger; _serviceB = serviceB; }
public TestComponent(IServiceA serviceA, IServiceB serviceB) { this._serviceA = serviceA; this._serviceB = serviceB; }
public TestComponent(IServiceA serviceA, IServiceB serviceB) { _serviceA = serviceA; _serviceB = serviceB; }
/*----------------------------------------------------------------------------------------*/ public PocoForConstructorAutoWiring(IServiceA serviceA, IServiceB serviceB) { ServiceA = serviceA; ServiceB = serviceB; }
protected AbstractTestComponent(IServiceA serviceA, IServiceB serviceB) { _serviceA = serviceA; _serviceB = serviceB; }
public HostController(IServiceA a, IServiceB b, IScriptHostManager hostManager) { _a = a; _b = b; _hostManager = hostManager; }
/*----------------------------------------------------------------------------------------*/ public void InjectServices(IServiceA serviceA, IServiceB serviceB) { ServiceA = serviceA; ServiceB = serviceB; }
public ClassA(IServiceA a, IServiceB b) { _serviceA = a; _serviceB = b; }
public InjectionDemo(TextReader input, TextWriter output, IServiceA serviceA, IServiceB serviceB) { _serviceA = serviceA; _serviceB = serviceB; _output = output; }
public ServiceA(IServiceB serviceB, IServiceC serviceC) { _serviceB = serviceB; _serviceC = serviceC; }
public ServiceA(IServiceB serviceB) { _serviceB = serviceB; }
public ServicesController(IServiceA serviceA, IServiceB serviceB) { _serviceA = serviceA; _serviceB = serviceB; }
public TestMiddleware(RequestDelegate next, IServiceA serviceA = null, IServiceB serviceB = null) { _next = next; }
public ServiceD2(IServiceB b) { }
public async Task Invoke(HttpContext httpContext, IServiceA serviceA, IServiceB serviceB, ILogger <TestMiddleware> logger) { logger.LogWarning($"Service B ID: {serviceB.Id}"); await _next(httpContext); }
public HomeController(IServiceA serviceA, IServiceB serviceB) { _serviceA = serviceA; _serviceB = serviceB; }
public ServiceA(IServiceB b) { ServiceB = b; }
public TestComponent(IServiceA serviceA, IServiceB serviceB) { ServiceA = serviceA; ServiceB = serviceB; }
public MyConsumer(IServiceA serviceA, IServiceB serviceB) { _serviceA = serviceA; _serviceB = serviceB; }
public ServiceC(IServiceB service) { Console.WriteLine("C被构造了"); }
private void CreacteProxy() { //IServiceA和IServiceB对应的终结点只有1个,无需通过终结点的名称来区分 //**务必注意,这里ChannelFactory<TChannel>泛型类的构造函数必须填写参数;否则之后创建信道的时候,会提示地址不存在 ChannelFactory<IServiceA> factoryA = new ChannelFactory<IServiceA>(string.Empty); ChannelFactory<IServiceB> factoryB = new ChannelFactory<IServiceB>(string.Empty); //IAdmin有2个终结点,所以需要通过终结点的名字来区分 ChannelFactory<IAdmin> adminFactoryA = new ChannelFactory<IAdmin>("TCP_IAdmin"); ChannelFactory<IAdmin> adminFactoryB = new ChannelFactory<IAdmin>("IPC_IAdmin"); proxyA = factoryA.CreateChannel(); adminProxyA = adminFactoryA.CreateChannel(); proxyB = factoryB.CreateChannel(); adminProxyB = adminFactoryB.CreateChannel(); }
public ServiceY(IServiceB serviceB) { }
public CommonServices(IServiceA serviceA, IServiceB serviceB) { this.ServiceA = serviceA; this.ServiceB = serviceB; }
static void Main(string[] args) { var level1 = new Level1 { Name = "Level1", Level2 = new Level2 { Name = "Level2", List = new List <string> { "aaa", "bbb", "ccc" }, Level3 = new Level3 { Name = "Level3" } } }; var level1_samevalue = new Level1 { Name = "Level1", Level2 = new Level2 { Name = "Level2", List = new List <string> { "aaa", "bbb", "ccc" }, Level3 = new Level3 { Name = "Level3" } } }; if (level1.ObjectValuesCompare(level1_samevalue)) { Console.WriteLine("Same Values"); } else { Console.WriteLine("Different Values"); } #region Creational Patterns #region Abstract Factory Console.WriteLine("*********** Abstract Factory Pattern starts ***********"); var phoneClient1 = new PhoneClient(MANUFACTURER.NOKIA); var normalPhone1 = phoneClient1.GetPhone(PHONETYPE.NORMAL) as INormalPhone; var normalPhone2 = phoneClient1.GetPhone(PHONETYPE.SMART) as ISmartPhone; Console.WriteLine(normalPhone1.Name()); Console.WriteLine(normalPhone2.Name()); var phoneClient2 = new PhoneClient(MANUFACTURER.SONYERICSSON); var normalPhone3 = phoneClient2.GetPhone(PHONETYPE.NORMAL) as INormalPhone; var normalPhone4 = phoneClient2.GetPhone(PHONETYPE.SMART) as ISmartPhone; Console.WriteLine(normalPhone3.Name()); Console.WriteLine(normalPhone4.Name()); Console.WriteLine("*********** Abstract Factory Pattern ends ***********"); #endregion #region Factory Method Console.WriteLine("*********** Factory Method Pattern starts ***********"); // Note: constructors call Factory Method var documentList = new List <Document>(); documentList.Add(new Resume()); documentList.Add(new Report()); // Display document pages foreach (Document document in documentList) { Console.WriteLine("\n" + document.GetType().Name + "--"); foreach (Page page in document.Pages) { Console.WriteLine(" " + page.GetType().Name); } } Console.WriteLine("*********** Factory Method Pattern ends ***********"); #endregion #region Singleton Console.WriteLine("*********** Singleton Pattern starts ***********"); Singleton <DbConnection> .Instance.Connect(); Console.WriteLine(string.Format("IsSameInstance = {0}", Singleton <DbConnection> .Instance == Singleton <DbConnection> .Instance)); Console.WriteLine("*********** Singleton Pattern ends ***********"); #endregion #region Builder Console.WriteLine("*********** Builder Pattern starts ***********"); VehicleBuilder builder; // Create shop with vehicle builders var shop = new Shop(); // Construct and display vehicles builder = new ScooterBuilder(); shop.Construct(builder); builder.Vehicle.Show(); builder = new CarBuilder(); shop.Construct(builder); builder.Vehicle.Show(); builder = new MotorCycleBuilder(); shop.Construct(builder); builder.Vehicle.Show(); Console.WriteLine("*********** Builder Pattern ends ***********"); #endregion #endregion #region Behavioral Patterns #region ChainOfResponsibility Console.WriteLine("*********** Chain of Responsibility Pattern starts ***********"); // Setup Chain of Responsibility Approver directorLarry = new CompanyDirector(); Approver vicePresidentSam = new CompanyVicePresident(); Approver presidentTammy = new CompanyPresident(); directorLarry.SetSuccessor(vicePresidentSam); vicePresidentSam.SetSuccessor(presidentTammy); // Generate and process purchase requests Purchase p = new Purchase(2034, 350.00, "Assets"); directorLarry.ProcessRequest(p); p = new Purchase(2035, 32590.10, "Project X"); directorLarry.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); directorLarry.ProcessRequest(p); Console.WriteLine("*********** Chain of Responsibility Pattern ends ***********"); #endregion #region Command Console.WriteLine("*********** Command Pattern starts ***********"); // Create user and let it compute var user = new User(); // User presses calculator buttons user.Compute('+', 100); user.Compute('-', 50); user.Compute('*', 10); user.Compute('/', 2); // Undo 4 commands user.Undo(4); // Redo 3 commands user.Redo(3); Console.WriteLine("*********** Command Pattern ends ***********"); #endregion #region Interpreter Console.WriteLine("*********** Interpreter Pattern starts ***********"); string roman = "MCMXXVIII"; var context = new Context(roman); // Build the 'parse tree' var tree = new List <Expression>(); tree.Add(new ThousandExpression()); tree.Add(new HundredExpression()); tree.Add(new TenExpression()); tree.Add(new OneExpression()); foreach (Expression exp in tree) { exp.Interpret(context); } Console.WriteLine("{0} = {1}", roman, context.Output); Console.WriteLine("*********** Interpreter Pattern ends ***********"); #endregion #region Mediator Console.WriteLine("*********** Mediator Pattern starts ***********"); // Create chatroom IChatroom chatroom = new Chatroom(); // Create participants and register them Participant George = new GraduateStudentParticipant("George"); Participant Paul = new GraduateStudentParticipant("Paul"); Participant Ringo = new GraduateStudentParticipant("Ringo"); Participant John = new GraduateStudentParticipant("John"); Participant Yoko = new UnderGraduateStudentParticipant("Yoko"); chatroom.Register(George); chatroom.Register(Paul); chatroom.Register(Ringo); chatroom.Register(John); chatroom.Register(Yoko); // Chatting participants Yoko.Send("John", "Hi John!"); Paul.Send("Ringo", "You need to study"); Ringo.Send("George", "Hello"); Paul.Send("John", "How are you"); John.Send("Yoko", "I can explain you Maths"); Console.WriteLine("*********** Mediator Pattern ends ***********"); #endregion #region Observer Console.WriteLine("*********** Observer Pattern starts ***********"); // Create IBM stock and attach investors Stock ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; Console.WriteLine("*********** Observer Pattern ends ***********"); #endregion #region Visitor Console.WriteLine("*********** Visitor Pattern starts ***********"); // Setup employee collection Employees e = new Employees(); e.Attach(new Clerk()); e.Attach(new Director()); e.Attach(new President()); // Employees are 'visited' e.Accept(new IncomeVisitor()); e.Accept(new VacationVisitor()); Console.WriteLine("*********** Visitor Pattern ends ***********"); #endregion #endregion #region Structural Patterns #region Bridge Console.WriteLine("*********** Bridge Pattern starts ***********"); var documents = new List <Manuscript>(); var formatter = new FancyFormatter(); var faq = new FAQ(formatter); faq.Title = "The Bridge Pattern FAQ"; faq.Questions.Add("What is it?", "A design pattern"); faq.Questions.Add("When do we use it?", "When you need to separate an abstraction from an implementation."); documents.Add(faq); var book = new Book(formatter) { Title = "Lots of Patterns", Author = "John Sonmez", Text = "Blah blah blah..." }; documents.Add(book); var paper = new TermPaper(formatter) { Class = "Design Patterns", Student = "Joe N00b", Text = "Blah blah blah...", References = "GOF" }; documents.Add(paper); foreach (var doc in documents) { doc.Print(); } Console.WriteLine("*********** Bridge Pattern ends ***********"); #endregion #region Decorator Console.WriteLine("*********** Decorator Pattern starts ***********"); // create a Simple Cake Base first var cBase = new CakeBase(); PrintProductDetails(cBase); // add cream to the cake var creamCake = new CreamDecorator(cBase); PrintProductDetails(creamCake); // now add a Cherry on it var cherryCake = new CherryDecorator(creamCake); PrintProductDetails(cherryCake); // now add Scent to it var scentedCake = new ArtificialScentDecorator(cherryCake); PrintProductDetails(scentedCake); // Finally add a Name card on the cake var nameCardOnCake = new NameCardDecorator(scentedCake); PrintProductDetails(nameCardOnCake); // now create a simple Pastry var pastry = new PastryBase(); PrintProductDetails(pastry); // add cream and cherry only on the pastry var creamPastry = new CreamDecorator(pastry); var cherryPastry = new CherryDecorator(creamPastry); PrintProductDetails(cherryPastry); Console.WriteLine("*********** Decorator Pattern ends ***********"); #endregion #endregion #region Service Locator Pattern //http://www.c-sharpcorner.com/UploadFile/dacca2/service-locator-design-pattern/ Console.WriteLine("*********** Service Locator Pattern starts ***********"); ServiceLocator serviceLocator = new ServiceLocator(); IServiceA serviceA = serviceLocator.GetService <IServiceA>(); serviceA.Execute(); IServiceB serviceB = serviceLocator.GetService <IServiceB>(); serviceB.Execute(); Console.WriteLine("*********** Service Locator Pattern ends ***********"); #endregion Console.ReadKey(); }