Thursday, June 27, 2013

A Date Check - To Narrow Down the Problem

I was facing an issue this week, wrong date of birth was shown for all the patients. When I look at closer, all the patients had same date of birth as "Jan/1/1970". Then I understood that "Jan/1/1970" must be the default date value of .net/sql server. It means somewhere we missed to assign the value for DOB field. Later we figured out, that it was a javascript issue. "Jan/1/1970" is the default date value of javascript. We missed to assign javascript DOB variable. So, as soon as you see "1970" instead of some other value, you can guess that something goes wrong in javascript.

.Net default date value is Jan-01-0001

Sql server default date value is NULL, starts from Jan-01-1753

Oracle default date value is NULL, starts from Jan-01-4712

Javascript default value is Jan-01-1970

Sunday, February 3, 2013

Why @@SERVERNAME Returns NULL?

The @@SERVERNAME can return NULL, when you accidentally delete the server. Most probably by this query

sp_dropserver '<ServerName>'

After the SQL Server restart, the @@SERVERNAME started returning NULL. You can confirm this; if the following query returns no records, the local server was deleted.

Select * From sys.servers Where server_id=0

Solution 1

Run the following query to add the server back.

sp_addserver '<ServerName>',local

Solution 2

I would recommend you to use

Select ServerProperty('ServerName')

This approach returns the machine name, even if the sever name is not present in the sys.servers table.

The SQL Profiler Secures Username and Password

Whenever you run a login related query, the SQL Profiler converts the credentials as ‘----’ (hyphens). This feature improves SQL server security.

For example when you run this query

EXEC sp_addlogin 'Victoria', 'B1r12-36';

The profiler hides the username and password by substituting with hyphens

--*sp_addlogin---------------------------

Similar way, when you run sp_addlinkedsrvlogin, it will display as

--*sp_addlinkedsrvlogin---------------------------

Designing a Wrapper

Here I am sharing my experience with wrappers. This article is intended for beginners. Nowadays it is a fashion to create a wrapper for every third party library. The theme of this article is to think beyond the present situation. An unnecessarily created wrapper or a poorly designed wrapper will lead to messy code.

Wrappers created only for the two reasons.

  1. Wrapper provides abstraction to a library and makes the developer life easy.
  2. Example: When you have a third party library which has wide range of functionality, but your application needs only very few of them. You should create a wrapper
  3. Wrapper enforces best coding practices of the product to use a third party library.
  4. Example: When there are many options in the third party library but few of the options can only be compatible with your application; you should hide/abstract others.

Points to consider when designing a wrapper

  1. Ask yourself, are you extending the functionality by a wrapper? If that is the case go for extension methods (C#). Unless until you have a solid reason, don’t create a wrapper.
  2. Look around! You may get lot of wrappers from online. But, you need to validate them properly whether it fits your needs and has it well designed. I can see lot of wrappers in code project.
  3. Never create a wrapper with reflection or dynamic calls. The degree of dynamism should be very minimal. In my experience introducing generic types are the maximum limit for designing a wrapper with dynamism. Because of dynamism, when there is a bug, the developer can’t even search it them in Google and can’t get help from any online forum. The wrapper should be very simple and neat.
  4. A poorly designed wrapper restricts thinking ability; you should decide what level of abstraction is needed.

    Example: One of the NHibernate wrappers didn’t allow me to write QueryOver for sub queries. I couldn’t alter the wrapper immediately, because it has dynamic and reflection calls. To understand those dynamic calls, it would have been taken 2 more days. I had to find alternative way.

  5. Think about production issues; never leave a wrapper half-way designed. The wrapper should cover all functionalities of the third party library. You should not design it for the present situation; if the third party library is too big, at least your wrapper should have some guidelines in future if some others want to extend the wrapper. When there is a production issue comes in, developers are forced to fix them as soon as possible. When they fix bugs, they generally don’t alter/extend the wrapper’s design. The code will become messy when the developer finds alternative way to fix the bug.

    Example: We faced a production issue, w3wp.exe was crashing. All our sites were down. The reason was, the wrapper for SqlConnection didn’t handle the SqlDataReader’s close operation. As open connections were not closed, the number of connections crossed the maximum limit and crashed the application. Instead of redesigning the wrapper, we first closed all the opened connections properly by typing “sqlConn.Close()” in 72 places of the application. Then we took time to refine the wrapper.

  6. Writing wrappers often lead to performance problems. You should be careful on that.

Sunday, January 20, 2013

Why Scala Becomes So Popular?

This article is intended for developers who have an agenda to learn at least one programming language in a year. I recommend them to go for Scala this year. Also I strongly believe Scala can become the next best programming language. Here are the reasons.
  1. Scala runs on JVM; takes all the advantages of JVM. Scala is platform independent, object oriented and strongly typed.
  2. Scala has well structured type system than Java. Scala uses unified type system as in C#. For an example, all the types are derived from the base type “Any”. “Nothing” is a subtype of everything and not super type of anything.
  3. Scala syntax is similar to Java. Scala is easier to learn than any other functional language.
  4. You don't have to worry about third party tools for Scala. Third party tools and libraries for Java are matured. Almost all the Java development tools can also be used for Scala. For an example, JUnit can be used as unit testing framework for Scala.
  5. Scala is functional. Functional languages are suitable for scalable architectures. In theory functional languages don't allow to alter the variable's state. All the variables are immutable. (In that way, Haskell is a pure functional language, you can't change the state at all.) The advantage of immutability is thread-safe. You can apply parallelism and threading concepts even if the system is so big and rapidly increasing in size. Scala gives good support for concurrency.
  6. Scala has very good open source IDE supports. I prefer to use Eclipse plug-in for Scala. IntelliJ IDEA from JetBrians is also a good one. The community edition of IntelliJ IDEA is free. Visual Studio plug-in for Scala is also available here.

Comparison with other functional languages

  1. Haskell is another powerful functional language, but it needs a deep learning curve. It doesn't have proper open source editor. The third party tools and libraries for Haskell are not matured enough. Building and installing GHC (Glasgow Haskell Compiler) is very harder than Scala installation.
  2. Though Python is matured and functional, Python has limited support for functional paradigm. Python is not considered for enterprise systems. One of our products in Python didn't scale and faced severe performance problems. Python is suitable for string manipulation or string search based applications. Python is best suited for dynamic programming, so comparing Scala with Python is not fair. (My guess is Groovy may overtake Python in near feature. Groovy also runs on JVM.)
  3. Like any other functional languages Scala compiler supports type erasure and type inference. Checked exceptions in Java is annoying. This is eliminated from Scala. Scala has a lot of developer friendly improvements from Java.
  4. Like F#, Scala also supports multi-paradigm (functional and imperative). One of the reason F# seems to be scary to me is its syntax. Scala syntax is similar to Java, But F# syntax is not similar to C#. See Here. Scala is much more minimalistic language than F#. Scala has a very small orthogonal set of constructs that are re-used throughout the language. F# seems to introduce new syntax for every little thing, thus becoming very syntax heavy as compared to Scala. Scala has 40 keywords, whereas F# has 97”.
  5. Also you can find currying, tuples, anonymous functions, pattern matching and lazy evaluation in Scala.

Clojure is also getting popular. Since I don't have knowledge on Clojure or LISP, I can't compare with Clojure.

Any talk on languages is controversial, the above points are my personal views. I welcome your feedback.

Sunday, November 18, 2012

Could not load type - The Structuremap Error

Recently I got an structuremap error when I launch my application. The error message as follows
Error Message: Exception has been thrown by the target of an invocation. - 0%System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> 
System.TypeLoadException: Could not load type 'MetricsEngine.ApplicationServices.Metrics.IMetricService' from assembly 'MetricsEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

at WebApplication6.Bootstrapper.Registries.ClinicalIntegrationRegistry.<.ctor>b__0(IAssemblyScanner scanner)
at StructureMap.Configuration.DSL.Registry.Scan(Action`1 action) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Configuration\DSL\Registry.cs:line 250
at WebApplication6.Bootstrapper.Registries.ClinicalIntegrationRegistry..ctor() in E:\Src\SolutionFolder\WebApplication6.Bootstrapper\Registries\MetricsRegistry.cs:line 33
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at StructureMap.Graph.PluginGraph.ImportRegistry(Type type) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Graph\PluginGraph.cs:line 232
at System.Collections.Generic.List`1.ForEach(Action`1 action)
at StructureMap.Configuration.DSL.Registry.ConfigurePluginGraph(PluginGraph graph) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Configuration\DSL\Registry.cs:line 397
at StructureMap.Graph.AssemblyScanner.ScanForAll(PluginGraph pluginGraph) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Graph\AssemblyScanner.cs:line 249
at StructureMap.Graph.PluginGraph.Seal() in c:\BuildAgent\work\767273992e840853\src\StructureMap\Graph\PluginGraph.cs:line 121
at StructureMap.PluginGraphBuilder.Build() in c:\BuildAgent\work\767273992e840853\src\StructureMap\PluginGraphBuilder.cs:line 72
at StructureMap.ObjectFactory.Initialize(Action`1 action) in c:\BuildAgent\work\767273992e840853\src\StructureMap\ObjectFactory.cs:line 65
at WebApplication6.Bootstrapper.Bootstrapper.RegisterContainer() in E:\Src\SolutionFolder\WebApplication6.Bootstrapper\Bootstrapper.cs:line 27
at WebApplication6.Bootstrapper.Bootstrapper.Bootstrap() in E:\Src\SolutionFolder\WebApplication6.Bootstrapper\Bootstrapper.cs:line 20

Reason:

My project looks very similar to the following picture.
In the above picture, I am referencing two different dlls from two different sources with the same name "MetricsEngine.dll". Now during the bootstrap the structuremap supposed to load it's referenced assembly(0.0.4696.21382) which is from vendor1, but it loads the assembly (1.0.0.0) from vendor2 which is referenced by WebApplication6.EngineServices. Though both the assemblies had different public key token and different version, it always loads the one from vendor2. The reason is structuremap uses reflection to load the assemblies dynamically at runtime; it scans though current project's referenced assemblies and it's references and so forth. In that way assembly MetricsEngine.dll from vendor1 and vendor2 are considered by the structuremap, now it picks the latest version, in our case it picks 1.0.0.0. It doesn't care about assembly's identity (public key token).

Temporary Solution:

I tried, But I couldn't figure out the right solution. For time being after downloading the assembly I renamed the assembly manually and referencing it in my project. I uninstalled that package from NuGet. That is, I am not using NuGet for one of the referencing assembly. Once I figure out the solution I will update this post.

Tuesday, November 13, 2012

The Moore's law and the Core's question

Preface:

This article describes, what can be the next generation computer? Processor making companies like Intel keep reducing the chip size or they increase the efficiency in the same chip size in their every next release. The hardware improvement for every two years is an inevitable requirement. The software operating data size increases day by day, the gaming softwares require very good hardware acceleration. Now the chip companies couldn't reduce the chip size as they could before; they are shipping more than one processor in one suite; they call them as dual core, quad core and so on.

Moore’s Law:

The number of transistors on integrated circuits doubles approximately every two years. Wiki Link. Many have predicted that Moore's Law will soon reach its end.

The semiconductor is the problem:

There is a limit for reducing the size, since we know the processors are made up of silicon and germanium type semiconductors. They are solid, after certain level we can’t reduce their size. What could be the solution for next generation?

Solution:

As of now, there are two ways.
  1. The quantum chips.
  2. The bio chips.
The Bio chips can be made up of either DNA or RNA.

Quantum computer chips:

Rather than encoding ones and zeroes into high and low voltages that switch transistors on and off, the idea is to use the electron's spin. The Princeton and the Wisconsin university are the pioneers of this research. The electron spin has two directions; clockwise and anticlockwise. Scientists are making use of this spin and generating binary digits.

Earlier Version of the Theory:

The earlier version of this theory when I studied in 2004 uses a pair of electrons. In quantum mechanics two entangled particles cannot be viewed individually, even after they leave the interaction zone, where they became entangled. They act as a single quantum object. For example, there are two electrons e1 and e2. They can be used to express 4 different binary numbers.

Example:

To express decimal 14 in quantum computers we need two pair of electrons.That is from the above pictures,

Anticlockwise e1 + Anticlockwise e2 + Anticlockwise e3 + clockwise e4 = 14
Where as in conventional computer we need 4 transistors. That is,

Transistor t1 high voltage + t2 high voltage + t3 high voltage + t4 low voltage = 1110 = (decimal 14)

Transistors are not comparable with electrons in terms of speed and the space requirement.

The Modern Version:

The modern theory deals with electron’s superposition; hence by using single electron we can express 4 different binary numbers as follows.

Example:

In the modern version only two electrons are required to express decimal 14. From the above pictures,
Superposition state anticlockwise e1 + Superposition state clockwise e2 = 14

The conventional computer expresses in the unit of bit, in quantum computer it is qbit (quantum bit). In this case it requires 2 qbits.

Advantages of quantum computers:

  1. Size problem will go away, since electrons are super tiny. Also weightless.
  2. The speed, the current conventional PC’s are about 1-5 GHz speed, quantum computer’s speed will be at about 1-5 THz.
  3. It can be highly parallel computer, because of the electron’s superposition. Understanding electron’s superposition requires some effort, I am not covering here. For now, consider it is an electron's transitioning state

Challenges:

Computer’s speed cannot be improved only by improving processor. Other peripherals’ speed is also important. Scientists are working on them as well. Some researchers suggesting LASER for bus interface.

The DNA computers:

The idea is to use high and low concentrations of these molecules to propagate signals instead of high and low voltages that switch transistors on and off. It is medically proven that DNA can store biological information. Scientists are trying, if DNAs’ can be a better storage for computers? Can they be used for calculations?

The DNA (Deoxyribonucleic acid) contains nucleobases. The primary nucleobases are A, T, C and G. Adenine, thymine, cytosine and guanine are the expansion for them respectively. The DNA is a pair of strands. The strand is made up of strings A with T and C with G. Here is the basic concept for DNA computing.

In a conventional computer, in a bit you can store either 1 or 0. But in a DNA computer you can represent a bit by using these four variables. That is, it can be.

Example:

The same example to represent decimal 14 for DNA computers will be much more clear,
TAGC + TACG => 1110 => 14

Benefits of DNA computers

  1. One Kg of DNA can store the data from all the electronic computers ever built.
  2. DNA computer of half inch will be more powerful than supercomputers.
  3. In one cubic centimeter we can fit 10 trillion DNA molecules. With this size, a computer would be able to hold 10 TB of data, and perform 10 trillion calculations at a time.
  4. By adding more DNA, more calculations could be performed. Scalability! Also they can be highly parallel.
  5. DNA’s extracted from cellular organisms, there will always be a supply of DNA. So these computers will be cheap.

The RNA computers:

The RNA (ribonucleic acid) molecules are very similar to DNA molecules; the only difference is; RNA uses U (uracil) instead of T (thymine). Hence A, G, C and U are called RNA-bases. Everything above mentioned for DNA computers is also applicable for RNA computers.