RSS

Category Archives: General

Does ConcurrentDirectory Value Get Locked Automatically?

According to MSDN doc, lock is not necessary with ConcurrentDictionary. Does the underlying object in the dictionary also gets locked automatically?

var items = new ConcurrentDictionary<int, List>();

public class Item
{
    public string Name {get;set;}
    public double Cost {get;set;}
}
 
Leave a comment

Posted by on August 15, 2017 in General

 

Adding ASP.Net Identity (with OWIN) to Existing MVC 5 Project

The simple way is start empty MVC project with authentication and pick and choose ASP.Net Identity part you want to implement.

Here is the summary:

  • Install dependencies. See https://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity, scroll down to “Components of ASP.Net Identity”
    • Entity Framework
    • OWIN
    • Microsoft.AspNet.Identity.Core
    • Microsoft.AspNet.Identity.EntityFramework
    • Microsoft.AspNet.Identity.Owin
    • Microsoft.Owin
    • Microsoft.Owin.Host.SystemWeb
    • Microsoft.Owin.Security
    • Microsoft.Owin.Security.Cookies
    • Microsoft.Owin.Security.OAuth
    • Microsoft.Owin.Security.Google (if you want allow external login with Google)
    • Microsoft.Owin.Security.Facebook (if you want allow external login with Facebook)
    • Microsoft.Owin.Security.Twitter (if you want allow external login with Twitter)
    • Netwonsoft.Json
  • In Web.config
    • Under
      <system.web>

      Add

      <authentication mode="None" />

      This is to remove IIS authentication, because we are going to use OWIN

    • Add all OWIN dependencies
  • Add Startup.cs. This is a startup class run by OWIN
    • Don’t forget its dependency, Startup.Auth.cs in App_Start folder
  • Add all classes in IdentityConfig.cs
    • There are 4 classes, EmailService, SmsService, ApplicationUserManager, ApplicationSignInManager. I’d like to separate them into different files
  • Add all classes in IdentityModels.cs
    • There are 2 classes, ApplicationUser, ApplicationDbContext. I’d like to separate them into different files

Those are the basic setup needed for ASP.Net Identity. The rest of the setup is front-end side, AccountController and ManageController, which has its own view models and CSHTML.

 
Leave a comment

Posted by on December 27, 2016 in General

 

Tags: , , ,

Mutual Exclusion

The requirement of ensuring that no two concurrent processes are in their critical section at the same time; it is a basic requirement in concurrency control, to prevent race conditions.

Source: Wikipedia

 
Leave a comment

Posted by on June 23, 2015 in General

 

Tags: ,

Final in Java

If you are a C# developer, like myself, and start learning Java (for whatever reason), there will be a lot of ‘what-the-heck’ question. One of mine was the final keyword in Java.

Turn out, final means just that, final. You can only initialize it once and it can’t be changed anymore for its entire life. I think in C#, we call it readonly.

Immutability with Final

The keyword final is often used to achieve immutability in Java. However, it won’t make the objects themselves immutable. In other words, final only gives you the sense of immutability, but it doesn’t modify the object to be truly immutable.

Primitive types such as int, long or short can be reassigned after being defined. final will prevent this.

int i = 42; //int is of primitive type
i = 43; // OK

final int j = 42;
j = 43; // does not compile. j is final so can't be reassigned

Reference types cannot be made immutable just by using the final keyword. final only prevents reassignment.

final MyObject m = new MyObject(); //m is of reference type
m.data = 100; // OK. We can change state of object m (m is mutable and final doesn't change this fact)
m = new MyObject(); // does not compile. m is final so can't be reassigned

Source: Wikiepedia

 
Leave a comment

Posted by on June 22, 2015 in General

 

Tags:

Rename Table and Column Name in EF Code First

Business rules change over the time. For developers, this can be frustrating. Especially after you have spent enormous amount of time to name your objects properly. After all, we all know naming is the most prominent process of the development.. 🙂

Luckily, in Entity Framework, you can change table names quite easily.

Two ways, using data annotation and Fluent API. (Code is in Entity Framework 6)

Data Annotations

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

// Change table name to People
[Table("People")]
public class Employee
{
    // Change column name to PersonId
    [Column("PersonId")]
    public int Id { get; set; }
    public Guid DepartmentId { get; set; }
    public int CompanyId { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

FluentAPI

Context file (inherit from DbContext)

using System.Data.Entity;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Change column name to PersonId
    modelBuilder.Entity<Employee>()
        .Property(p => p.Id)
        .HasColumnName("PersonId");

    // Change table name to People
    modelBuilder.Entity<Employee>()
        .ToTable("People");
}
 
Leave a comment

Posted by on June 18, 2015 in General

 

Tags: , ,

Producer-consumer Problem

Two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer’s job is to generate a piece of data, put it into the buffer and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.

While it may sound like a simple problem, the solution is not as easy. Not even simple. There are many sub-impediments you have to watch:

  • Locking / unlocking mechanism.
  • Multiple access to the semaphore.
  • Concurrency and mutual exclusion / race conditions.

Source: Wikipedia

 
Leave a comment

Posted by on June 17, 2015 in General

 

Tags: ,

JavaScript Bundle Doesn’t Work on ASP.Net MVC 5

Developers often make this very common mistake. You start an ASP.Net MVC app, add few JavaScript files and libraries, run. Everything works fine but the JavaScript.

ASP.Net MVC Bundling

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-1.*"));

    bundles.Add(new ScriptBundle("~/bundles/startstop").Include(
                "~/Scripts/startstop*"));
}

ASP.Net MVC bundle has default ignore list and one of ignore list is ‘*.min.js’ files. Yes, any minified JavaScript file.

You can add the following code RegisterBundles to clear the ignore list before adding anything to the bundle.

bundles.IgnoreList.Clear();

Once you clear the list, you are also able to build your own ignore list.

bundles.IgnoreList.Ignore("");
 
Leave a comment

Posted by on June 12, 2015 in General

 

Tags: , ,

 
%d bloggers like this: