Saturday, November 29, 2008

Generic DAO with LINQ to SQL

One of the first thing I try to implement when learning a new programming language is a Generic Data Access Layer. I hate to write repetitive code and always try to find ways of working around this. I managed to implement this in Java and PHP and had no doubt that .NET will allow me to do so. After a good bunch of reading on the net and playing around with the tutorials for a while now, I managed to implement Generic DAL by using LINQ to SQL.  Here is what I did:

[ Step 1 ]:

I created a seperate Class Library to Generate all my Business Objects and handle all my Data Access Objects (DAO). In this Class Library I created a a LINQ to SQL Data Context and named it UTSDataContext.

[ Step 2 ]:

I then created a Generic class which had two Parameterized Type variables one being the class representing a Business Object and the other being the Primary Key type.

public class GenericDAO<E, K> where E : class

[ Step 3 ]:

I then added all methods that will carry out CRUD tasks. The code for the Generic DAO is as follows:

public class GenericDAO<E, K> where E : class
    {
        private UTSDataContext context = new UTSDataContext();

        public UTSDataContext Context
        {
            get { return context; }
            set { context = value; }
        }

        public virtual void save(E instance)
        {

            this.save();
        }

        public virtual void save()
        {
            context.SubmitChanges();
        }

        public virtual List<E> selectAll()
        {
            return context.GetTable<E>().ToList();
        }

        public virtual void insert(E instance)
        {

            context.GetTable<E>().InsertOnSubmit(instance);
            context.SubmitChanges();
        }

        public virtual void remove(E instance)
        {
            context.GetTable<E>().DeleteOnSubmit(instance);
            context.SubmitChanges();
        }

        public String getKeyProperty()
        {
            String primaryKeyFieldName = typeof(E).Name + "Id";
            return primaryKeyFieldName;
        }

        public Expression<Func<E, bool>> getLambaKey(K id)
        {

            var parameter = Expression.Parameter(typeof(E), "e");
            var propId = Expression.Property(parameter, typeof(E).GetProperty(getKeyProperty()));

            return Expression.Lambda<Func<E, bool>>(Expression.Equal(propId, Expression.Constant(id)), parameter);
        }

        public E findById(K id)
        {
            return context.GetTable<E>().Single(getLambaKey(id));
        }
    }
}

Limitations

(1) I am not in REFLECTIONS yet in order to identify primary key property field through annotations. As such, all my primary key fields are named as follows:

Exact Table Name +"Id"

For Example, the table Department will have DepartmentId as primary key Field.

 

(2) There are some reasons why an Object will no longer reside in LINQ Context and as such not be monitored for Changes. The most frequent one being Deserialisation. For example, if you stored an Object in session and retrieved it later on, you will have to fetch the object again from database (by using its Primary Key Identifier perhaps) and then write the changes to it.

Tuesday, November 25, 2008

ASP.NET Security, Authentication and Authorization

Security is one of the major features in a web application. A good way of providing reliable security in your web application is to use a tried and tested solution which follows known standards. ASP.NET's security mechanism is outstandingly beautiful. As a side note here, I have  good experience with Java Authentication and Authorization Service and the brillant Seam Security Implementation which uses underlying Java Security mechanisms.

ASP.NET though, provides the simplest way of implementing security in a web application. Authentication and Authorization has all been taken care of with even the possibility of customization. To demonstrate this, here are the various steps I have been carrying out:

(1) Configure your web application to use Membership and Role Management by adding the following in your web.config

<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
  <providers>
    <clear />
    <add
      name="SqlProvider"
      type="System.Web.Security.SqlMembershipProvider"
      connectionStringName="SimpleSqlConnection"
      applicationName="MyApplication"
      enablePasswordRetrieval="false"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="true"
      requiresUniqueEmail="true"
      passwordFormat="Hashed" />
  </providers>
</membership>

<roleManager enabled="true" defaultProvider="CustomizedRoleProvider">
  <providers>
    <add name="CustomizedRoleProvider"
         type="System.Web.Security.SqlRoleProvider"
         connectionStringName="SimpleSqlConnection" />
  </providers>
</roleManager>

(2) Make sure that the connection string you specify here exists in your connection string settings in the web.config file

(3) Now using the Visual Studio Command Prompt type aspnet_regsql.exe to launch the ASP.NET SQL SERVER SETUP Wizard and configure your database with the required tables for security

(4) Once this is done, in Visual Studio IDE, launch the ASP.NET WEB SITE ADMINSTRATION TOOL (Click on Project -> ASP.NET Configuration )

(5) Click on the Security Tab and use the Security Wizard to create roles, users along with roles, and access rules

(6) ASP.NET comes with a variety of pre build login controls for Login, Authenticated User Information display and Logout

For Login Control, the code is as follows:

<div>
        <asp:Login DisplayRememberMe="false" ID="LoginControl" runat="server"/>
    </div>

For User Information Display

<asp:LoginView ID="LoginViewControl" runat="server" Visible="true">
    <AnonymousTemplate>
        <asp:HyperLink NavigateUrl="~/general/login.aspx" Text="Please Login"/>
    </AnonymousTemplate>
    <LoggedInTemplate>
        <table>
            <tr>
                <td>
                   Welcome <asp:Label ID="LoginUserName" runat="server"/>
                </td>
            </tr>
            <tr>
                <td>
                    You are logged in as <asp:Label ID="LoginRoles" runat="server"/>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:LoginStatus ID="LoginStatusControl" runat="server" />
                </td>
            </tr>
        </table>
    </LoggedInTemplate>
</asp:LoginView>

Hope its also a cruise for you.

Friday, November 21, 2008

Learning LINQ to Entities as compared to Hibernate

It took me hardly two days to implement a Generic Data Access Layer on .NET 3.5 SP1 by using LINQ to Entities. The first time I worked on an ORM solution (Hibernate) was on a project for the Danish Commerce and Companies Agency around two and a half years before. It took us around one week to set up Hibernate and even after the system went into production we had problems with performance and memory leaks which were later fixed by the folks at hibernate. Of course, hibernate is open source and sure it is much more powerful but still is has its lots of problems. Earlier this year, together with my team at M-ITC LTD, we built an in house ORM solution (TinyORM) by using annotations and reflection APIs. We wanted something simple without any persistence and usage of traditional SQL. We further extended this solution to provide support for Oracle PLSQL for the needs of our client. Performance wise, we were far better than Hibernate and it was superb experience. Object Relational Mapping has been in software development for a while now. Every language I have been into now has such implementations. However, LINQ to Entities (DLINQ as it was called in the past) and Entity Framework are to me the most promising. Coupled with Dynamic Data Framework, they can be of great effect in enterprise applications.

Until now my .NET learning has been going on all so good. I am really impressed with the speed at which I can develop features in my application as compared to JAVA. I still feel that JAVA is much more powerful though. Perhaps, its all down me having much more experience in JAVA. Hopefully, one year from now, I will unleash its full potential.

Tuesday, November 18, 2008

SOFTWARETEAMS now in Quatres Bornes

Since October, our company has been operating under a new brand. SOFTWARETEAMS now located in Quatres Bornes and together with new partners is now well in place and hopefully our major projects will kick off soon. I have been busy for the past month or so in the set up of our new infrastructures both here and in Denmark. University assignments and class tests have also been taking a lot of my precious time. Now that all of that are behind me I will be able to post some of my findings here.

Throughout the past month, I have mainly been focusing on some research into performance issues with Entity Framework. Having worked with Hibernate for the past two years, the amount of problems I have had with this framework with regards to concurrency, performance and other bottlenecks have only been discouraging. To workaround these problems, I have even developed, with the help of my team of course, an in-house ORM solution (TinyORM) for the needs of the CBS Housing Project. I don't want to repeat the same mistakes and throughout the various reviews and comments I have been looking, most of the conclusion sees to indicate that Entity Framework though promising is not there yet ! Instead, they recommend using LINQ to SQL Classes. So here I am now trying to implement a Generic Data Access Layer with LINQ SQL. My next post will discuss the architecture and implementation issues. Stay tuned !

 

Saturday, October 4, 2008

.NET 3.5 Dynamic Data

Since my last post, I had a look at Dynamic Data Framework from Microsoft. Though not the final release, it is yet the best CRUD framework that I could find in any languages I have tried since now. Best of all, the learning curve and time it took me to implement it was simply outstanding. Though I have not tried some in depth customization and validation is not present in this framework, it worked like a charm. I will post some more details into how i implement CRUD for all my business models later.

Stay tuned .

Monday, September 29, 2008

CRUD with ASP.NET

For the past week, I have been looking at ways of building CRUD interfaces in .NET. I wanted something that would couple with Entity Framework and automatically generate grid views and insert / edit interfaces. This of course done by reading metadata from the Entity Model code which could be customized by the developer.

Enters, Dynamic Data Framework for ASP.NET. It looks promising and flexible enough. This week will be dedicated into implementing Dynamic Data into an existing ASP.NET Web Site, customizing templates and modifying templates for individual tables.

Wednesday, September 24, 2008

Recover Deleted Linux files and Folders

Today, among all the load I had on my head, someone from my team happened to delete the data folder for oracle database server on one of our remote rack servers. I still remember all the pain I had setting oracle on this 64-bit Fedora 6 Core machine, with kernel incompatibilities and missing libraries. All in all, it took me two weeks to set it up correctly using the GUI from oracle install with an uprate of 1 mbps.

So in order to avoid doing it all over again, i decided to have a look at how i could restore deleted files and folders under linux. I knew that linux stored deleted files in trash when using GUI (Gnome or KDE) but through command line, it seems that the file was not recoverable using ext3 partitions other than looking at blocks and inodes one by one.

After loads of despair, i found out that PhotoRec was one of the best tool that was able to solve such problems. So here i go with this tool, trying to recover all my oracle database files and hoping that it will all be sorted out :(. With 24hours estimated time for completion, I really hope all my files will be restored. God bless !

Tuesday, September 23, 2008

Windows Vista DHCP Problems

I have been having problems recently with DHCP on Windows Vista. It seems that Vista is not able to obtain an ip address through DHCP when changing networks. I noticed that on linux, DHCP worked like a charm on the same machine (did not surprise me :P).

A quick look on the net and I found at that Microsoft brought in a change into the way an ip address was queried for using DHCP. I wonder whether they got to know about such a thing called Backward Compatibility !!

Anyway, have a look at this site and download the useful tool mentioned there to get a quick fix.

http://www.reviewingit.com/index.php/content/view/29/2/

Friday, September 19, 2008

Starting with Entity Framework and LINQ

 

So, here I am starting to try out LINQ and Entity Framework. I have been using Hibernate for the past two years can say that there has been many hurdles throughout the learning process of this process of this open source ORM tool. However, Hibernate was a first glance at the power of modern ORMS. I expect the same robustness from LINQ.

After about a week of research (1 hour daily :P) and some patches (download and installation) relevant to VS 2008, .Net Framework 3.5, ADO .NET Entity Framework, I finally managed to have a proper platform to start with LINQ. My aim is to use something similar to Hibernate in C#.

I first created my tables (models) in SQL Server 2005 Express and added some values to it.

imageimage 

 

A console application was sufficient to demo Entity Framework. Note that in order to have Entity Framework Tools in VS 2008, I had to download a few stuffs including patches. Instructions can be found at http://oakleafblog.blogspot.com/2007/12/entity-framework-beta-3-available-for.html.

In my VS2008 console Application, I added an ADO.NET Entity Data Model named EntityFrameworkDemoModel.edmx. A wizard was presented to me and I chose to Generate my models from Database. The next screen allowed me to choose my Datasource and store my Connection String in my App.config

 

image

 

In the next screen, I chose the models I wanted to generate:

image

When finished, the following was presented to me :

image

The models was automatically generated in a class. As opposed to hibernate, all models were regrouped in one single class. Now to test, whether using LINQ, I could obtain all data in my tables through my Business Objects which have been generated, I ran the code below:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Objects;
using System.Text;

namespace EntityFrameworkDemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("ADO .NET Entity Framework Demo");

            DemoDBEntities demoDb = new DemoDBEntities();
            ObjectQuery<UserRole> userRoleQuery = demoDb.CreateQuery<UserRole>("SELECT VALUE u FROM UserRole as u");

            foreach (UserRole userRole in userRoleQuery.Execute(MergeOption.NoTracking))
            {
                Console.WriteLine("User Role is " + userRole.UserRoleName);
            }
            Console.ReadLine();
        }
    }
}

 

And my result is as follows:

image 

 

I must say, its not bad at all for now. My next step will attempt building (if it does not exist already) a generic class that will handle CRUD operations for all class.

Wednesday, September 17, 2008

.NET at a Glance

Over the past two and a half year (add ONE more year if university is accounted for :P ), I have been working strictly on JAVA and Linux platforms. There has always been some sort of animosity between our open source JAVA & LINUX teams and our Microsoft slaves (.NET Team). However, one of my workmate who has had shots at both JAVA and .NET kept insisting at how easy it was to do things in .NET rather than in JAVA. Throughout the past 6 months or so, I have been researching at what would be the next best language for me to learn. I had a dig around a year or so at PHP and it really has been a cruise. But PHP for me is meant to develop simple, lightweight applications. While some may argue that PHP is being used for Large Scale Enterprise applications, I do believe that its not where its got its strongholds.

So here I am finally embarking on my Final Year Project for my Post Graduation at the University of Technology of Mauritius. My choice for Final Year Project came to development of a University Timetabling and Scheduling software that would be used by UTM. This as a recognizance of what the UTM gave me as a student and also as a 99% guarantee that my software would be used in a production environment.

I have already set up the .NET Framework 3.5, with Visual Studio 2008 (along with SP1 ) and am now digging deep in "Beginning ASP.NET 3.5 in C# 2008 - From Novice to Professional". Hopefully, my quest for .NET insights will be satisfied.

My First Blogger Post

 

I recently register for a blogger account in order to post all my research and findings relevant to MSC Final Year Project. So here ya goes with my very First Post (not one to remember though :P ). I hope that this blog helps me and others as well.