Laravel dynamic routing

Route::get('/', function () {
    return view('welcome');
});

Route::get('{controller}/{action}', function($controller, $action = 'index') {
    $ctr = app()->make('\App\Http\Controllers\\'.ucfirst($controller).'Controller');
    return $ctr->callAction($action, array());
});

Route::get('{controller}/{action}/{id}', function($controller, $action = 'index', $id = null) {
    $ctr = app()->make('\App\Http\Controllers\\'.ucfirst($controller).'Controller');
    return $ctr->callAction($action, array('id'=> $id));
});

 

More:

https://laracasts.com/discuss/channels/general-discussion/l5-calling-dynamic-controller-names

JavaScript tricks for freshers

1. Always use === and !== instead of == and !=

alert('' == '0'); //false
alert(0 == ''); // true
alert(0 =='0'); // true

 

2.  Assigning default values to variables

You can use the logical or operator || in an assignment expression to provide a default value:

var i = x || y;

The i variable will get the value of y only if x is false (if is null, false, undefined, 0, empty string, or NaN), otherwise i will get the value of x.

3. True or False

if(x) and x=1, 1/2, 0.00001, 5.3, -1, "string" then condition is
always true.

typeof NaN === 'number' //true
if(x) and x=0, 0.0, null, undefined, "", NaN then condition is always false

NaN === NaN //false

 

4. String literals can be shown as follow

var string= "A quick brown \
fox jumps \
over the lazy dog";

 

5.  Check an object is empty by:

Object.keys(obj).length === 0

 

 

 

 

Understanding Hibernate session flushing

Tags

, ,

flush(): Forces the session to flush. It is used to synchronize session data with database.

When you interact with Hibernate, you do so via a Hibernate session. Hibernate sessions are flushed to the database in three situations:

  • When you commit a (Hibernate) transaction.
  • Before you run a query.
  • When you call session.flush().

 

In the Hibernate Manual you can see this example

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();


for ( int i=0; i<100000; i++ ) {
    Customer customer = new Customer(.....);
    session.save(customer);
    if ( i % 20 == 0 ) { //20, same as the JDBC batch size
        //flush a batch of inserts and release memory:
        session.flush();
        session.clear();
    }
}

tx.commit();
session.close();

Without the call to the flush method, your first-level cache would throw an OutOfMemoryException.

 

When you call session.flush(), the statements are executed in database but it will not committed.

If you dont call session.flush() and if you call session.commit() , internally commit() method executes the statement and commits.

So commit()= flush+commit.

So seesion.flush() just executes the statements in database (but not commits) and statements are NOT IN MEMORY anymore. It just forces the session to flush.

Multiple Inheritance in C#

C# doesnot support multiple inheritance. However, you can use interfaces to implement multiple inheritance

using System;
namespace InheritanceApplication
{
   class Shape 
   {
      public void setWidth(int w)
      {
         width = w;
      }
      public void setHeight(int h)
      {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // Base class PaintCost
   public interface PaintCost 
   {
      int getCost(int area);

   }
   
   // Derived class
   class Rectangle : Shape, PaintCost
   {
      public int getArea()
      {
         return (width * height);
      }
      public int getCost(int area)
      {
         return area * 70;
      }
   }
   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();
         int area;
         Rect.setWidth(5);
         Rect.setHeight(7);
         area = Rect.getArea();
         
         // Print the area of the object.
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

Result:

Total area: 35
Total paint cost: $2450