Object Oriented, Test Driven Design in C# and Java: A Practical Example Part #1

Download the code in C#

For a brief overview, please refer to this post.

At this point, many tutorials start by launching into a “Hello, World” style tutorial, with very little practical basis.

HelloWorld

This isn’t the most exciting concept, so let’s try a more practical example. Instead of churning out boring pleasantries, our application is going to do something a bit more interesting…build robots.

Robot

Specifically, our application is going to build awesome robots with big guns.

Ok, let’s get started with a narrative description of what we’re going to do.

“Mechs with Big Guns” is a factory that produces large, robotic vehicles designed to shoot other large, robotic vehicles. Robots are composed of several robotic parts, delivered by suppliers. Parts are loaded into a delivery bay, and are transported by worker drones to various rooms; functional parts such as arms, legs, etc., are dispatched to an assembly room. Guns…

View original post 876 more words

Working on a new compression algoriphm

Can’t disclose how it will work. But early tests indicate that if you take a reasonable sized RAR archive compressed to the max and place it in my container, you should expect to achieve a further 15-17% compression.

Now the plan is to finish the library and post the service as a website where you can compress and uncompress a single file. I will probably put a limit on the file around 50 Mb mark. This will allow me to gather vital statistics (without killing my server).

If all checks out I will add support for multiple files and release it as open source. Still thinking about the last step though 😛

Interesting behaviours with enum in .NET

This is completely plausible and the check is avoided first for performance reasons. Just imagine the CLR having to enumerate all the valid values and check whether yours falls in it, this no longer becomes a simple cast. The other reason is versioning. That is why you have Enum.IsDefined(…). Some developers now rely on this feature when dealing with an enum defining flags. When they want to set all flags they simply cast int.MaxValue to Enum

Bernado Nguyen-Hoan's Blog - Coding Stories from an IT Mercenary

When defining an enum type, we can assign an int value to each of the enum members. Below is an example.

Now there are two behaviours that you may not be aware of. Firstly, given the above definition, you can actually assign any int value to a variable of type Status. Let say I have a class as below.

The following code will then happily compile and execute:

OK, who on earth would be doing something like that? Probably not many, but this leads on to the second behaviour: by default, the value of the enum will be 0, even if 0 is not one of the defined enum members.

So with the code below, the value of record.Status will be 0, which does not match any of the defined status values:

This behaviour could have an impact on your code if not handled properly. This is because when defining an enum…

View original post 95 more words

IEnumerable vs IQueriable

dotnetbysatya

IEnumerable

-IEnumerable exists in System.Collections Namespace.
-IEnumerable can move forward only over a collection, it can’t
move backward and between the items.
-IEnumerable is best to query data from in-memory collections like
List, Array etc.
-While query data from database, IEnumerable execute select query
on server side, load data in-memory on client side and then filter
data.
-IEnumerable is suitable for LINQ to Object and LINQ to XML
queries.
-IEnumerable supports deferred execution.
-IEnumerable doesn’t supports custom query.
-IEnumerable doesn’t support lazy loading. Hence not suitable for
paging like scenarios.
-Extension methods supports by IEnumerable takes functional
objects.

IEnumerable Example

MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p =>
p.Name.StartsWith(“S”));
list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee]
AS [t0]
WHERE [t0].[EmpName] LIKE @p0

Notice that in this query “top 10” is missing since IEnumerable
filters records on…

View original post 155 more words

Converting XML or JSON to C# Classes in Visual Studio

Kapil's space

In the Visual Studio 2012 IDE, a new feature is introduce to convert XML document into C# classes as a Serializable type.

In the .NET framework 4.5 there is another Paste Special option is exists in the Edit menu which enables you to copy the XML as C# Classes.

Paste-xml-as-classes.png

Copy the XML  you want to create a class/classes for, place the cursor in a class file on the location you want the code to be added and select the following menu items:

  • Edit.
  • Paste Special.
  • Paste XML as Classes.

And you’re done.

Suppose you have following XML File.
xml-in-vs-2012.jpg

And when you paste the above XML using Paste XML as classes. It will looks like that.

xml-as-classes.jpg

View original post

Covariance and Contravariance. What do they mean again?

My Software Notes

I don’t have to deal with covariance and contravariance very often, so every time I do I forget which is which. So, just for my own benefit, here is a note describing them:

Covariance and contravariance are terms that refer to the ability to use a less derived or more derived type than originally specified.

Covariance – Enables you to use a more specific type than originally specified

E.g., in generics

We have a base type called “Feline” and we have a derived type called “HouseCat”.

IEnumerable<HouseCat> cats = new List<HouseCat>();
IEnumerable<Feline> gods = cats;
//Note: felines have a very high opinion of themselves 🙂


Contravariance – Enables you to use a less derived type than originally specified.

E.g., in generics

Using the same base and derived types as above.

IEnumerable<Feline> gods = new List<Feiline>();
IEnumerable<HouseCat> cats = gods;


References:

Covariance and Contravariance (C# and Visual Basic)

Covariance and Contravariance…

View original post 2 more words

ASP.NET MVC – If only we had it 10 years ago

I realised that one of the technology or rather a group of the technologies that stops me from applying for some jobs is ASP.NET MVC + the usual JavaScript stack.

So I bought 2 books on the subject and have been playing with the technology for couple of days now. And I must say things have improved A LOT since I’ve been doing Web Development.

Back in 2003 I were working on an Order Management System and my boss being a perfectionist and a great fan of Google, requested that we created an order input form, comprised of 6 tabs without any postbacks to the server (‘coz they are evil)

All we had at our disposal was ASP.NET and JavaScript. But somehow we managed to archive what he wanted. We of course had to use some serious black magic to fetch data asynchronously (remember no AJAX back then) instead we realised we could use hidden Iframes to fetch all the required data. And the application (the order entry form) became extremely nice to use.

The traders would no longer have to remember the exact ticker, as long as they knew the name the app would asynchronously go and fetch corresponding tickers.

Once you selected a ticker, you would be presented with some information about the instrument, settlement date and the fees would automatically default based on the market where the instrument was traded on, if it was a bond it will also calculate yield, time to maturity, for convertible bonds it will show you all the greeks and much more (limits, holiday calender per market, settlement periods, compliance stuff, history of changes etc). Not only that but the could switch to a different tab effortlessly to see the executions and modify them if necessary. I’m not going to go to cover the other 95% of features, but the app was brilliant in terms of usability, only Google could complete.

image

Continue reading “ASP.NET MVC – If only we had it 10 years ago”

Parsing HTML in C#

C# Disciples

Today I stumbled upon a bizarre problem, I wanted to parse an HTML for a site to find out if the site contains any RSS feeds. After some research I found out that finding the RSS feed for a site is not that hard, you have to look for an element that looks like this

image

“Easy task”, I said, me Mr.Optimistic; “I will just load it up in XElement and using Linq to XML to get the data I want”. BUT guess what the web is filled with crazy HTML that a standard .NET parser such as XElement just gives up on and blows up in flames.

After some heavy head banging to walls and ceilings, I found the solution, HtmlAgilityPack. This open source project lets you load HTML even if it is not in a good shape. With some options HTMLAgilityPack will fix these errors and then you…

View original post 36 more words

Anything Over Anything – Tunneling Sofware

http://AoA.codeplex.com – tunneling software written using the pre-release version of the Rx framework. Currently the implementation only supports http protocol, the exchange of data is done by posting the data in http request and then returning the data accumulated on the server. This is done several times a second and requires an normal one directional http access.

The abstraction of the tunnel makes it easy to tunnel any protocols traffic over another protocol, thus the name – Anything Over Anthing.

Would be very glad if someone could add to this project

Blog at WordPress.com.

Up ↑