AutoMapper

June 7th, 2011 No comments

AutoMapper is a great tool for mapping objects from a type to another. Their own description goes like this:

AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values.

I am new to the AutoMapper myself, have been using it for only few weeks now. Mainly just learning the syntax and the basics, but still it has been proving to be a very valuable. Basics are really easy, and it saves a lot of time. You can do quite complex mappings with it, and the simple stuff it almost handles automatically, best of all the mapping configuration is looking elegant and effective.

I provided a small example just to show you how to get stated. We have two classes that needs to be mapped, a entity class, Book and DTO class BookDTO, can’t get any simpler.

    public class Book
    {
        public int Author { get; set; }
        public string Title { get; set; }
        public string Plot { get; set; }
        public int Pages { get; set; }
    }

    public class BookDTO
    {
        public int Author { get; set; }
        public string Title { get; set; }
        public string Plot { get; set; }
        public int Pages { get; set; }
    }

They have the same properties so this will map very easily. This kind of situations you can just use the DynamicMap which AutoMapper is providing. Map the BookDTO to Book:

Book book = Mapper.DynamicMap<Book>(bookDto);

And the other way around:

BookDTO bookDto = Mapper.DynamicMap<BookDto>(book);

That’s how simple it is. AutoMapper is doing all the hard work for you.

Comments

December 2nd, 2010 No comments

From now on you have to register to comment. I have received so much adds in the comments, tired of reading and rejecting them. Hopefully it stops now.

Categories: Uncategorized Tags:

Web host from Arvixe

November 26th, 2010 No comments

Today we ordered the web host, with 50% discount. Everything went smoothly, and our account was up and running in no time. The whole process from starting to register to the notice that our account is ready to use took something like an hour.

I just tested that it works and installed WordPress there, with the necessary database and user. You can check out our blog from http://www.flamehead.org/blog. We will try to write there about what we are doing etc, so keep tuned in.

I was also thinking that maybe I should move my own blog there as well. Since we already have paid for the host, and we have unlimited disk space, traffic and databases. Also was thinking about getting my domain back, since I stopped paying for it when I was too lazy for writing anything on my blog. But now I did get at least some of my motivation back, hopefully it lasts longer than few posts. Actually doing something outside my normal work should give me more topics to write about.

Categories: Uncategorized Tags:

Company and web hosting

November 25th, 2010 No comments

Few days ago I got the feeling that I needed to learn something new. You know what I’m talking about, right? That motivation that drives you forward in programming. I hate when I’m not learning anything new, it’s frustrating.

I have chatted with my colleague from time to time about running our own company, side by side our current job. I’m a programmer and he is studying usability and graphics design, so we make a perfect team for developing web sites. The main idea is to learn new stuff and promote ourselves more.

We already have a name for our company, FlameHead. Now we need a good(cheap) host for our website and projects. Compared few options, first came across with BigDaddy. I have heard about them few times before, they are quite cheap and have European servers also. Since we live in Finland that would be nice. Chatted in irc with some guys that have used them and they did have some problems, they can’t run in full trust etc. Same guys mentioned about Arvixe web hosting company, first time I heard about this company. Trusting their word I thing we are going to take our host from them. They also have this “50% off for life” if you order on black Friday, which happens to be today ;) . The down side of Arvixe is that the servers are located in Texas, but the European users said that it’s still fast. I thought, what the heck it’s only 20€ for a year.

Categories: Uncategorized Tags:

C# Delegates and lists

February 26th, 2010 No comments

Delegates really help handling with collections. The examples are really simple here, but hopefully you get the idea. But enough with the chitchat, lets get on with it.

Instead of using the normal foreach sentence, you could call foreach straight from the list, and give it a delegate depending on what you want to do. You just pass the code block to the function.

            List<int> integerList = new List<int>(){1,2,3,4,5,6,7,8,9,10};
            integerList.ForEach(delegate(int number)
            {
                Console.WriteLine(number);
            });

Following example prints the integerList to the console. Now lets move forward to the searches. Basically they work the same way as the foreach, but they are more practical in my opinion. Here we search the list for item that is greater than 5. Notice that it returns just one integer, not a list. It returns the first match, so in this case 6.

            int result = integerList.Find(delegate(int number)
            {
                return number > 5;
            });

In this example we use the FindAll funtion to search all items that matches the criteria.

            List<int> resultList = integerList.FindAll(delegate(int number)
            {
                return number > 5;
            });

You can also just check if the list contains something, here we check if the list has number 3.

                        bool found = integerList.Exists(delegate(int number)
            {
                return number == 3;
            });

Delegates give you an easy way to remove items from list, which you cannot do with normal foreach syntax, because you cannot remove items from the list that you are iterating.


            integerList.RemoveAll(delegate(int number)
            {
                return number < 3;
            });

You can use the same principles with lists that contain any objects.

Categories: Uncategorized Tags: , ,

Nice way to open a wine bottle

December 22nd, 2009 No comments

Categories: Video Tags: ,

Testing WCF Services

September 16th, 2009 No comments

I was writing tests for my server/client software that uses Windows Communication Foundation.

For creating tests I had two options, create test to the server and client separately or create a whole new testing solution. I did go for the second option, because I need both solutions for testing the communication anyway. I added all projects from server and client to the testing solution, and started writing tests.

At first I needed to start the server side and test the connection with the client. You need to add them both to the app.config file, they should work nicely side by side, just remember to name the configurations, in case they already aren’t. Here’s an example of my configuration:

<system.serviceModel>
 <behaviors>
     <serviceBehaviors>
         <behavior name="MyServiceTypeBehaviors">
         <serviceMetadata httpGetEnabled="true" />
         <serviceDebug includeExceptionDetailInFaults="true" />
         </behavior>
     </serviceBehaviors>
 </behaviors>
 <bindings>
     <basicHttpBinding>
     <!-- Server-->
     <binding
         name="ws"
         transferMode="Streamed"
         messageEncoding="Mtom"
         maxReceivedMessageSize="10067108864"
         maxBufferSize="500000"
         maxBufferPoolSize="500000"
         receiveTimeout="10:00:00"
         sendTimeout="10:00:00"
         closeTimeout="10:00:00"
         openTimeout="10:00:00">
         <readerQuotas
              maxDepth="32"
              maxStringContentLength="2147483647"
              maxArrayLength="2147483647"
              maxBytesPerRead="4096"
              maxNameTableCharCount="16384" />
         <security mode="None">
             <transport clientCredentialType="None"/>
         </security>
      </binding>

      <!-- Server -->
      <binding
          name="FileTransferServicesBinding"
          transferMode="Streamed"
          messageEncoding="Mtom"
          maxReceivedMessageSize="10067108864"
          maxBufferSize="500000"
          maxBufferPoolSize="500000">
         <readerQuotas
             maxDepth="32"
             maxStringContentLength="655360"
             maxArrayLength="655360"
             maxBytesPerRead="4096"
             maxNameTableCharCount="16384" />
      </binding>
      </basicHttpBinding>
 </bindings>

 <!-- Server -->
 <services>
     <service
         behaviorConfiguration="MyServiceTypeBehaviors"
         name="Namespace.Namespace">
         <endpoint
             address=""
             binding="basicHttpBinding"
             bindingConfiguration="FileTransferServicesBinding"
             contract="Namespace.INamespaceSC" />
         <host>
             <baseAddresses>
                 <add baseAddress="http://localhost:8081/Namespace" />
             </baseAddresses>
         </host>
     <endpoint
         address="mex"
         binding="mexHttpBinding"
         contract="Namespace.INamespaceSC" />
 </service>

 <!-- Client -->
 <client>
     <endpoint
         address="http://localhost:8081/Namespace"
         binding="basicHttpBinding"
         bindingConfiguration="ws"
         contract="Namespace.INamespaceSC"
     />
 </client>
 </system.serviceModel>

After setting up the config, you’ll need to get the server running, here’s how you do it:

// Create
 ServiceHost server = new ServiceHost(typeof(NameSpace.ServerClass));

 // Start
 server.Open();

 // Test code here

 // Close
 server.Close();

Remember to close the host. If you leave it open and test again, it will fail, because it can’t open the address. I put the host creation to the constructor and in the tests there are a try/catch with finally block where I close the host, just to make sure that the host will be closed. Something like this:

 // Host has been created and opened at the constructor
 try
 {
     // Test here
 }
 finally
 {
     // Close the host here
     server.Close();
 }

I did call the server through my clients own functions. The server is also writing stuff on the local drive, so it’s easy check if both are valid.

Categories: .NET, Programming, Testing Tags: , ,

Science of motivation

August 26th, 2009 No comments

Excellent speech on TED by Dan Pink.

Categories: Uncategorized Tags: , , ,

8-bit trip

August 25th, 2009 No comments

Cool 8-bit trip video.

Categories: Uncategorized, Video, Web Tags: , , , ,

Google Chrome OS

July 8th, 2009 No comments

Few years back I heard rumour about Google developing their own operating system. Now it seems that it is true. Google has announced that they are developing their own operating system, Chrome OS. Which seems to be quite interesting. Read the blog entry here. I have great expectations, because Google’s products has been great so far.

Categories: Uncategorized Tags: ,