Tag: dotnet

Using Autofac in a ASP.NET MVC 4 application

Posted by – October 18, 2012

ASP.NET MVC is a great framework, very flexible, but sometimes we need to organize a little better our business logic, there’s nothing better than have a business logic completely decoupled from controller, which can be easily reused on several controllers over the project.

To help ASP.NET MVC make use of reusable business logic and other logic components aswell, we can add a DI/IoC container to our project, after few days researching about IoC container alternatives, I choose Autofac to create my first web application that takes advantage of Dependency Injection.
More

Share

Fixing SelectedValue issue on DropbDownListFor with SelectList

Posted by – July 30, 2012

After spent several hours trying to figure out how the SelectedValue works on DropDownListFor, I finally figure out how to do it, you can also check my answer on stackoverflow here.

It’s very simple to get SelectList and SelectedValue working together, even if your property isn’t a simple object like a Int, String or a Double value.

Example:

Assuming our Region object is something like this:

1
2
3
4
public class Region {
     public Guid ID { get; set; }
     public string Name { get; set; }
}

More

Share

ByteBuffer implementation for dotNET

Posted by – August 2, 2009

Today I created a dotNET version of ByteBuffer class, with this class you can easily read and write data into byte array. You can get the source file here.

Here’s an example about how to use ByteBuffer class:

Program.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestCode
{
    class Program
    {
        static void Main(string[] args)
        {
            ByteBuffer buffer = new ByteBuffer(20);

            buffer.AddInt(10);
            buffer.AddString("Test");
            buffer.AddLong(10000);
            buffer.AddInt(350);
            buffer.AddChar('c');
            buffer.AddDouble(10000.325);

            Console.WriteLine(buffer.ReadInt());
            Console.WriteLine(buffer.ReadString());
            Console.WriteLine(buffer.ReadLong());
            Console.WriteLine(buffer.ReadInt());
            Console.WriteLine(buffer.ReadChar());
            Console.WriteLine(buffer.ReadDouble());

            Console.ReadLine();
        }
    }
}

My implementation doesn’t contains all features found on Java version of the same class, but can help on most cases.

Share

Working with Mongo Wire Protocol on C#

Posted by – August 2, 2009

Last saturday I started a small C# project when I try to send a message to MongoDB, after few hours working on it I finally could see the database receiving the message sucessfully.

Here’s the code that I used to send the message to database:

Program.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace Mongo.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            //Creating a socket connection to mongodb instance
            Socket socket = new Socket(AddressFamily.InterNetwork,
                  SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(Dns.GetHostAddresses("192.168.0.103"), 27017);

            //This is the message to be sent to mongodb instance
            List<byte> message = new List<byte>();
            message.AddRange(Encoding.UTF8.GetBytes("This is a test."));
            message.Add(0x00);

            //Each message to be sent to mongodb must have a header like
            //where we can specify which operation will be performed on server.
            List<byte> header = new List<byte>();
            header.AddRange(BitConverter.GetBytes(16 + message.Count));
            header.AddRange(BitConverter.GetBytes(1));
            header.AddRange(BitConverter.GetBytes(0));
            header.AddRange(BitConverter.GetBytes(1000));

            //Now we must put the message header a body together before send it
            // to the server.
            List<ArraySegment<byte>> buffer = new List<ArraySegment<byte>>();
            buffer.Add(new ArraySegment<byte>(header.ToArray()));
            buffer.Add(new ArraySegment<byte>(message.ToArray()));

            //Send the message to the server
            socket.Send(buffer);

            //Close connection
            socket.Close();
        }
    }

A very good documentation about how to write a MongoDB driver can be found here.

Share

ASP.NET Basics with Visual Studio Web Developer 2008

Posted by – January 8, 2009

ASP.NET is one of the key technologies behind .NET Framework, you can easily create web applications to collect data entered by user by placing some input controls in your page, once the data is entered and the page is submited, you can read data on server side by accessing specific properties in your controls.

This article will give an idea about how to create a simple ASP.NET project, it will have a page that contains some textboxes and a button, this button will have an event attached to it, we will add some code in this event to store user entered data into an SQLite database using its ADO.NET provider that can be downloaded here.

I’m assuming that you already have installed Visual Studio Web Developer Express 2008 in your computer, if not, you must download both from this site.

Now you must start Visual Studio Web Developer Express 2008 and then go to File->New Web Site… menu like we have in the image below:

new_project_menu
More

Share