1

Topic: Implementation of dependences in application typical a web.

Hello. Typical application easily-expanded a web (the Internet //// service on handling  the data): Application is divided on two parts: 1.FE html+js+css 2.BE asp.net webservices Hostitsja on one website-ip the address, but web services - under-appendix. FE shows html a page and necessarily pulls methods of web services. FE - it can be written on anguljare/kastomnogo js / __ to substitute the. BE. The basic controler, contains the information on session, on the user and the information which is necessary in almost in all requests about the server. From basic  almost all controlers will be inherited.  ` exception ` is "filter" which intercepts all exceptions,  and if it is necessary returns to the client  an error in json or HTTP CODE. [exception] public class baseController: ApiController {public session session {get; internal set;} public domain.servicemodels.user.user user {get; internal set;} } An example of the typical controler. "Filter" ` auth ` says that only  users can cause the given controler." The filter "` authorz ` - tells what roles should be at this user that it could cause the controler.  service - speaks that only HTTP POST the request can be fulfilled to this method; since PUT, DELETE - it defects/crutches HTTP of the protocol, in real life they are unnecessary, all requests to services will go only through POST,  we create one more level in network  OSI. HTTP GET lack HTTP of the protocol is necessary only for browsers for downloading html or js, too, it would be possible to live easy with one POST, sending still as parameter lifetime of the data which were given by the server. In a controler body, there is a basic handling of the code the necessary configs, necessarily, get, entering parameters in the necessary objects are converted. [auth] public class carController: baseController {[authorz (roles.user, roles.admin)] [service] public rr delete (car car) {var s = new carservice (); return s.delete (car, this.user);} } Each method of the controler, will be  the code of an error/kastomnoj of an error with the description. public enum responce_codes {//Action was handled well, without errors success = 0,//HTTP parameters (from body|url) not valid bad_request = 400, unauthorized = 401, forbidden=403, internal_server_error = 500...} public class error {public string name {get; set;} public string message {get; set;}} public class rr: error {public responce_codes code {get; set;} public IEnumerable <error> errors {get; set;} } Typical service about business by logic. In a method ` delete ` the data which will be checked up are transferred: basic , then requests to , any logic, conversion of a proxy/adapter by a class in object - Responce for the client in whom will not contain the superfluous data. In the unit ` using (var context = new xcontext ()) ` - creates connection with , get the necessary data, is processed and closed connection. ` invalid_data_exception ` -  an exception which will be processed in ` exception ` the filter public class carservice {public rr delete (servicemodels.car.car car, servicemodels.user.user user) {var date = DateTime. UtcNow; if (user.shop == null || car == null) {throw new invalid_data_exception ();} using (var context = new xcontext ()) {var carsmanager = new carsmanager (context); var usersmanager = new usersmanager (context); var carproxy = new carproxy (); var update_car = carsmanager.list_by_shop_not_deleted (user.shop.id).FirstOrDefault (x => x.id == car.id); if (update_car == null) {throw new invalid_data_exception ();}... update_car.deletedate = date; carsmanager.update (update_car); context. SaveChanges (); return new rr ();}}} All any superfluous code, all is beautiful, all works, we always know that in each method of service that data/configs which are necessary to it for logic performance will be transferred only, then there will be a check of the entering data, and after if it is necessary to us, instead of at everyone request|responce, will be created  with a bd/disk, and you only rivet controlers yes methods. Where it would be possible to apply Dependency Injection what to simplify to itself life?

2

Re: Implementation of dependences in application typical a web.

Hello, #John, you wrote: J> Where it would be possible to apply Dependency Injection what to simplify to itself life? Yes here you already also apply it: public rr delete (servicemodels.car.car car, servicemodels.user.user user)

3

Re: Implementation of dependences in application typical a web.

Hello, IQuerist, you wrote: IQ> Yes here you already also apply it: IQ> IQ> public rr delete (servicemodels.car.car car, servicemodels.user.user user) IQ> And how it is possible to integrate any IoC e.g. Autofac (or any another), what it would work as DI, instead of how SL?

4

Re: Implementation of dependences in application typical a web.

Hello, #John, you wrote: J> Hello, IQuerist, you wrote: IQ>> Yes here you already also apply it: IQ>> IQ>> public rr delete (servicemodels.car.car car, servicemodels.user.user user) IQ>> J> And how it is possible to integrate any IoC e.g. Autofac (or any another), what it would work as DI, instead of how SL? At such setting of a question the output arises - IOC the container most likely simply is not necessary to you. The Author see http://rsdn.org/forum/design/6512200.1: IQuerist Date: 27.07.16

5

Re: Implementation of dependences in application typical a web.

Hello, IQuerist, you wrote: J>> And how it is possible to integrate any IoC e.g. Autofac (or any another), what it would work as DI, instead of how SL? IQ> the Author see http://rsdn.org/forum/design/6512200.1: IQuerist Date: 27.07.16 It is possible to tell that DI is the most typical method of refactoring of the code, removal of common parts - logicians on data handling in separate units. At first we have a long function in which any data is initialized, then causes some more functions which inside initialize optional data. 1. - void func () {var controller=new controler (); int z=controller.calc (5,10); int w=controller.edit (z);} class controler {int calc (int x, int y) {int s=x> 0? 1:2; return x+y+s;} int edit (int z) {int s=z> 0? 3:4; return z+s;}} Podrefaktorim, we carry out the code the reused code in a class ` super `. 2. - void func () {var controller=new controler (); int z=m.calc (5,10); int w=m.edit (z);} class super {int check1 (int s) {return s> 0? 1:2;} int check2 (int s) {return s> 0? 3:4;}} class controler {int calc (int x, int y) {var super=new super (); int s=super.check1 (x); return x+y+s;} int edit (int z) {var super=new super (); int s=super.check2 (z); return z+s;}} we See that the class ` super ` is used in several places, we carry out it  in the designer. (That it would be possible to make the step 4 see.) 3. - void func () {var super=new super (); var controller=new controler (super); int z=controller.calc (5,10); int w=controller.edit (z);} class super {int check1 (int s) {return s> 0? 1:2;} int check2 (int s) {return s> 0? 3:4;}} class controler {super super; controler (super super) {this.super=super;} int calc (int x, int y) {int s=super.check1 (x); return x+y+s;} int edit (int z) {int s=super.check2 (z); return z+s;} } And suddenly at us can one more any user parameter of adjustments in a class ` super ` will be added or on the contrary it is necessary any parameters ` h, g, w, u `  somewhere above in the code (e.g. what still somewhere  or that the user could set them etc.), we add a class ` config ` 4. - void func () {var config=new config {h=1, g=2, w=3, u=4}; var super=new super (config); var controller=new controler (super); int z=controller.calc (5,10); int w=controller.edit (z);} class config {int h; int g; int w; int u;} class super {config config; super (config config) {this.config=config;} int check1 (int s) {return s> 0? config.h:config.g;} int check2 (int s) {return s> 0? config.w:config.u;}} class controler {super super; controler (super super) {this.super=super;} int calc (int x, int y) {int s=super.check1 (x); return x+y+s;} int edit (int z) {int s=super.check2 (z); return z+s;}} In certain cases enough first variant, in some it is necessary to do at once as in 4 an example. - we present that the class ` controler ` is the most normal controler from asp.net web api. But, we do not have possibility to create controlers (we can, but in the most simple case for us it makes asp.net web api ) void func () {//. It for us does . Somewhere there it is far in the code//var controller=new controler (super);//int z=controller.calc (5,10);//int w=controller.edit (z);} class configs {super get_default_super () {var config=new config {h=1, g=2, w=3, u=4}; return new super (config);}} class controler {super super; controler () {this.super=configs.get_default_super ();} int calc (int x, int y) {int s=super.check1 (x); return x+y+s;} int edit (int z) {int s=super.check2 (z); return z+s;} } And we have a choice. 1. To cause in each controler - its designer/in  initialization of the necessary configs. 2. To use AutofacX th: in Global.asax to register, for such controlers, by a call of its designer, to initialize object ` super ` such data void func () {//this function is defined in Global.asax and caused at the first application launch. var super=configs.get_default_super (); AutofacX.Register (typeof (controler), typeof (service), super);} class configs {super get_default_super () {var config=new config {h=1, g=2, w=3, u=4}; return new super (config);}} class controler {super super; controler (super) {this.super=configs.get_default_super ();} int calc (int x, int y) {int s=super.check1 (x); return x+y+s;} int edit (int z) {int s=super.check2 (z); return z+s;} } The second variant is better, since it reduces an amount of places of initialization of this most basic data.! - it is possible to draw an output: DI the container should be used, when at us any objects form by a reflection and we do not have explicit access to the code of their creation what to transfer in the designer the data necessary to us (as in an example with controlers). But if application not strongly big, at basic stages of its development, quite to fit the code without DI containers-issolzovanija Singltonov, e.g. for , e.g. let we use Nlog  (we can  stretch titles in different attributy/controllers by means of DI containera) since  at us not much, we simply create a static class/function which to us returns a name list , we use standard function Nlog which on a function title  to us  and we use it. In the future if there will be time and not laziness, such code it will be not difficult  and to rewrite by means of DI the container.

6

Re: Implementation of dependences in application typical a web.

Hello, #John, you wrote: J> It is possible to draw an output: J> DI the container should be used, when at us any objects form by a reflection and we do not have explicit access to the code of their creation what to transfer in the designer the data necessary to us (as in an example with controlers). You need to transfer what data in the controler? The controler it not the general-purpose processor business of logic it only an interlayer between web an infrastructure and your application. All that is accessible to you in the controler these are the data web (http request, cookies, variable surroundings of the server) all that the controler should make -  web the data on entity classes business of logic and to cause . The output agent logic business.   should be explicit since differently this logic  starts to be spread on business to the logician and suddenly transaction business start to depend directly for example on details of a data format of client forms which gather on the client through javascript.

7

Re: Implementation of dependences in application typical a web.

Hello, IQuerist, you wrote: IQ> Hello, #John, you wrote: IQ> you need to transfer What data in the controler? At the first application launch, in Global.asax, different configs, e.g. from Web.config the list of ways to different utilities on a disk c:\bin, to the data (, to docks), the virtual ways, email addresses, ftp_creds are read out. Register in service/modelku (e.g. which is able to work with  ways). class Global.asax {public func (); var configs=configs.get_configs_from_web_config_that_work_with_path ();//service which is able  pdf var pdfservice=new pdfservice (configs); AutofacX.Register ("*Controller", typeof (pdfservice), pdfservice);}} class carController {public pdfservice; public carController (pdfservice pdfservice) {this.pdfservice=pdfservice;} public rr pdfgenerate (pdfmodel pdfmodel) {return this.pfdservice.generate (pdfmodel);} } Here also it turns out that thus, we drag different configs in the controler.>> all that is accessible to you in the controler these are the data web (http request, cookies, variable surroundings of the server) all that the controler should make -  web the data on entity classes business of logic and to cause . The output agent logic business. Yes, so.>> transactions start to depend directly for example on details of a data format of client forms which gather on the client through javascript. Was few times such that the client (3 by the side) separately has been written, and separately services - were added by one more layer from a proxy classes which converted entering parameters and the answer of services, in model which the client expected. But, the additional layer imported small complexities when changed business of the logician (services and the client) when new fields in models were added.

8

Re: Implementation of dependences in application typical a web.

Hello, #John, you wrote: J> Hello, IQuerist, you wrote: IQ>> Hello, #John, you wrote: IQ>> you need to transfer What data in the controler? J> at the first application launch, in Global.asax, different configs, e.g. from Web.config the list of ways to different utilities on a disk c:\bin, to the data (, to docks), the virtual ways, email addresses, ftp_creds are read out. Register in service/modelku (e.g. which is able to work with  ways). J> J> class Global.asax {J> public func (); J> var configs=configs.get_configs_from_web_config_that_work_with_path (); J>//service which is able  pdf J> var pdfservice=new pdfservice (configs); J> AutofacX.Register ("*Controller", typeof (pdfservice), pdfservice); J>} J>} J> class carController {J> public pdfservice; J> public carController (pdfservice pdfservice) {J> this.pdfservice=pdfservice; J>} J> public rr pdfgenerate (pdfmodel pdfmodel) {J> return this.pfdservice.generate (pdfmodel); J>} J>} J> J> Here also it turns out that thus, we drag different configs in the controler.  such decisions! The code simply shouts - yes burn this yours  the code befouled DI container with a dark blue flame, we make all logic necessary business on the client is better, on javascript it is good that you there are not put)))>>> transactions start to depend directly for example on details of a data format of client forms which gather on the client through javascript. J> Was few times such that the client (3 by the side) separately has been written, and separately services - were added by one more layer from a proxy classes which converted entering parameters and the answer of services, in model which the client expected. But, the additional layer imported small complexities when changed business of the logician (services and the client) when new fields in models were added. Small complexities? You had not to catch errors connected to changes in .

9

Re: Implementation of dependences in application typical a web.

10

Re: Implementation of dependences in application typical a web.

11

Re: Implementation of dependences in application typical a web.

12

Re: Implementation of dependences in application typical a web.

13

Re: Implementation of dependences in application typical a web.

14

Re: Implementation of dependences in application typical a web.

15

Re: Implementation of dependences in application typical a web.

16

Re: Implementation of dependences in application typical a web.

17

Re: Implementation of dependences in application typical a web.