Wednesday, November 07, 2012

Extend ASP.NET Entity Framework by Partial Classes

ASP.NET Entity Framework 4 can help link up entity objects to existing database resources. Let's say you have your database tables already setup and ready to use. On Visual Studio 2010, you can import databases tables into its Entity Model. Every column in the data table can be wrapped inside a class as a property. When you open up the model's designer cs class after the import, you'll the source code like this:
#region Entities
#    
[EdmEntityTypeAttribute(NamespaceName="Model", Name="table1")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class table1 : EntityObject
{
    public int Id {get; set;}
    public string Name {get; set;}
    //...
}
But how would we extend this entity if we want to add new property to support this, the answer is using Partial Class:
#
public partial class ExistingClass {

    public string NewProperty {get; set;}
    //...
}
With the keyword "partial", you can extend the original class at any other places within the project. All those partial classes would just be compiled with the original ones so the class would support all those properties you mentioned in the partial classes.

So what if you want to encrypt a property within a usual entity class so it will be encrypted when being saved to the database? Let's create a partial class for this:

As you may notice the name of partial class must match the name of the original class.
#
public partial class table1 {

    //Let's say Name property will contain encrypted text

    //Open property to the public with no knowing of any encryption/decryption done on the fly
    public string ClearName{
        get{
            return decrypt(this.Name); //Decryption works here
        }
        set{
            this.Name = encrypt(value); //This is where encryption takes place
        }
    }
}
#
The combination of private variable and public property can provide transparent encryption/decryption feature to the property in the original class. The entity object table1 can finally be saved to the data table "table1" with encrypted text in the column "Name" 

The only downside is that LINQ does not support this additional property like "ClearName" which is not possible to be translated from its complex data type to the primitive data type while generating valid SQL statements inside its gears. You'll see error messages whenever you involve this property in LINQ's conditional search statement, like:

#
var tables1_entities = Entities.table1.Where(p => p.ClearName == "Julia");

Obviously, "ClearName" property does not exist in the data table. It's just a property in the .NET partial class.

To get things work again, you need to isolate the additional property out of LINQ statement:

#
//Encrypt the search text first and put it into a variable
var SearchCondition = Encrypt("Julia"); 
//Search it by using encrypted text instead
var tables1_entities = Entities.table1.Where(p => p.Name == SearchCondition); 
#
Of course, for encryption, the search text must be exactly the same as the content in the data table. Therefore, it's not recommend to search encrypted text this way. You would search by using other clear text columns in the same data table to avoid any conflict like this.








Wednesday, September 26, 2012

IIS 7 default URL rewrite

IIS 7 provides Cookieless feature by default for ASP.NET applications while it may break MVC4 controller to certain extent like editing or creating record based on the model and database connection provided.

This article http://msdn.microsoft.com/en-us/library/aa479315.aspx explains what additional string means in the URL returned by IIS.

URL patterns in trouble would be like these:
http://server_URL/A(XXXXX)/...
http://server_URL/S(XXXXX)/...
http://server_URL/F(XXXXX)/...

To check IIS configurations, please refer to the following article:
http://technet.microsoft.com/en-us/library/cc754725(v=ws.10).aspx



Friday, September 14, 2012

How to recover MySQL Website Configuration Tool Icon in VS 2010

Visual Studio 2010 has been updated to SP1 but a weird symptom happened while I came back and revisited my favourite icon of MySQL Website Configuration Tool over the Solution Explorer. It was just missing!

After a series of upgrade and re-installation of MySQL .NET Connector package via URL:
http://dev.mysql.com/downloads/connector/net/

The icon is still missing and someone might think it would reappear on creating New Web Site template. However, that's not the case for me.

Before I reinstalled the whole thing for VS2010, I got my last hope to see if any miracle could happen.

The fix I have done is just as follows:



Click on the the solution in the Solution Explorer.




















In the Properties Window, click "Property Pages" icon.














A project properties template is shown up in the content window.









Try a couple of clicks on the items and then save it and close it.

See if that MySQL Website Configuration Tool icon reappear again under Solution Explorer toolbar?














Sunday, February 26, 2006

MS Atlas: The beginning of AJAX like technology on .NET platform

As a java programmer for the last several years, I've returned to the university for my research and study. The first job I've got here is the research project of Microsoft Atlas: an AJAX like technology. Perhaps it's better than AJAX as I saw the comments from the tech news.
Actually, you may not be surprised that it has many features resembling AJAX, however, implemented on .NET framework. It's all about Javascript: a client-side scripting technology. Some specialists may wonder if we should depend heavily on this kind of stuff while considering the security threat to the client-side technology.
The legal issue may arise on whether it is the responsibility of customers using client-side scripting technology while a fraud may occur during transactions. What we concern about Ms Atlas is its authenticity and confidentiality in the implementation of the E-Commerce Application. You may prefer security rather than richer client interactivity.
Atlas provides library in .NET framework to cooperate well with ASP.NET so as to provide a more interactive web user interface. At the moment, Altas is still in experimental stage so you may have to download new release to catch up with the progress in specific period of time.
What I could see is Google provides the best example of using AJAX to facilitate its webmail client interface. Also, Blogger here is another good example to demonstrate AJAX usage.