I've been working a lot with .NET framework lately and wanted to share some useful knowledge with others human beings (but if you are a robot, you can also use this post for your tasks =) ).
If we want to call a static method from a cshtml file, the best way to do it is by using AJAX. In the project I am working on, I needed to perform some back-end task at some specific moment when user requests it. The back-end function is supposed to return a URL (string) and then use that new URL to update some of my links in the front-end.
We can directly call a static method from cshtml by just calling it, like this:
@MyClass.methodName("abc")
However, with ASP.net, RAZOR code is executed when the page is loaded, so the only way to "control" when to perform this action is by using AJAX.
This is my code using AJAX:
<script>
function vaxGUID() {
$.ajax({
type: 'POST',
url: "/ControllerName/method",
data: '{"Name":"anything"}',
contentType: 'application/json; charset=utf-8',
dataType: 'html',
success: function (data) {
$('a.varURL').attr('href', data);
alert("Good response - " + data);
},
error: function (data, success, error) {
alert("Error : " + error);
}
});
return false;
}
</script>
As we can see in the method, we are making the call with: url: "/ControllerName/method", this means that is we put that URL in our browser (with any parameter if needed) , we should get a response. That is a good way to test it first. "Data" is returned and then we can use it in the way we need it.
Thursday, August 25, 2016
Sunday, August 21, 2016
Java Singleton Pattern
Posted by Carlos Rolando Febrero
Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, drivers objects, caching and thread pool.
For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multi threading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.
Implementing Singletons
The easier way to create a thread-safe singleton class is to make the global access method synchronized, so that only one thread can execute this method at a time.
- Private constructor to restrict instantiation of the class from other classes.
- Private static variable of the same class that is the only instance of the class.
- Public static method that returns the instance of the class, this is the global access point for outer world to get the instance of the singleton class.
Example:
package
com.bigbangcode.constructors;
public
class
MySingleTon {
private
static
MySingleTon myObj;
/**
* Create private constructor
*/
private
MySingleTon(){
}
/**
* Create a static method to get instance.
*/
public
static
MySingleTon getInstance(){
if
(myObj ==
null
){
myObj =
new
MySingleTon();
}
return
myObj;
}
public
void
getSomeThing(){
// do something here
System.out.println(
"I am here...."
);
}
public
static
void
main(String a[]){
MySingleTon st = MySingleTon.getInstance();
st.getSomeThing();
}
}
Another example, Thread Safe Singleton:
package com.bigbangcode.singleton; public class ThreadSafeSingleton { private static ThreadSafeSingleton instance; private ThreadSafeSingleton(){} public static synchronized ThreadSafeSingleton getInstance(){ if(instance == null){ instance = new ThreadSafeSingleton(); } return instance; } }
Subscribe to:
Posts (Atom)