Recently in one of my project, I used T4 Template for quick code generation and it turns out to be a really helpful tool for accomplishing repetitive coding tasks, this inspires me to write this blog.
Challenge
Many times in various parts of code development cycle we require similar piece of code with a little variance. It may be UI related code or business logic classes (Manager Classes) or Entity classes or could be stored procedures. I always felt that, this kind of code can be auto generated with the help of some tools instead manual efforts.
In my project I wanted to auto generate all the “Admin Web Site” pages which includes seed data editing as these may have similar kind of UI & functionality, like showing all the records into an Ajax grid and allowing them to edit on popup window etc.
Candidate Solutions
There are number of third party tools available in the market today which are capable of generating reusable code. The only limitation I see is they have their predefined architecture and code generation strategies. Fitting auto generated code from different architecture to current application architecture is always a frustrating task at least for me.
Then another option I thought of is Code Dom, which comes along with .net framework itself especially for code generation purposes. When I explored this option in much detail, I realized that it requires huge learning curve which mainly includes writing code into “Intermediate Language” (IL). Of course it has many pros, like it can generate code for multiple languages from a single source template, but this is not what I want. I want very simple and easy to implement solution for generating similar kind of reusable code.
Now here comes Entity Framework code generation approach which uses T4 templates to generate all the source code files. When I dig more into it I found that it is pretty easier to implement this instead of “Code Dome” because it uses simple C# syntaxes for template writing. I was sure that T4 approach will serve the purpose so I decided to opt for it.
A brief Introduction about T4 Template
In Visual Studio, a T4 text template is a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic. The generated file can be text of any kind, such as a Web page, or a resource file, or program source code in any language. You can find more detail in following link:
http://msdn.microsoft.com/en-us/library/bb126445.aspx.
My Approach
First I wrote one complete functional web page which I wanted to replicate for rest of web pages. Then I created two separate T4 templates, one for “ASPX designer file” and other for “Code behind file”. After this I copied previously developed Webpage into respective templates. Then I replaced hard coded entity names and column names with dynamic column names by using reflection. I also add logic to generate UI and data operations based on columns data types.
We are pretty much done here, now the only thing left which is to change entity name in the template and complete code will be automatically generated in minutes. Every time you need to generate code for different entities just replace the entity name in the template file.
Below is the snap of my ASPX file generation T4 template.

Below is the snap of my code behind file generation T4 template.

Challenge
Recently I started working on a new project, with a few other team members. This project involves a really good amount of ERD, means there are a bunch of entities involved in the Database, with a lot of relationships in-between.
I am personally very fond of working with ORMs and familiar with Entity Framework 4.1 and nHibernate which was an obvious choice here but other people involved in this project suggested not to use this as they had some bad experiences with “Entity Framework”.
I guess you might have come across same situation like this. So I thought to write down my own ORM using T4, Which was absolutely not the right choice as per the tight deadlines.
So I thought of another solution which involves writing a base manager class which will serve the same purpose.
Features
- Supports stored procedures
- Enables use of “BaseEntity” class which is missing in Entity Framework Database first Approach
- Ability to work with business entities without manually filling business objects from data tables
- Single function is able to fill all the Complex/Navigation properties of a particular entity, up to N Level whether it is “One to One”, “One to Many” or “Many to Many” relationship. And same behavior is required in the case of saving the data
- Ability to perform basic data operations without writing repetitive code
- Ability to use eager loading as well as lazy loading
Solution
- Create all the tables in database along with their relationship. Make sure every table has a column “ID” in it as a primary Key
- Create Stored Procs for all the data manipulation tasks. Make sure that the entire stored procs name will have a special naming format “prefix + Table” Name like “Get+Table Name” and “Save+Table Name”
- Create a simple DAL, using plain ADO.NET, that has the ability to execute stored procedures, and return DataTable objects
- Create a base entity class for common properties in all the tables
- Create a class for every entity of database. Make sure that all its properties of entity matches with all the columns of a certain Stored Procs result set. Also all the entity must inherit from the base entity class
- Include “Base Manager” class which has basic data interaction methods. You need to inherit all manager classes from this base manager class. Now you can work with all the business data as objects and not Datatables
Here is a snap of my base manager class.

Here is how fast and easy to create your manager class. No method required for basic database operations.

If you have complex Entity which has a complex or Navigation properties with different kind of relationship in between, You can specify relationship by “ChildEntityInfo” object. In this case Manager class would look like below code.

Now you can use manager class as a normal manager class. Like below

Conclusion
This obviously isn't the best solution to this case, and obviously isn't something ground-breaking either. I just tried a simple solution that helps a lot when it comes to trying to deal with such circumstances. Hope this will be helpful for others as well.
Quality is simply an obsession. An obsession to deliver nothing but the best. Obsessively and compulsively engaged in delivering what is required without deviating even a small pixel.
Only with this spirit will one see great results, great products, great success.
If you see software development as a cost center, you need to rethink that entire department. If it is technically a cost center, then you are not in the business of software. That is when you need to seriously think about outsourcing it to someone who thinks of it as a profit center. Those are the folks who would treat their team well. And that would result in great solutions and great software for your company.
Writing software is hard. It is not trivial and there will be no magic formula anytime soon that would create great software with just a click of a button.
One needs to realize that when writing software, bugs are always a part of the package. There just cannot be a chunk of software that is written that would be totally bug free. I am not saying that one should ship with bugs. NO!
What is important is the management of these bugs.
If developers and indeed the quality folks can find, manage and then fix those bugs, the end result will be great software exceeding expectations of everyone involved.
Managing the bugs involves first acknowledging that there will be bugs. Most developers refuse to believe that their software can ever have bugs. Let that go. Every piece of code ever written has bugs. Accept it.
Then there needs to be concerted effort to find those bugs. First at the developer level, what some would say unit testing. But going beyond that, what stops the developer from running full blown test cases that apply to her code? Run them, and the unit tests and whatever other tests you can run. Don't leave that to the QA guys. After all you are proud of your code, you do not want them to think your code is a piece of junk. Give them your best and save yourself the blushes later on.
Then of course you fix those bugs. That is obvious.
But what is not obvious is that you have to test all over again. Assuming that you have fixed everything and it will run fine after you have fixed a bug is hazardous. You are just undoing all the hard work that you have put in.
Once you ship to QA, don't disown the code. Be in touch with them and find out what is going on with the testing, in particular your own stuff, but also with the entire software that your code is integrating into. You can catch things they will not catch.
Ask the testing folks to break your code. Then take pride in the fact that they were not able to.
Another aspect of bug management is tracking your bugs. Make sure you understand the bugs that are assigned to you, you fix them, understand the fix and then track them right back to the QA guys so that you know what is going on with your bug.
Last, but not the least important is something that is really always overlooked. Expectation management. Managing expectations of the quality team, and the user. If you can tell them before hand that there is a certain issue with the software, there is a lot of time, energy and heartbreak that you can save.
Telling the QA folks that there is a certain or to expect a non conforming behavior is a sure shot way of making friends out of your sworn enemies. They will be eternally grateful to you for not wasting their time.
Telling your customers up front about the issues will delight them. Not antagonize them. Be brave, honest and up front.
Know that you cannot kill all your bugs. Manage them well and you can sleep well.
When you are looking to outsource anything, not just software development or maintenance, anything at all, you are not looking to get the best technology solution. That is a given. That is by default. That is a requirement. Without that there is no outsourcing happening.
What you are looking beyond that is a solid team that shares your passion and zeal. Somone who can stand right next to you in turbulent times. A bunch of people who share your vision, are willing to work towards fulfilling your vision. You are looking for someone who can help you look good in front of your customers, management etc.
IT Application outsourcing is not just head count replacement by cheaper alternative (offshore, outsource, whatever). It has to be more strategic than that. The skin in the game needs to be there for a complex outsourcing deal to work out.
Delivering complex enterprise solutions require a deep understanding of not only the technology but also the business, the impact (both positive and negative) and long term ROI.
Considering app development outsourcing as a simple transaction would be foolish.
Performance is a major concern in every application. Here, I am describing a crucial feature of SQL by which we can improve the performance of our applications that offer search functionalities.
I got a chance to get familiar with “Full Text Search” due to a requirement in my last project. There was a need in my project where I had to maintain millions and millions of records in the database.
The main challenge that I faced was to provide a search page where the end user will enter a word and I had to pull all those database records which have any data matching with the word entered by the user. To achieve this, I was previously using the “LIKE” operator to look for matching rows against millions of records. But every time I would search any record, the performance of my application would go down and the fetching process was taking almost a minute to return the results.
After talking to some database gurus and reading several articles on the internet, I realized that SQL has a great feature called “Full Text Search”. What I had to do was, replaced the “LIKE” operator by “Full Text Search” and voila! I was able to improve the performance of my application by a huge margin! Now the search functionality was able to pull and display results in 5 to 7 seconds instead of 60.
Now to clarify the technical difference: The “LIKE” operator uses a character pattern search whereas “Full Text” is used for word searches.
The lesson is, “LIKE” should be only be used when we have a requirement to get results by character patterns and I would personally suggest the use of “Full Text Search” every time when we have to deal with large amount of data.
There has been a lot of talk about the cloud, it's various advantages and disadvantages. Here are my thoughts.
I believe a full cloud solution is still a distant reality. There will always be some or the other integration point, some piece of code that will be lying somewhere that is not on the cloud. So something that is 100% cloud based seems a little farfetched at the moment.
The biggest advantage that a cloud based solution provides is scalability, not reliability as some might think. Scalability that is peak load oriented. There will be some websites that are very active during some time of the day/month/year and dormant at other times. Why would you want to invest in a huge sever that remains idle for most of the time and is used only intermittently at best. An event ticket website is a good example. There is huge load closer to the event date.
However beyond that there are some serious implications of a pure cloud based system.
A cloud is, well, just like a cloud. Not too much that you can control on these things. Security, reliability are yet to be proven in a big way. You do not know where your data is kept and you do not have any control if for some reason the cloud bursts. Happened with the amazon cloud just a few days ago. Human error was the culprit.
So think carefully before you opt for a cloud based solution. It is not the panacea for all your server/hosting needs. You might be better off with something closer to you. Something you have a little more control of.
Every application developer strives to reuse programming code that would reduce the effort and cost to develop an application and to achieve that, several tools are available in the market that are developed by different vendors. Microsoft too, has always come up with new technologies and providing new tools for programmers to develop applications with ease. Unsurprisingly, continuing with this celebrated tradition, Microsoft introduced ADO.NET Entity Framework 1.0 with .NET Framework 3.5. It is based on ORM (Object –Relational mapping concept) and offers a platform for developers to work with data as application-specific objects while allowing it to manage data accessibility.
Entity Framework 1.0 has been widely criticized by developers across the globe due to various flaws in its architecture. Recently I got an opportunity to work with this version and I, along with my team, faced various challenges using it. However, Microsoft soon came up with an upgraded version 4.0 and introduced it with .NET Framework 4.0 along with lots of upgrades and new features that make it worth using. This new version, in my opinion, finally justifies the decision of Microsoft introducing this tool.
Some of the key features of Entity Framework 4.0 are as follows:
- Create an entity model and generate a database from it
- Create customized code generation templates (POCO classes, for example)
- Automatically fix entity names if database tables are in plural form
- Load related entities automatically on-demand (Lazy Loading)
- Use of Silverlight with Entity Framework and ADO.NET Data Services to quickly create data-driven Rich Internet Applications (RIA)
Besides this, EF 4.0 has a better LINQ provider that gives it an edge over NHibernate and other ORM tools. There’s a lot more in Entity Framework 4.0 that improves the developer’s productivity, code quality and application performance. It is the best ORM tool I have used so far and guess what… It is available absolutely free of cost! For organizations that do not have specialized database developers, EF 4.0 provides an excellent platform to work with database from front-end that minimizes the required resources and the cost of project.
I am eagerly waiting for the next version of Entity Framework, hoping that it will provide an even easier and powerful way to manage entities.
Welcome to the world of smart programming!
- Check out our ad in Dainik Bhaskar Indore edition front page this morning. Celebrating our CMMI Level 3 achievement. http://t.co/MCRB7UmE
1 day ago - We are now CMMI Level 3 Certified. http://t.co/mhTNNScj http://t.co/YRazntlA
1 day ago - T4 Template Code generation | InfoBeans - Creating WOW! http://t.co/NnuxeGTi
78 days ago - Excellent blog post on .NET Base Manager Class http://t.co/VYdROfRx
85 days ago - InfoBeans Pune team at Goa http://t.co/BLPPQjeQ
136 days ago
| Call 888 4INFOBEANS to schedule a presentation |
| Request a capabilities presentation |

