find a location for property in a new city

Tuesday, 11 January 2011

Optimising Entity Framework - Don’t query from base entity

Be careful to query the derived type when dealing with an entity data model that involves inheritance. You can massively reduce query complexity by making sure you avoid querying the base type of your model.

Say there is a model with a base type of Person and three derived types of Customer, StaffMember, Client. It doesn't matter if your inheritance model is Table per Inheritance or Table per Type (TPT) but say for example it is Table per Hierarchy (TPH) and the condition for Customer is that an OrdersID column is not null. Also say there is a quality that of all Customers that they will have a guaranteed value of PersonType == 1.

So what is the difference between:

context.People.Where(p => p.MessageType == 1) and context.People.OfType()?

Aside from the better code the difference in generated SQL is massive. For some reason Entity Framework will generate SQL that works out which derived type each of the rows in the Person table are even though I'm asking for just the base type. This means unnecessarily joining on a number of tables if you’re using TPT or evaluating many conditional columns if you’re using TPH.

I tried the different approaches on quite a large Entity Data Model of mine. There is one base type with 5 derived types and a few other entities being included (joined). Using the first query from above the generated query was 28224 characters long. The revised query was 14177. That's half the size!

Using query length is a poor way of evaluating a query because most of EF queries are explicit query lengths which don’t actually add to complexity but believe me – in the case the saving of query length was almost exclusively down to removing pointless complexity.

Follow britishdev on Twitter

Thursday, 6 January 2011

Enable assembly bind failure logging using Fusion

I got a FileNotFoundException with an error message of 'Could not load file or assembly 'LoveMoneyHttpModules' or one of its dependencies. The system cannot find the file specified.' This doesn't help much but does give some advice on enabling assembly bind failure logging.

The advice is to turn on the assembly bind failure logging but gives a tip of setting the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.

That's not very friendly advice so the question is how to do that exactly. I found a nicer way to do it than messing with the registry. First, you need to open the Fusion Log Viewer tool. You can do this by opening a Visual Studio command prompt and running fuslogvw.exe or alternatively just type Fusion Log Viewer into your Windows search bar thing.
  1. Open Fusion Log Viewer
  2. Click 'Settings...' and select the 'Log in exception text' radio button. Click 'OK'
  3. Do an IISreset.
The IIS isn't meant to be part of it I just found that it wasn't working until I tried that too. Anyway, after that I started getting logs of the attempts at binding in my yellow screen of death.

Follow britishdev on Twitter

Tuesday, 4 January 2011

How to POST to REST WCF web services with Fiddler

After making a set of REST web services with WCF I tested them by making requests with a browser. This is fine for GET requests however you will need some HTTP building tool such as Fiddler to test a POST, PUT or DELETE REST web service.

To begin with I have a WCF REST web service that intends for a Comment object to be POSTed:
[WebInvoke(UriTemplate = "comment", Method = "POST",
  RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
public Comment Comment(Comment comment)
{
    return comment;
}

The definition of Comment is simple:
[DataContract(Namespace = "")]
public class Comment
{
    [DataMember]
    public string Body { get; set; }
}

Note this namespace is very important as it will need to be specified in the XML sent in the request. Personally I set my DataContract with no namespace to save having to write it out in the XML every time.

Now open up Fiddler and lets get testing:

Create a POST to your REST service in Fiddler

  1. Go to the Request Builder tab in Fiddler.
  2. Set the verb to POST
  3. Set the URL to the one of your service
  4. Type "Content-Type: application/xml" into the Request Headers. (This is the bit that got be stumped for hours)
  5. Add your XML to the Request Body like <Comment><Body>colin</Body></Comment>

If you did give your DataContract a namespace other than blank as I did you will need to specify it in the XML of your Request Body. Such as: <Comment xmlns="MyNameSpace"><Body>colin</Body></Comment>

Happy testing!

Follow britishdev on Twitter

Thursday, 23 December 2010

3 things that changed ASP.NET development in 2010

As 2010 draws to a close I thought about how I was writing code last year. I realised how much development has changed in just one year. I want to look at how my work has evolved during 2010.

Entity Framework 4

I really like using Entity Framework. I wasn't quite sure about it in its first version, however, since the release of Entity Framework 4 in April I fully adopted it. It solves so many problems that I feel exist in modern web development.

I like stored procedures, I know a lot about using and optimising them but there is something seriously wrong with their concept - you have business logic in your database! When you think open mindedly about what a logical architecture should be you realise just how ridiculous that is. Entity Framework takes the last remaining strand of business logic back to the web developer.

Aside from the improvement in architecture, the other benefits I love about it is that speed you can develop with, deferred querying that improves reuse of queries, strong typing of Linq to Entities, Intellisense when writing queries, no more mapping, no more writing tedious POCOs, lack of tedious code in general.

Its hard to believe only a year ago I was writing stored procedures.

ASP.NET MVC 2

Again, I had a look into this when it was in its first version but was carried by the hype around v2. I fully adopted the use of MVC in April too and what a joy it has been to use.
A lot has changed over the last 10 years of web development such as increasing expectations of performance and web standards, popularity of unit tests and agile development, ubiquity of AJAX.

Now, I wouldn't go so far as to say there is anything wrong with ASP.NET Webforms I just feel that ASP.NET MVC has been a superb reaction to these changing trends in what web developers want to produce.

Whilst all of the above concerns can be answered by Webforms, like turning off view state to remove bloat, moving logic out of code behind to unit test, improved web standards in ASP.NET 4 etc you still feel as though you are swimming up stream some what.

I much prefer to get down and dirty with ASP.NET MVC. It's refreshing to get involved in HTML and HTTP again in a way that was abstracted out of your hands with web forms. It makes you feel like a real developer again, omnipotent! Of course the framework and templates do a lot of the tedious work for you but let's keep that quiet ay... Omnipotent! Remember that, yeah.

It's hard to think only a year ago I was using web forms.

Contribution

This has obviously been around for some time but I think only during 2010 has Microsoft's commitment to open source and code contribution has really shone through. It had been displayed, proven and truly adopted by the community. Look at how much there is to gain from MVC contrib.

I would like to think this is why frameworks such as Entity Framework and ASP.NET MVC are evolving so quickly and more importantly in the right direction. Microsoft is really listening to the community now. Forums and polls about what you would like to see in the next version of Entity Framework for example.

It is not just direct listening either, I'd be extremely surprised if Microsoft weren't reading blogs, browsing stackoverflow and stalking twitter to gain insight into what the community love and what they are crying out for.

It's hard to think last year I didn't have a blog or a twitter account or contribute on stackoverflow or publish any code.

Follow britishdev on Twitter

Wednesday, 15 December 2010

WCF Data Services for using Entity Framework over REST web services

I spent some time looking into WCF Data Services 4.0 (AKA ADO.NET Data Services & Astoria) as a way to expose our Entity Data Model (EDM) over an easy to create set of RESTful / OData web services. I decided to document my findings.

WCF Data Services 4.0 is service that provides a way of exposing an EDM over RESTful (OData) web services using WCF. It means that if you have an EDM already built, the work is essentially done. The EDM can be exposed using WCF Data Services so that it can be queried directly by the client.

How to begin

I successfully set up some WCF Data Services to expose a Comment part of my model. This helped me. I did find difficulty due to our use of POCOs instead of raw Entity Framework's generated code but I found a post that helped which told me about a weird hack that overrides the CreateDataSource into the Data Service:
public class CommentsService : DataService<SocialEntities>
{
    protected override CommentingEntities CreateDataSource()
    {
        var context = new CommentingEntities();
        var tracestring = context.CreateQuery<Comment>("CommentingEntities.Comments").ToTraceString();
        return context; 
    }
}

Consuming the web services

A client can now access the data by requesting URLs. E.g. http://api.mysite.com/CommentsService.svc/Comments to get a list of messages or http://api.mysite.com/CommentsService.svc/Comments(413L) to get the message with ID 413 (the L is for long since I use long instead of int)

The querying gets a lot more complicated than that too. See how to use your browser to query the data model

There are many SDKs out there to begin developing against your WCF Data Services from a number of popular platforms since it is simply using the OData protocol (e.g. JavaScript, PHP, Ruby, Java, Objective-C, .NET).

Restricting use

You can restrict the number of results to return when returning a list of entities to, say 5, so the client can't overload the server/bandwidth.

You can also restrict access to select parts of your EDM to calm your security nerves too:
public static void InitializeService(DataServiceConfiguration config)
{
    config.SetEntitySetAccessRule("Comments", EntitySetRights.AllRead);
}

Evaluating when to use WCF Data Services

So it does look like a great way to use the existing EDM to instantly have a set of RESTful web services to consume. However I think I would only use this if the developers of the web service client were have knowledge of the data and are expecting to get stuck into some code when consuming our data services.

If were internally designing our own Silverlight apps then this is great. However, expecting an external user to write sometimes complex queries to get to our data using all of our business rules (e.g. IsDeleted = false && SiteID = 21) is a little much to be expected I feel.

Conclusion

So great when providing RESTful web services to a handful of specific clients who have knowledge of your data and business logic. Not so great for making a friendly API for the masses based on a complex EDM as you cannot expect them to understand your business rules as well as you do.

I am not claiming authority on this topic. I am just discussing my findings so far in the hope that you may find this useful. If you have anything you think I have missed please feel free to comment.

Follow britishdev on Twitter

Thursday, 2 December 2010

What motivates developers?

It concerns itself with the surprising truth of motivating people who's job requires cognitive skill (I'm taking that to include developers). It is very different from what you might you expect and explains why Linux and other open source exists.



Interesting indeed. What do you think?

Follow britishdev on Twitter

Monday, 22 November 2010

Adding service reference gives the system cannot find the path specified error

I got an "The system cannot find the path specified." error box pop up when I tried to add a WCF web service reference to my web project. Problem with Service References?

Every time I tried to add the service reference to another WCF service I had set up in another web project I kept getting "The system cannot find the path specified." pop up in an alert box in Visual Studio 2010.

Solution

Somewhere along the line I had deleted the "Service References" folder from the project folder I was trying to add the reference to. It is simply solved by creating an new folder (in Windows Explorer) in the correct location called "Service References". Problem solved

Follow britishdev on Twitter

Thursday, 18 November 2010

How to host multiple secure sites in IIS

I have two sites: www.site1.com and www.site2.com. Both have pages that are served securely and so need a binding for https on port 443.

The problem is that only one site can be bound to the https protocol and if I disobey that rule I get strange behaviour such as requests for site1 actually serving the page from site1.

How to host multiple secure sites on your local IIS

First, you should know that sites resolve which site to bind to based on:
  • Host header e.g. local.www.site1.com
  • Port e.g. 443
  • IP adress

In this scenario I really just want to change the host headers of the two https bindings for the different sites but IIS won't let me. But I have ways of persuading it...

Step 1:
Navigate your way to C:\Windows\System32\inetsrv\config where you may or may not see applicationHost.config. This is a very mysterious file, it's there but not, sort of. I've found that it can be edited in NotePad (but nothing else(?))

Step 2:
Ctrl+F your way to "443:" and you should find your attempted 443 http bindings. In my case there are two and they both look like <binding protocol="https" bindingInformation="*:443:" /> (Oh yeah they are exactly the same, lets change that.

Step 3:
Change them to <binding protocol="https" bindingInformation="*:443:local.www.site1.com" /> and <binding protocol="https" bindingInformation="*:443:local.www.site2.com" /> respectively.

Job done

If you check in your IIS UI you will notice that you host headers have indeed been set and your sites should now be able to discriminate between the two.

Follow britishdev on Twitter

Static Membership helper class causing problems with MembershipProvider

I was having a problem where after a user changes their password using my custom MembershipProvider it was being updated in the database but didn't seem to be propagating to my application. So if they tried to log in they would be expected to provide their old password even though there was a new password in the database.

It is almost as if Membership didn't particularly care what has changed in the database and it kept on just doing its own thing.

Almost as if Membership is a static class... oh right it is

I thought the Membership was acting as if it was a singleton or something and never going through its constructor which sets a new UserService() to the UserService property (the service which ultimately gets the users' data). Maybe it is something to do with the fact that MEMBERSHIP IS A STATIC CLASS! Of course.

Membership was using my MembershipProvider but only ever instantiating it once due to its static nature so the UserService was keeping the old version even if the data had been edited. My fix was to make sure that the MembershipService property returned a new MembershipService() each time it is called.

So for context I changed this code in MyMembershipProvider:
public MyMembershipProvider()
{
    UserService = new UserService();
}

public UserService UserService { get; set; }

To this:
public MyMembershipProvider() {}

public UserService UserService
{
    get { return new UserService(); }
}

Follow britishdev on Twitter

Wednesday, 3 November 2010

Allow self closing tags in TinyMCE

I needed to make a tag self-close when using TinyMCE. It wouldn't allow it though, it kept opening them up again for me even after I edited them in HTML. How difficult can it be to add a tag to this allow self-closing tags list.

Answer: Not very. Even still though it took me a ridiculous amount of time to find out how to allow a new self closing tag. So I thought I'd share my discovery in case anyone else is struggling to find the answer.

I was trying to type in <source src="myVideo.m4v" /> but TinyMCE kept changing it to <source src="myVideo.m4v" ></source>.

Closed setting allows new self-closing tags

Turns out there is a little know closed setting you can set on tinyMCEs init method. I added my source tag to the list like so:
closed: /^(br|hr|input|meta|img|link|param|area|source)$/

Now any source tags are automatically self closing.

Notice I added a load more like img. The others are there by default in the core TinyMCE code so if I were to just put in source, the others (like img) would stop being self-closing. Not cool.

Follow britishdev on Twitter