jeudi 19 septembre 2019

Javascript code design: how to try catch each line in a good way?

My goal is to catch the error of each line and even there is an error, still run the remaining code, like this:

try {
  doFirstThing()
} catch(err) {
  //not important
}
try {
  doSecondThing()
} catch(err) {
  //not important
}
try {
  doSecondThing()
} catch(err) {
  //not important
}
//...
try {
  doLastThing()
} catch(err) {
  //not important
}

My question is: Do I have to try-catch each line to do this? Or there is a clever equivalent way that can have this done in a more elegant manner?

How to remove duplication from CallableStatement boiler plate code?

We have many stored procedures and functions that we call on our DB and the setup for each call in our data access layer is really verbose with setting the inputs and registering the output etc... Is there a better solution to maybe generating the CallableStatement dynamically for any stored procedure or function with any types/amounts of parameters and output type?

We have a home brew solution and it is ugly... full of if/else, fors and whiles... very hard to read and maintain. We have also tried to centralize common boilerplate code for like function calls. I.E. All of the ones that take a Long and return a boolean, all use the same centralized method with dynamic Long and stored procedure string.

The code is from memory please don't pay too much attention to syntax, this is a design question more than anything.

//Client usage in Controller class

certAwarded = PackageName.isCertAwardedFor(personIDToCheck);

//In class that mimics the interface of the database packages 
//There would be a method per public function
public static boolean isCertAwardedFor(Long personID){
    return PackageUtils.isMet(personID, "{? = call PACKAGE.is_met(?)}");
}

//In Package scoped Utility class 
//Attempt to centralize all single input param and return of boolean
//type of procedure calls.
static boolean isMet(Long personID, String proc){
    boolean met = false;
    try(AutoCloseableStatement stmt = new AutoCloseableStatement(proc)){
        CallableStatement callableStmt = stmt.createStatement();
        callableStmt.registerOutParameter(1, OracleTypes.VARCHAR2);
        callableStmt.setLong(2, personID);
        callableStmt.execute();
        met = convertYNtoBool(callableStmt.getString(1));
    }catch(SQLException ex){
        Logger.log(ex);
    }
return met;
}


///////////////////////////////////OR///////////////////////////////

//Client usage in Controller class

certAwarded = PackageName.isCertAwardedFor(personIDToCheck, CertPackageEnum);

//In class that mimics the interface of the database packages 
//There would be a method per public function
public static boolean isCertAwardedFor(Long personID, PackageProc procCall){
    return PackageUtils.call(personID, procCall.IS_CERT_AWARDED);
}

//In Package scoped Utility class 
//Attempt to centralize all single input param and return of boolean
//type of procedure calls.
static boolean isMet(Long personID, String proc){
    try(AutoCloseableStatement stmt = new AutoCloseableStatement(proc)){
        CallableStatement callableStmt = stmt.createStatement();
        LOTS OF CONDITIONS TO CHECK AND SET ALL POSSIBLE INPUTS AND OUTPUTS
    }catch(SQLException ex){
        Logger.log(ex);
    }
    return ?????
}

Looking for Design Pattern to Automate Repeated Task such as Login/Logout

I'm looking to a design pattern to help simplify my code.

My code is using HttpClient to call a web API that gets or posts information, but each session requires a login call first where a cookie is returned as ID, and a logout call is made at the end to close the connection. So my web API class looks like this:

public class APIHelper 
{
    public HttpClient myClient { get; set; }

    public async void Login()
    {
        using (HttpResponseMessage response = await myClient.PostAsync("loginAddress", "loginInput"))
        {
            if (response.IsSuccessStatusCode)
            {                    
                //save cookie
            }
        }
    }

    public async void Logout()
    {
        using (HttpResponseMessage response = await myClient.PostAsync("logoutAddress", ""))
        {
            if (response.IsSuccessStatusCode)
            {                    
                //session ends
            }
        }
    }

    public void GetOrder()  {...}

    public void NewOrder(object OrderData)  {...}

    public void GetCustomer()   {...}

    public void NewCustomer(object CustomerData)    {...}
}

And to use them, I would simply call them in order:

public Main()
{
    APIHelper.Login();
    APIHelper.GetOrder();   //or NewOrder, or GetCustomer, or any other actual API calls
    APIHelper.Logout();
}

Is there anyway I can place the Login/Logout calls inside each of the actual API calls so I don't have to type them up for each call? Ideally I just have to set up the structure once, then for whatever API calls I create, the system will automatically call the Login/Logout at beginning/end. Which design pattern addresses this kind of issue? Any simple example would be very helpful!

Thank you. SC

Another way to create data access layer than repository pattern

Everywhere people create DAL as Repositories. Is there any other/better solution to create data access layer with Dapper?

jquery replace part of a class name with pattern

I select all classes by class that begins with photo-. Replace part of class name by pattern. I need to replace photo-gallery-RID459852 with photo-gallery. Note: the part -RID[0-9] is replaced by ""

$("#Master [class*='photo-']").replace(function(index, css) {
  return (css.match(/(^|\s)-RID\S+/g) || []).join(' ');
}, "");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<main id="Master">
  <div class="photo-gallery-RID459852 other any some">
    Algo
  </div>
  <div class="photo-gallery-RID987410 other any some2 other2"></div>
  <div>
    <div>
      <div class="photo-gallery-369841 other any some"></div>
    </div>
  </div>
  <article>
    <div class="photo-gallery-RID36541 here now other any some"></div>
  </article>
</main>

My jsFiddle: https://jsfiddle.net/ngqku78p/

Find the right design pattern

I have a controller class that make its objects visible only behind the login. Now, I want some of these object to be visible outside, The record are already in the database so I do not need to create them, also I do not need to create a new visibleObjectController class because the functions will be exactly the same. The objects just need to behave differently according to a visible or hidden feature.

I am quite sure there is a design pattern I can use for this. just do know which one. Any advise?

Design-pattern to limit access one class to another

I have class Shop and class Customer. So I want to specify the way class Customer can interract with class Shop (could not have access to some methods and fields). As far as I understand I need the third class wich will be inherited from Shop class and override access to this fields and methods. At the same time I want to create new Customer instance via Shop class and store all customers there. This means cyclic imports and this is not good. What kind of pattern you could advice me in this situation?