Wednesday, April 28, 2010

I recently started to working on new version of ASP.NET MVC 2. And the first thing I must to do is customizing the controller factory to using IoC for getting controller type. In the first stable version of ASP.NET MVC (1.0), I had done it, but in this version it had some thing changed. In special, when you inherited from the DefaultControllerFactory class it didn't have the RequestContext instance to the constructor. In the version 2, the constructor is have 2 parameters.

But why do you need customizing the Controller Factory? That because you need injecting some instances to the constructor of Controller. For example, you coded the CategoryFacade class and wanted to injecting into the CategoryController for getting some data from Database. this is a situation that you need customizing the Controller Factory.

Now, we started to implementing it. The first thing you need is inheriting the DefaultControllerFactory class:

In my case, I used Structure Map for IoC container. And in my Registry class, I need scanning the Web assembly as below:

Now, we must notify for ASP.NET engine for it can understand my custom Controller. How do I make it?. It's really easy, I implemented as below:

Don't confuse about it. At here, I only implement the Bootstrapper for my application. So don't care about it. We only concentrate on the ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory()). As you see I replaced the default Controller Factory with my custom Controller Factory.

Next step, we need registering all my configuration, so when the website started we shall have all instances to using. In this post, I implemented the Bootstrapper for booting the Website. So we only write the simple line of code as below in Global.asax:

As you see, we registered all Areas and continued to booting my website by the Bootstrapper. Just simple thing! Now saving all codes, and press F5 in Visual Studio's IDE. We shall possible injecting the Category Facade instance as here:

Finally, in order to writing this post, I referenced many articles from Internet. And this is my resources:

Tuesday, April 27, 2010

I really impressed with AutoMapper's Jimmy Bogard. If you aren't try it before, I advice you should try it now. It's so good for mapping objects (maybe entity to DTO). I still remembered I take many time for writing mapping objects in past. But now with AutoMapper all thing is simple. Now I'm trying to collecting all best practices that I learned from co-worker and Internet, trying to implementing the project (it's not publish yet). And when implemented the AutoMapper for my Application, I also get some problems from it. So finally, I also finished my work. Now I will show it for you.

In this post, I used Bootstrapper for booting my Application. I attend to writing a post about it later (comming soon :D). So I need when my Application run, I will have any thing I need. So I will implement IBootstrapperTask for it. Next, I used the StructureMap for scanning all assemblies implement IBootstrapperTask interface.

And there are some code as below:

+ Registry class (implement Registry class from Structure Map)

+ Profile class

+ AutoMapperConfigurator class

As you see, it's seen very easy (^_^). I'm many thanks to some articles that I read in Internet. In specially, I really referenced to 2 articles as below:

Friday, April 23, 2010

In Problem, I will describe about the problem that I got in my implementaion

In Design, I will show all method that I used in my implementation

Finally, I will show you my solution that I used for finishing my this article.

Now, I start with Problem section. In my solution, I built some entity and in the Facade layer I wanted to export my entity to UI layer, but in Design Principle, we shouldn't expose my entity to UI layer. We can figure out it same with the shell, with my Facade layer is a shell of application. And now, my problem appeared, if I try to expose the service with Expression Tree (maybe Expression<...<Entity>>) is a parametter. At here, I just want to expose the service with Expression<...<DTO>>. So, how do I do that? Maybe I must to convert the Expression Tree from my entity to my DTO (Data Transfer Object)

That is my problem, and now I will show you about my Design for solving it. The first thing, I can do for now is introduction about Expression Tree (ET) in .NET. Do you know about ET?

"In general, expression trees are special kind of binary trees. Binary tree is a tree in which all nodes contain zero, one or two children. Why expression trees have been implemented as binary trees? Mainly because binary trees allows you to quickly find what you are looking for. The upper limit of steps necessary to find required information in binary tree equals to log2N, where N denotes the number of all nodes in a tree."

Do you know the difference between Lambda Expression (LE) with the Expression Tree? All of people didn't know about it, so I will show you now.

LE is a wrapper of delegate function in .NET 2.0. And in the .NET 3.0 above, we wrap it with Func, Action, Predicate key word. So you can call the delegate function so easy and readable. Now we change to ET, as you saw in the definition above, did it make sense for you? ET is only a Data Structure contains the LE. You can figure out that you must transfer your object through the wire, and this is the place that ET participate in. Because ET is a Data Struture so when you transfered it to the UI layer, you must compile it into the LE and call LE to executing it. I hope you clear about this procedure. And the last thing I want to show you: "only using the ET when you want to transfer your object through the wire, maybe SOA architecture"

That definition about ET & LE is so far, I will turn back now. So what Solution will I use to make it working in my code? In my Application layer, I used AutoMapper for mapping my entity with my DTO, and all thing it worked normally. Only one thing, the ET wasn't converted.And this was a place that I must to working.

Tuesday, April 20, 2010

Today, when trying to unit testing for some code in Specification class, I just found that "It's very difficult to mock the Lambda Expression in code". But I also try mocking it in 2 hours and I mocked it perfectly (:D). And my work start now

+ First thing I have the class as:

As you see the red rectangle, we have the Lambda Expression like this Expression<...>>, so we must to mock it for unit testing (I used Rhino Mock for mocking object in my test fixture). And now I want to show you the UserRespository class as below:

Don't refuse about that, we only demonstrated about this function for my post. Don't care about other code in this function. We shall pass the Lambda Expression (LE) for executing in run time. So how do you mock this LE? It's not secret for anyone, and now I will show you about it

Pay attention to the red rectangle, I mocked the LE using Rhino Mock. As you see, it's very simple, we only supposed that the LE is not null and expected that if it's not null we shall return true for it. IMHO maybe have many methods for mocking the LE but anyway this is my working for it.