.NET Enum Comparison

       

Scenario:

Compare 2 Enums 

Solution:


     public enum Mode
        {
            Dark,
            White
        }
    
    
    var mode =  IsMobile()
                        ? Mode.White
                        : Mode.Dark;
    
    return data
    	.Where(i.Mode.Equals(mode))
    	.Select(data.render);

NHibernate

 

Scenario:

Use a ORM framework to work with your database.

Solution:

NHibernate is an open-source ORM framework, which is persistence layer for the .Net  based on Object-Relational Mapping Technique. It  creates a "virtual representation" of database objects within the code. 

NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading and storing the objects.

Object Relational Mapping (ORM) is a technique used in creating a "bridge" between object-oriented programs and, in most cases, relational databases. It solves the issues like DB does not have inheritance and the structure is diffrent between DB and classes.

NHibernate, persists the object into the relation database and can do CRUD operations, you dont have to write SQL queries, the data access logic can be contained in  the application. 

1. The XML mapping file, which can be done in the code as well.
2. Configuration file
3. POCO
They need to have atleast one id property
All properties need to be marked Virtual


    //Construct a Session Factory
    
    var sessionFactory  = Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2008.ConnectionString(sqlConnection))
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyContext>())
                    .BuildSessionFactory();
    
    //start a session and save data/ Flush mode could be Manual, Automatic and commit time
    using (var session = NHibernateHelper.OpenSession()
    Session.FlushMode = FlushMode.Manual) { 
    
    using (var transaction = session.BeginTransaction()) { 
       var user = new User { 
    	  FirstName = "Ram", 
    	  LastName = "Mangal" 
       }; 
    		
       session.Save(customer); 
        
       session.Flush();
       transaction.Commit();
       session.Close();  
    } 
    
    
    //Mapping
    class UserMap : ClassMap<User> { public UserMap() { Id(x => x.Id); Map(x => x.FirstName); Map(x => x.LastName); Table("User"); } //load data using (ISession session = NHibertnateSession.OpenSession()) { using (var tx = session.BeginTransaction()) { /*** Using Query ***/ var users = session.Query<User>().ToList(); foreach (var user in users) { //read user data } tx.Commit(); } }

Sql Commands

Queries:


--convert DateTime to Date

CAST(myDateTime AS DATE)

 

Create Data Pool using Concurrent Queue

      

Scenario:

Create Data Pool of numbers using Concurrent Queue.

Solution:

C# ConcurrentQueue is a thread-safe collection class. It is introduced in .NET 4.0 with other concurrent collection classes. It provides a thread-safe First-In-First-Out (FIFO) data structure. ConcurrentQueue exists in System.Collections.Concurrent namespace. ConcurrentQueue is a wrapper around generic Queue class

    using System.Collections.Concurrent;
    
    namespace TestProject
    {
        public class PoolNextData
        {
            private readonly Dictionary<string, ConcurrentQueue<long>> _dataPool;
            private int _poolLength;
    
            public PoolNextData()
            {
                _dataPool = new Dictionary<string, ConcurrentQueue<long>>();
            }
    
            private long[] GetNextData(ConcurrentQueue<long> pool, string kind, int noOfRecords)
            {
                var result = new List<long>();
    
                for (var i = 0; i < noOfRecords; i++)
                {
                    long data;
                    if (!pool.TryDequeue(out data))
                    {
                        break;
                    }
    
                    result.Add(data);
                }
    
                //not enough available, put it back
                if (result.Count < noOfRecords)
                {
                    FillPool(pool, kind, noOfRecords - result.Count + _poolLength);
    
                    result.ForEach(pool.Enqueue);
                }
                else
                {
                    Task.Factory.StartNew(() => FillPool(pool, kind, noOfRecords));
                }
    
                return result.ToArray();
            }
            private void FillPool(ConcurrentQueue<long> pool, string kind, int noOfRecords)
            {
                //Get next numbers list from DB
    
                var nos = new List<long>();
                nos.ForEach(pool.Enqueue);
            }
    
            private ConcurrentQueue<long> GetPool(string kind)
            {
                var result = new ConcurrentQueue<long>();
                _dataPool.Add(kind, result);
    
                return result;
            }
        }
    }

Create Self Signed Certificate

            

Scenario: Scan your website for SSL vulnerabilities

Solution:

Create Self Signed Certificate

Below are the steps:

  1. mmc -> Add/Remove Snap-ins -> Certificates -> Computer Account -> Finish -> Ok
  2. Certificates -> Personal -> Certificates -> Request New Certificate -> Active Directory Enrollment Policy -> Web Server 2010 -> Certificate Properties
  3. Name - {cert_name}
  4. Subject name:
    1.   Common name - *.subdomain.domain.com
    2.   Email - test@test.com
    3.   Organzation - Test
  5. Alternative name: 
    1. DNS - my.site1.test.com
    2. DNS - my.site2.test.com
    3. DNS - my.site.test.com
    4. ipv 6: - ip address
  6. Certificate is now available.
  7. IIS -> Web Site -> Bindings -> 443 -> SSL certificate
Use Open SSL to create certificate:
    >openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr -config ssl.conf
    
    >openssl x509 -req -in server.csr -signkey server.key -out server.crt
    
    >openssl pkcs12 -export -out gfcclocal.pfx -inkey server.key -in server.crt
    [req]
    distinguished_name = req_distinguished_name
    x509_extensions = v3_req
    prompt = no
    [req_distinguished_name]
    C = US
    ST = NY
    L = New York
    O = Test, Inc.
    OU = MyDivision
    CN = *.cloud.test.com
    [v3_req]
    keyUsage = critical, digitalSignature, keyAgreement
    extendedKeyUsage = serverAuth
    subjectAltName = @alt_names
    [alt_names]
    DNS.1 = my.site.test.com
    DNS.2 = my.site1.test.com
    DNS.3 = my.site2.test.com
    DNS.4 =  ip address

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...