The type or namespace name ‘*’ could not be found (are you missing a using directive or an assembly reference?)

Check the .Net version of the library and consumer. It should match or lower version.

Advertisements

Missing type map configuration or unsupported mapping. Mapping types: IDataReader -> List`1 System.Data.IDataReader ->

You will often find this error when converting IDataReader to POCO. There are 2 resolutions here –

if you are using Automapper 4.0 or above, check MSBuild probably have deleted Automapper.Net4.dll in bin folder, if it is, restore it.

Second, IDataReader is unsupported from Autoamapper. you can use below 4.0 (or 3.1.1), which supports IDataReade, unless you are not using specific version features.

The Decorator Design Pattern using VS 2010

The decorator pattern can be used to extend the functionality of a certain object at runtime, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new decorator class that wraps the original class.

Decorator in abstract –

image

Source: Wikipedia.org

Suppose, you are commissioned for the software program which produces Computer, along with peripherals, sales. Assuming, you design a Computer class with description method which returns some string value identifying it’s peripherals support viz. monitor, hard-disk, pointer device, keyboard, etc.

Without knowing much of the vision ahead, you design a class like this –

namespace DecoratorPattern
{
     public class Computer
     {
         public Computer() { }
         public void Description() { Console.WriteLine("This is just computer!"); }
     }
}

and when company decided to sale the Computers with assembled peripherals like monitor –

namespace DecoratorPattern
{
     public class Computer
     {
         public Computer() { }
         public void Description() { Console.WriteLine("This is just computer with a monitor!"); }
     }
}

along with, some input and pointing device like mouse, keyboard and storage such as hard disk.

namespace DecoratorPattern
{
     public class Computer
     {
         public Computer() { }
         public void Description() { Console.WriteLine("This is just computer with a monitor, pointing device, keyboard and a harddisk!"); }
     }
}

Well, this is one way of avoiding kind of situation to modify the class perpetually add the code and workaround for the solution. Let us look on Decorator design pattern how it will help us to solve this problem by writing a wrapper class where developer no longer have to modify the method for updates.

Starting off again, we have Computer class with Description method. Instead of modifying it every time and to implement Decorator pattern, lets create a abstract class with extending Computer class with Description method and pass on the Computer object to the Disk class. Thus, Disk object will call the Computer.Description() method in its own Description method and will add the text return from Computer.Description(). Lets look at the steps –

Wikipedia says –

  1. Subclass the original “Decorator” class into a “Component” class (see UML diagram);
  2. In the Decorator class, add a Component pointer as a field;
  3. Pass a Component to the Decorator constructor to initialize the Component pointer;
  4. In the Decorator class, redirect all “Component” methods to the “Component” pointer; and
  5. In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.

In general,

  • Create the  abstract decorator class extending Computer object.
  • Create a class extending decorator abstract class.
  • Take the object which has to be extended and store object.
  • Call base method with will return value and extend/manipulate the value and return back.

Lets see how the decorator can be implemented using VS 2010

Computer component:

namespace DecoratorPattern
{
     public class Computer
     {
         public Computer() { }
         public string Description() { return "This is computer with motherboard, ram, 4 usb connectors"; }
     }
}

 

Decorator –

namespace DecoratorPattern.Abstracts
{
     public abstract class ComponentDecorator: Computer
     {
         public abstract string Description();
     }
}

Concrete decorator –

namespace DecoratorPattern
{
     public class Disk: ComponentDecorator     
{
        Computer _computer;
        public Disk(Computer c)
         {
             this._computer = c; ;
         }
         public override string Description()
         {
             return _computer.Description() +  ", disk";
         }
     }
}

Download source code here

References – Wikipedia.org, VTC examples.

Strategy Design Pattern implementation in .Net using VS 2010

Let’s go through the problem first design to elevate. Let’s assume you are in-charge of task for designing set of Vehicles. To start off with,you will surely design a Vehicle abstract class which will support certain set of common characteristics like Change Gear, Type, Engine, Number of Gears,

image

Let’s extend Vehicle class to Street Racer vehicle

image

StreetRacerVehicle is called at a client and will invoke go() method.

image

Going further, business objectives changes and it started another production line of producing Formula1 cars, hence, you decided to implement Formula1 class by extending Vehicle class. Obviously, it will support the basic characteristics of the Vehicle.

image

Now, we have created two different types of objects here of type – StreetRacer named racer, and ForumlaOne named forumla1.

image

When executed, the output will be as below –

image

And, if you further extend the class for further vehicles viz. Helicopter, and instantiate the class at client and invoke go() method, you will get the same set of output. You see the problem has started now, as if you go on extend the Vehicle class, it will return the same output irrespective of how it is implemented or rather say, which Vehicle should be drove, or fly. The problem is you have classes that extends the abstract to the functionality agreed with the base class functionality. Solution could be override the go() method in each of the new classes and the output will be much different. Like –

image

image

image

However, you are now maintaining separate code for various generations of inheritance, and it will spread out the various generations of code when extended or inherited classes. This is where Strategy Patterns addresses the problem that you can write the code in base class without overriding the base class functionality in inherited classes or in all the multiple generations of classes.

Strategy Design Pattern says instead you should have code with algorithm variable. Variable is changed based on the series of objects that base class is derived to. That is, the object being is worked upon or object that has been derived to. The idea is to separate out algorithm code from the class into algorithm object and allow your algorithm object to be different and you use algorithm object that will let you supply different method that use different algorithms. Using the polymorphism technique, you can thus identify the object variables. Hence, this is different way of looking at Object way of programming and the relationship between the base and derived class will have a “has a”.

courtesy – wikipedia.org

Let’s write a Interface which will support the change in behviour of vehicles, but still supports common characteristics like go, Engine Start, Engine Stop, Change Gear, Has Gear, etc.

image

Now, implement the interface using different implemented class – like explained above, create a interface of common characteristics and implement it differently.

image

Let’s change the abstract class Vehicle to support the Strategy pattern.

image

What it has been done is we are storing the algorithm object as a member object to utilise and invoke the correct algorithm of type object. Hence, of any type vehicle can now support different behavior by setting the proper algorithm when initialized like for FormulaOne and StreetRacer it will be driving, for Helicopter – it will be flying and for Jet it will be fast flying. For setting different algorithm based on the derived class, we have to set inside the constructor of each derived class of Vehicle. Like –

image image

 image image

When the solution is ran, following output is generated –

image

Parallel Programming in .Net 4

Please note: If you are not aware of Parallel Programming, this article is for you else read it for your revision.

Every developer had come across performances issues and lots of brainstorming goes on when customer is not happy with the app performance.

Traditionally, we Improve application performance by – Remove scope of bad-coding, increase application productivity, writing efficient code, improve and optimize SELECT queries, and what not we do to increase and improve application performances, but we tend to forgot that hardware has certain limitation too. Like a single core (processor aka CPU) will execute single thread a time … logical enough. Right? Then comes Asynchronous process into picture, but again with its limitation – defining locks and releasing those, is again a tedious task, and if any is missed, your application will tend to crash. Oh! Did I pointed you can have Async process in ASP.Net to scale your applications and improve performances to certain limit. But this is not parallel programming, its multi-threaded programming.

Parallel vs. Concurrent

Concurrent is accessing the same resources or sharing resources for different problems. old skool example – 2 users accessing same table and even more worse accessing same row. thus increase chance of dead locks.

Parallel is dividing a certain task into a set of related tasks to be executed concurrently. latest example, update tweets/post on twitter.com/facebook using its API to your followers/friends! (Social Media everyone loves it and share latest updates!)

Context of Parallel programming:

Multi-core machines and think of utilizing their power.

Partioning, dividing single piece of work into multiple pieces to execute concurrently.

Staic partitioning, dynamic partitioning, thread pooling.

Model of .Net Parallel Programming:

Task Parallel Library – Task based programming consists of Task Class, Parallel Class, Parallel LIN!(PLINQ).

Architecture:

Example:

Serial Task:

for (int i, i <= 100, i++)

{

//Post Wall on Facebook

}

Asynchronous Task:

thread A = addressOf(SendPosts);

thread B = addressOf(SendPosts);

A.execute(); B.execute();

void SendPosts()

{

for (int i, i <= 100, i++)

{

lock (object);

PostIt(“happy friendship day!”);

unlock(object);

}

}

void PostIt (Account facebook)

{

//Post Wall on Facebook

}

Parallel Task:

System.Threading.Tasks.Parallel.For(0, 10, i =>

{

//Post Wall on Facebook

}

);

By now, you know how to swim in ‘llel programing, its time to deep dive into its ocean. Make sure you are ready with the following kits:

Get’ started – http://msdn.microsoft.com/en-us/concurrency/ee847320.aspx

‘llel programming in .Net 4 – http://msdn.microsoft.com/en-us/library/dd460693.aspx

Blog ‘llel programming – http://blogs.msdn.com/b/pfxteam/

Thread programming in ASP.Net – http://msdn.microsoft.com/en-us/magazine/cc163463.aspx

‘llel patterns – http://parallelpatterns.codeplex.com/

.Net 4.5 is coming with lot of improvements as hardware world is changing with multi-core architecture and soon you will not find the machines with single core and 2 core will soon out-dated.

Try it on vLabs from Microsoft – https://cmg.vlabcenter.com/default.aspx?moduleid=cc0bc14f-b222-4c3b-b29b-7f729df2af08

It did work for me:

short circuiting operators in .net

The short-circuiting operators are conditional versions of the logical operators. They conditionally evaluate the second expression as follows:

  • x AndAlso y will always evaluate x and will evaluate y only if x is true. (VB)
  • x OrElse y will always evaluate x and will evaluate y only if x is false. (VB)
  • x && y will always evaluate x and will evaluate y only if x is true. (C#)
  • x || y will always evaluate x and will evaluate y only if x is false. (C#)

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: