December 2007 - Posts
Yet, another great feature in Visual Studio 2008.
JavaScript Inteliscence
This is wonderful, i can finally write JavaScript prototypes and see them in the Inteliscence.
It was so hard to write Object Oriented in VS2005 because of no Inteliscence support.
To see this in action, first, I've created a new JS file called JScript.js, within i created a new class called Test().
Then i created 2 new prototypes function under Test() called DoSomthing1() and DoSomthing2().
Then i created a new instance of Test(), and here you can see my methods under the Test class.
Even more, when i created a new ASPX file, and add a reference to my JScript.js file. I could see my Test() class and my test var with my created methods:
There's also support for local browser variables like document, window, XMLHttpRequest.
This is a major change for Web Developers !
LINQ is for queries, no doubt about it. Yet - we can do some interesting thins with LINQ, and not just queries. For example, we all deal sometimes with legacy code inherited from some old project - so here some interesting ways to handle this code with few simple lambda expressions.
For this example, I will use this Employee class:
public class Employee
{
public Employee(string firstName)
{
this._firstName = firstName;
}
private int _id;
private string _firstName;
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public int ID
{
get { return _id; }
set { _id = value; }
}
}
Creating Collections
Lets say we have an array of strings returned from database, represents the employees names:
string [] users = {"Gilad", "Maor", "Michal", "Shachar", "Gili"};
Now, we want to create an Array with an Employee class for each user in the "users" array.
Employee[] arrUsers = users.Select(e => new Employee(e)).ToArray();
How simple is that?
Lets go further, say we want to create a generic List<Employee> from the "users" array:
List<Employee> employees = users.Select(e => new Employee(e)).ToList();
We can also do the same on integers Array:
string [] arrNumbers = {"5", "3", "8", "10"};
int [] arrIntegers = arrNumbers.Select(i => int.Parse(i)).ToArray();
Sorting the integers array is simple as that:
int [] arrIntegers = arrNumbers.Select(i => int.Parse(i)).OrderBy(i => i).ToArray();
How about Casting?
Lets assume that we already have an ArrayList contains Employee objects in it.
public ArrayList GetEmployees()
{
ArrayList list = new ArrayList();
list.Add(new Employee("gilad 1"));
list.Add(new Employee("gilad 2"));
list.Add(null);
list.Add(new Object());
return list;
}
ArrayList arrEmployees = GetEmployees();
Now we want to create a generic List<Employee> from that Arraylist, we can do that easily with casting the objects in the ArrayList to Employee classes.
List<Employee> employees = arrEmployees.Cast<Employee>().ToList();
But, the problem here as you may notice, is that one of the objects in the ArrayList is null and another one is not even an Employee class.
In this case, we will get an InvalidCastException when we try to cast the NULL value to Employee class, and the same goes to the Object().
This can happen sometimes in legacy code, and the solution for that is simple:
List<Employee> employees = arrEmployees.OfType<Employee>().ToList();
The different between Cast<> and OfType<> is that Cast<> throws an InvalidCastException on every type that doesn't match specific casting type, while OfType will cast only if its the same casting type.
Conclusion
Yes, LINQ is for querying , but you can use it for more operation except that.
Work against the legacy code object is easier, more abstract and understandable.
Related Links
The LINQ Project
www.LINQDev.com
Introduction to LINQ
Pro LINQ
Usually when we developing a WCF service in Intranet environment, we need to enable users to authenticate on IIS with there login credentials - Integrated Security.
Assuming we using basicHttpBinding and we host the service library on IIS web site, This can be accomplished by the following settings:
<bindings>
<basicHttpBinding>
<binding name="basicHttpBindingConfig">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
Disable the "Allow Anonymous" login and enable the "Windows Authentication" login in IIS web site security settings tab.
Finally, don't forget to update the client service reference.
More about binding and security issues.
Recently I've started to read a book about LINQ.
I must say, LINQ language some how manage to amaze me each time i starting a new topic, it already changed the way I code.
I consider LINQ to be a "Must Know" language for every developer in every day coding.
This book is very light, understandable, and contains allot of good code examples with allot of explanations in it, step by step.
Later in the book, the language starts to be more complex and less understandable, because of some complex code samples.
Nonetheless, if you determine to understand the code, and read it couple of time again, finally you will understand the complex code.
It covers all the LINQ related topics:
- LINQ to Objects
- LINQ to XML
- LINQ to DataSet
- LINQ to SQL
You will also find some lambada expression with a good explanation about what the hack are they doing and way.
I must say, that I having allots of fun learning from this book, because the ease of the syntax and the language by the author Jr. C. Rattz, this book is speaking my language so i found it very educating.
I really recommend this book for developers how already start messing with LINQ and not for beginners developers.
How do we know which binding to use and when?
How do we know which security schema goes with our selected binding configuration?
There's allot of considerations with the binding configuration:
- Is there IIS involved.
- Are we going to use IIS Integrated Security.
- Are we going to use net.tcp binding?
There's also security issues we need to address:
- How we going to secure our services communication.
- Do we use the transport or the message layer.
| Setting | Features |
| Transport | Server Authentication Client Authentication Point-to-point security Interoperability Hardware acceleration High throughput Secure Firewall High-latency applications Re-encryption across multiple hops |
| Message | Server Authentication Client Authentication End-to-end security Interoperability Rich claims Federation Multi-factor authentication Custom tokens Notary/Timestamp service High-latency applications Persistence of Message signatures |
| TransportWithMessageCredential | Server Authentication Client Authentication Point-to-point security Interoperability Hardware acceleration High throughput Rich client claims Federation Multi-factor authentication Custom tokens Secure firewall High-latency applications Re-encryption across multiple hops |
The following page help us to decide witch binding configuration is the best for us due to our security demands.