Month: October 2009

Book review: Beginning ASP.NET MVC 1.0

A dear friend of mine, Simone Chiaretta, ASP.NET MVP wrote an interesting book about ASP.NET MVC in collaboration with Keyvan Nayyeri.

image

The book is Beginning ASP.NET MVC 1.0, edited by Wrox, available on Amazon.com at this address for the price of 31.49 USD.

I am going to write a review of this book because, first of all, I know Simone and his professionalism. Second, because I am working now on an ASP.NET MVC project in my company, and this book was really helpful for me, so I am pretty sure that it will be the same for you guys.

Summary and structure of the book.

The book has 19 chapters plus a couple of appendixes. Each chapter covers a specific argument. At the end of each chapter there is always a section for the exercises and a summary section. The first thing you will like about this book is the huge amount of sample code!

At the end of the book there is the appendix B that contains all the solutions related to each exercise, so you won’t get lost.

The appendix A has a lot of useful links that you can follow, resources, links, books and more.

Finally there is the index that is done for a “dev” reader. It’s not a normal index but an index divided by alphabetic order that includes also all those components and “code” that you will find in the book. So you will find, for example, the page that talks about “NHibernate”.

Case studies.

What I really liked about this book is the 2 case studies that you will find at the end. Simone has created these 2 case studies that will reflect 2 real solutions, one is a blog and the second one is … a surprise. I am not going to tell you everything because you must buy it. Wink

This is something really well done as you will apply all the knowledge you got through the book into 2 real solutions!

Chapters.

Of course I am not going to describe each chapter, but this is the list of chapters so you can understand why this book is so well done, IMHO:

  1. MVC pattern
  2. WebForm VS MVC
  3. Getting started
  4. The model
  5. The controller
  6. The view
  7. Routing
  8. Unite test concepts
  9. Testing ASP.NET MVC
  10. Components
  11. Action filters
  12. AJAX
  13. Deployment
  14. Leveraging ASP.NET WebForm futures
  15. Authentication and authorization
  16. Extend ASP.NET MVC
  17. Migrating from ASP.NET
  18. Case studies
  19. Resources
  20. Exercises solutions

Final Considerations.

I am a NET developer, most of time I develop Silverlight/WPF or WinForm applications. For fun I did a couple of ASP.NET MVC applications in the bank where I was working before, but my knowledge about ASP.NET MVC was relative to those projects.

Now that I got this book, I was able to read all the aspect of MVC pattern applied to ASP.NET and I feel comfortable to build a real application with this framework.

I did a real ASP.NET MVC application for the Company where I am working now, and I was able to do everything is less than 1 week, including the use of JQuery and AJAX.

I am really satisfied of this book and I hope that you will buy it as it is a very nice and well written book that will drive you inside this pretty cool framework.

Thanks Simone and Keyyvan for your effort!

Tags:

Build enterprise application with WPF, WCF, Entity Framework and Prism. Tutorial 02.

Update: source code and documentation are now available on CodePlex at this address: http://prismtutorial.codeplex.com/

Build a generic repository with the Entity Framework.

In the previous series of articles about M-V-VM we used the AdventureWorks database, available on CodePlex.
After this article (configure a data layer with LinQ-to-SQL) I have got some reader’s emails asking me this and that. So, I am going to give you all those answers right now.

Download and configure the Adventure Works database.

On my dev PC I have SQL Server 2008 Standard version, but this tutorial is working also with the express or any other version.

  1. Go to CodePlex Adventure Works web site project and download the AW available version (light and full). I have downloaded the package called ”Adventure Works All Databases”.
  2. Go into the folder tree Tools/Samples and you will find the .mdf and .ldf and also the T-SQL script if you want to run it from a prompt command line.
  3. Open your SSMS and from the master database, install the version that you prefer. My samples work with both version, as I use always the Adventure Works LT (Light version).

image

  1. Mandatory: as I use Windows Authentication in all my samples, you must fix that in your local Db and change the connection string in my samples.

Open Visual Studio and create the folder three.

As we are going to work with an enterprise application, in order to simulate the 3 tiers, I have created 3 solution folders, one for the data service, one for the client part and one for the test. We will expand these folders during the building process.

The initial solution three will be:

imageSolution name: PrismTutorial
DataLayer: PrismTutorial.DataLayer
  TDD: Test.DataLayer
Consuming Service: PrismTutorial.WCF

Working with Entity Framework.

In the first series of tutorials, we used LinQ-to-SQL because:

  • It’s easier, It’s faster, It uses the relation 1 to 1 with the database tables and so on.

For this tutorial we are going to use the Entity Framework because this one is the real OR/M coming from Microsoft so in an enterprise application it’s better to spend more time and effort on building something stronger and reusable.

Open you VS data layer solution, right click and add an new component, ADO.NET Entity Data Model and configure it to reflect the Adventure Works LT database installed into your machine.

After that VS will show up a entity model designer window very similar to the one that we used with Linq-to-SQL.

imageIf you want to be compliant with my settings, this is how I have configured my Entity Framework:

image

It’s time to play, the Entity Framework repository wrap!

Like any software architect, I try to fit always everything in a more generic, reusable and more readable pattern. If we want to have a reusable UnitOfWork associated with a Repository engine, we must wrap the Entity Framework inside something more generics. Here comes the pain ….

The final result I would like to have is this one:

image

IRepository interface.

First of all we have to define a generic contract (interface) that we will use as our repository. As we want to be generic we need something like this:

At this point we have a generic implementation of the repository pattern and we do not need to worry about what we are going to save, update or delete.

BaseRepository to handle the dispose of the data context.

The second problem that we have to fix is more related to how the entity framework uses the context in a web application. I have found this article over the web: http://blog.zoolutions.se/post/2009/03/26/Generic-Repository-for-Entity-Framework.aspx, really interesting, so I am going to use the same type of solution.

We need a simple base class that is going to handle the data context and close it if not needed. We can also use the using clause in each method of our repository. I want to use this solution just because it’s more clean and doesn’t force me to write too much code inside the final repository.

Cool, we just need now to wrap all together and play with the data.

The concrete repository.

For the repository pattern there are two implementations, one is to keep everything generic and have 1 repository for the entire domain. The second one is to have a repository for each entity, like UserRepository, ProductRepository, and implement the CRUD operations related to the entity, like AddUser, AddProduct and so on.

I would suggest the second solution only if you need to include some business logic in your CRUD operations or there are some special development requirements. So let’s go for the easier and faster solution, he first one.

In the constructor of our repository we need to pass an instance of the UnitOfWork that in our case is the Entity Framework, like we do with the session with NHibernate.

Then we have also to implement the IDisposable interface in order to clean-up the connection after each call:

Now we can go ahead and implement each C.R.U.D. method and reflects the changes in the entity framework.

The save method is pretty straightforward, we add an object to our context, using it’s FullTypeName and we persist the changes in the database.

Same for the count methods except that we have to create an ObjectQuery that it’s nothing more than a translated query using the T object passed as a parameter.

The only part that you may not know is the parameter MergeOption used in the objectquery constructor. MergeOption has the following values (from the source of the NET Framework) :

Then we need to implement the delete command. Because we are working on a disconnected environment, first of all we need to get the original object from the repository. Then, we delete it.

Now the easy part, GetAll and GetAll using an expression for the criteria.

With the criteria:

Just a little note here. If you watch this piece of code:

.CreateQuery<T>(

                “[” + typeof(T).Name + “]”)
here I am using the CreateQuery method to get an ObjectQuery object using the generics. Unfortunately this method requires the name of the entity, so we use Reflection and get the name with the method typeof.

The GetSingle method is the same, we just retreive the FirstOrDefault result of our query:

And finally, the update method that is going to save the changes we did to an entity.

Like we did with the delete method, first of all we need to retrieve the original instance of the entity, then we simply apply the changes we got from the “working” entity, using the method ApplyPropertyChanges and finally we save everything.

Conclusions.

In this article we saw how to work with the entity framework and build a repository pattern around it. The next step will be to include business and value validation to our entities. We will use the enterprise library 4.1.

Stay tuned!

Tags: