Recommendations for duplicating C # code

I am trying to structure my code in such a way as to reduce / avoid code duplication, and I came across an interesting problem. Each time my code calls a stored process, I need to pass in several variables that are common to the stored procedure: for example, username, domain, server_ip and client_ip. They all come from either the HttpRequest object or the system.environment object.

Since they are passed to each stored process, my initial thought was to create a utility class, which is a wrapper for the database, and would initialize and pass them each time, so I do not need to do this in my code. The problem is that the C # class (inside the App_Code folder) does not see the Httprequest object. Of course, I could pass this as an argument to the shell, but that would defeat the whole purpose of creating a wrapper. Did I miss something?

I understand that it is not so difficult to repeat 4 lines of code every time I call a stored procedure, but I would rather eliminate code duplication at the earliest stages.

+3
source share
7 answers

, 4 . 4 .

- - - 4 .

BusObj (Request [ "username" ],...).method()

, SQLParameter 4 , .

+4

, , , // .

, , , (4 - , ), , . 99.9999999999999999999999% , ( ).

+3

HttpContext.Current , HttpRequest, , , App_Code.

+2

: "" , , .

class P {
    readonly string name;
    readonly string domain;
    public P(string name, string domain) {
        this.name = name; this.domain = domain;
    }
    public void inject(Action<string, string> f) {
        f(p.arg1, p.arg2);
    }
    public T inject<T>(Func<string, string, T> f) {
        return f(p.arg1, p.arg2);
    }
}

VB.net , AddressOf. , .

+2

, . , / unit test.

HttpContext, , , , . , HttpContext, . , HttpContext? ?

+1

System.Web.HttpContext.Current.Request, .

+1

, . DRY , - , . , 4 , 4 . , httprequest , . , -.

, , . SecurityPrincipal ( IPrincipal), . , , . , , .

, , .

+1

Source: https://habr.com/ru/post/1703857/


All Articles