Thursday, 14 November 2013

Entity Framework Template not available on VS 2013

Few weeks ago I installed VS 2013 in my laptop. After installing it , without any issue, I ended up having three version of Visual Studio: Visual Studio 2010, Visual Studio 2012 and Visual Studio 2013.

Over the following weeks I opened and built VS 2010 projects with VS 2013 without any problem. This week I started developing the data model of an ASP NET MVC 5 Web Application. My plan was to used the latest and greatest version of Entity Framework, EF 6.0.

To my surprise I couldn't find the template for Entity Framework on my Visual Studio 2013:





  As I didn't perform a custom installation of VS 2013 I knew that there wasn't nothing wrong with the installation process.

After fiddling around a bit with VS 2012 and VS 2013 I noticed that my installation of VS 2012 didn't have  Entity Framework 6.0 installed. So I installed Entity Framework 6.0 for Visual Studio 2012 which you can find by following this link.

Once Entity Framework 6.0 was installed on VS 2012 I run the 'repair' installation option of VS 2013 and voilà the Entity Framework template was back on VS 2013




Sunday, 1 September 2013

OTB SharePoint forms and jQuery


SharePoint Forms and jQuery
Sunday, 1 September 2013
3:06 PM
For OTB forms SharePoint uses the hierarchy of UI controls to create the "id" and "name" attributes for their fields. This is a functionality that SharePoint inherits from ASP .NET

Sometimes you may want to get a 'hook' on one or several of these fields, without modifying the OTB form, to apply styles or modify the control behaviours  (i.e. make them read only). 
Since the developer doesn't have any control on the name and id attributes generated by SharePoint another attribute must be used to effectively get a reference to any of the fields.
First let see an illustration of the html mark-up generated by SharePoint and then we will see another attribute that can be used to get a reference to a field or fields in a SharePoint OTB Form.
If You go to an OTB document library such as the Document library and click on "new document". You will get a form, for adding a new document, consisting of a single input field with a browse button.
 









Using your favourite browse you can inspect the name and id that SharePoint (ASP .NET really) render/creates on the client side.
 






In this case we can observe that the name of the field is made of the Main Content control ID plus the name of the section where the field lies, which in this case happen to be a table row ( Html element),  plus the Input field own id. The main content control is the asp:content control that you see on the form should you open it with SharePoint designer:  <asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
 
There is another property that gets generated and it "title" As you can see from the screen shot below the title attribute of this field is "Choose a File"



Using the title attribute we can get a hook or reference to this field and perform operations on it like we would on any HTML Element using jQuery:

 //we hide the field by using the whole title property
("[Title='Choose a file']").hide();

//we hide any field starting with the term 'Choose'
["[Title^='Choose']").hide()

 //we hide any field ending with the term 'file'
["[Title$='file']").hide()
The cool thing about jQuery wildcard selectors is that if you have several fields that have a title attribute starting or ending with a common term such as 'employee' and you want to apply the same operation to all of them then you can do with a single line of code.

["[Title^='employee']").attr('disabled','disabled')

This would make any field with a title starting with 'employee' a read-only field.

Saturday, 27 July 2013

Property Sheets


If you find yourself working with MFC property sheets and run into a first-chance exception inside the  ::OnInitDialog method, check that all your dialogs resources have the right windows styles.


 
If you are using dialog resources , check that the dialog resources have the following properties:
 
Border: Thin
Disabled: True
Style: Child

In my case, one of the dialog resource had the 'Border' property set to 'Dialog Frame' and 'Sytle' to 'Pop up'

According to MSDN documentation , for the CPropertySheet::DoModal() method, all the windows styles should be disabled  with the exception of :

DS_3DLOOK
Obsolete. The system automatically applies the three-dimensional look to dialog boxes created by applications.
DS_CONTROL
Creates a dialog box that works well as a child window of another dialog box, much like a page in a property sheet. This style allows the user to tab among the control windows of a child dialog box, use its accelerator keys, and so on.
WS_CHILD
0x40000000L
The window is a child window. A window with this style cannot have a menu bar. This style cannot be used with the WS_POPUP style.
WS_TABSTOP
0x00010000L
The window is a control that can receive the keyboard focus when the user presses the TAB key. Pressing the TAB key changes the keyboard focus to the next control with the WS_TABSTOP style.
You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use the SetWindowLong function. For user-created windows and modeless dialogs to work with tab stops, alter the message loop to call the IsDialogMessage function.

 
And these are optional windows styles, that can be used with the guarantee that they won't cause First-chance exceptions.

DS_SHELLFONT
Indicates that the dialog box should use the system font. The typeface member of the extended dialog box template must be set to MS Shell Dlg. Otherwise, this style has no effect. It is also recommended that you use the DIALOGEX Resource, rather than theDIALOG Resource. For more information, see Dialog Box Fonts.
The system selects a font using the font data specified in the pointsizeweight, and italicmembers. The system passes a handle to the font to the dialog box and to each control by sending them the WM_SETFONT message. For descriptions of the format of this font data, see DLGTEMPLATEEX.
If neither DS_SHELLFONT nor DS_SETFONT is specified, the extended dialog box template does not include the font data.
DS_LOCALEDIT
Applies to 16-bit applications only. This style directs edit controls in the dialog box to allocate memory from the application's data segment. Otherwise, edit controls allocate storage from a global memory object
WS_CLIPCHILDREN
0x02000000L
Excludes the area occupied by child windows when drawing occurs within the parent window. This style is used when creating the parent window.
 

If you dialog resources are Kosha and you still get a First-change exception is very likely you are experience an exception that is expected (by design) when calling CPropertySheet::DoModal() or CPropertySheet::Create()

You can find out more about this expected exception 'that is safely handled by the operating system' following this link.

On that Microsoft page you will find three methods for resolving the issue.
 
Until next time, see ya.
 
 

Tuesday, 2 July 2013

Designing to a common interface - Application loggers for SharePoint and ASP .NET

Last week I decided to start working on an application that I am planning to release as an open source initiative. My contribution will not be a framework but rather an application that will help schools to manage their information in a better way.

As I am slowly starting to work on the design of the application I started by tackling one of the Cross-cutting concern: Logging.

It is a good practice to centralized your logging code so if there are changes on the requirements, for the logging of your application, it will be easier to perform these changes.

Consider the case where an application was originally developed with logging to files, on the file system. If there is a change on the logging requirement, say the application needs to write its logs to a Oracle or MSSQL database tables, how easy is to change the code of the application to accommodate this new requirement?

If the logging functionality is scatter all over the different layer of the application then the changes will required a greater effort. On the other hand if the logging functionalities are centralized then the changes are isolated requiring less effort.

When building modules and applications it pays off to develop them having in mind 'Anticipation of Change'.

So I want to have my logging functionality centralized as much as possible and would love to have a common or related logging API for ASP .NET and SharePoint.

After some thinking I came up with the following design



 The logger to be used for SharePoint and ASP .NET implement a common interface. The interface ILogger and the classes that implement this interface , the SharePointLogger and WebLogger, can be built into the same assembly providing a common library that can be shipped for both platforms.

The SharePoint logger inherits from the SharePoint class SPDiagnostServcieBase. Inheriting from the SPDiagnosticsServiceBase allows us to have our own custom diagnostic categories and use the WriteTrace and WriteEvent methods to perform logging. You can read it up more about the SPDiagnosticServiceBase class here

The WebLogger on the other hand contain a logger property that can be an a type of a .NET logger library or framework such as Log4Net.

How the SharePointLogger and WebLogger implements the logging of errors and messages is abstracted from their clients, the classes our component using the logging API. 
In fact we could change the .NET library used by the WebLogger and that shouldn't affect other layers of the application such as the Database layer as long as the interface contract remain the same. The interface contract in this case being the methods Log(String category, String message) and LogError(String category, String message).

This design achieves the objective of centralizing the logging functionality for the application. It also makes it possible to ship the loggers to both platforms ASP .NET and SharePoint.
As an added bonus, we all like bonuses,  this design abstracts the logging logic away from other classes/components.

Many of the concepts that help us to write  re-usable code have its root on sound Software Engineering principles and programming paradigm such as OOP (Object Oriented Programming) help us to implement these principles.

On this note, I would like to recommend you  Fundamentals of Software Engineering (2nd Edition) by Carlo Ghezzi, Mehdi Jazayeri,Dino Mandrioli

This book is timeless and highlight the good principles of developing re-usable and robust software. A good companion book for any serious developer that views writing code as a Craft.

This link to a PDF lecture will give you a highlight of the third chapter of this book where it briefly mention the Software Engineering principles. Other chapters, of the book, dive-in deep into these principles and how to apply them.

We can be agile and write good code. ;-)

Wednesday, 26 June 2013

Design for thought


Before I start developing a new piece of software, be it a new module for an existent software or a new application I find it useful to create couple of UML diagram to get a feeling of the design and the different moving parts that will make up the new module or application.

On the spirit of being agile and yet no cowboyish I just create the necessary information on the diagrams before I star development. There is a good post on whether UML is dead or not. I concur on how the author uses UML on his projects. Is UML on the way out?

You could have few UML diagrams and yet apply your favourite agile approach such as TDD (Test Driven Development) to the development phase.

Now a bit of context. I want to build a web application using the suepr-dupa .NET technology. This web application will help school to manage their information. The application is targeted to schools in developing countries so for simplicity the components will be deployed on a single server.

Few infrastructure requirements that can start our brain working are:
  • The application will run on a .NET web server.
  • The application will follow the three tier model.
  • To minimize up front investment on new hardware the application components will be
    deployed  on a single server.
  • The application will use a relational database for the storage of its data.

The .NET web server is easy to pin point. IIS.  IIS comes already as a role in many windows server operating systems such as Windows 2008 and 2012. The database can be MS-SQL 2008.

So a simple deployment diagram , based on are few infrastructure, look like this:



As we can see from the above diagram the application will be available to users via personal computer or laptops or/and other personal devices such as tablets and smart phones. HTTP will be the application protocol Notice that I only put the necessary information on the deployment diagram.

This deployment diagram got me thinking that the application could be deployed to an ASP NET application such as SharePoint. SharePoint is becoming a dominant platform for collaboration, portals, business intelligence, integration and content management..

Microsoft offer SharePoint Foundation version for 2010 and 2013 so organisations that have a valid server license can start using several of the awesome SharePoint functionality for free.

For a features comparison between SharePoint Foundation 2010 and the other editions you can visit this site: SharePoint 2010 Feature Comparison

For a feature comparison between SharePoint Foundation 2013 and the other edition you can visit this site: SharePoint 2013 feature comparison

A deployment diagram for SharePoint looks very similar to the one for ASP NET



 For simplicity reasons I am leaving the different SharePoint deployment option out.

For instance, will the application will be deployed as a Feature Solution with farm or site scope?
Will the application take advantage of the new SharePoint 2013 application model, (i.e SharePoint Apps)

This simple deployment diagrams already got me on a design mode and I can see that some application component could be decoupled from our target platform be it ASP .NET or SharePoint.
So I came up with modified deployment diagram.





Please note that Service layer can be deployed on the same server where the Presentation and Data access layer will reside. I decided to put it in separate deployment node as it also could run as a self hosted server outside IIS

In this design the service layer could be implemented as a set of RESTFUL services. We could implement the application RESTUL Api using either WCF or the new kid on block "ASP NET Web API".  I tend to favour Web API as it has less over head than WCF and our default application protocol is HTTP anyway.  If the application was to support other communication protocols like raw TCP/IP or UDP then WCF would be my first choice.

For SharePoint this design would work well because the SharePoint client object model supports RESTFUL services.  The improvement on the design is that our service layer can be nicely decoupled of the hosting environment (i.e. SharePoint).


 
 
 
 
 
 

Food for thought


For few months I have been thinking on another way, beside blogging, on contributing to the wider community.

Blogging is a very nice way of sharing your knowledge. How many of us have been stuck on a technical issue and found one or more pointers on Google? it took some one to experience the same kind of problem and then the will to share his or her experience through a blog or diary.

I originally come from a third world country and I have been thinking that there may be some organisations, on developing countries,  that want to improve their process through the use of software. Some of these organisations may not be able to afford such software or the configuration of a complex software system. So after some thought I decided that my personal project would be to build a software to help schools to manage their information.

I am planning to develop the main core of the functionalities or modules and then release it as an open source so other developers can contribute to it as well.

As always if you have any thought about this endeavour of mine , please feel free to comment on it.

Thursday, 23 May 2013

Infamous HAL_INITIALIZATION_FAILED message while installing Windows 2012


Several  weeks ago I decided to install Server 2012 on a development VM. For VM I usually use VMWare.  A while ago I was able to install and configured Windows 2008 R2 using Vmware Player. It worked fine .

Since it worked last time  I decided to re-use the recipe. I grabbed an ISO image of Windows 2012 developer's edition and mounted into a logic drive using 'Virtual Clone'. So I created the VM that would host Windows 2012 and I started it up expecting that it will pick up the mounted drive.



No, VMware Player was not able to detect a valid operating system. I did some research and found on some forums people suggesting to create an installation DVD from the ISO image. Some folks had luck with VMware detecting the operating system installation from bootable physical media.

Cool, I shall try that. I did. There was a joy for few minutes and then the installation crash . It was the eventful evening that I got introduced to the 'blue screen of the sad face'



I did another research and found some pointers. I tried several of them . No Joy.
I found one interesting post of  someone, Daniel Marquard, having the same issue with Windows 8. The solution that works on his  case was an upgrade to the latest ESXi

http://www.technosultants.com/blog/fix-hal_initialization_failed-windows-8-error-in-esxi

I just didn't feel like upgrading my Wmware at all. I heard of VirtualBox so I decided it to give it a go.
VirtualBox was able to detect the installation disk and run windows 2012 installation to completion.
No more 'blue scree of the sad face'.

One thing I found cool with Windows 2012 is the 'Desktop view' . If you press the windows 'Start' key You get
a nice view of the apps




What have been your experience installing Windows 2012? Do you find Windows 2012 easier to use than Windows 2008? More intuitive or not?


Saturday, 23 March 2013

LNK1255: link failed because of metadata errors


I got this error while playing around with C++ VS 2010 projects.

On my configuration I had several MFC DLL projects. One of them with a configuration type of static library (.lib) and the others were Application (.exe). All of them use MFC in a Shared DLL and have Multi-threaded Debug DLL (/MDd) on the Run Time option of the Code Generation setting.

It occurred to me to add a new project that will act as a Unit Test for the Application (.exe) and the Library (.lib) projects.

I read about NUnit and decided to give it a go. Now all of the projects are unmanaged C++  whereas the project for the Unit Test had to support managed code  as I decided to use Nunit.

I got some cool instructions on how to set up a managed unit test project with native C++ code here:

http://www.codinginlondon.com/2008/11/using-nunit-with-native-c.html

After creating the managed C++(CLR) class project and following the instructions on that blog I was ready to build the solution and started mocking around with the Unit Test.

Well, while building the solution I got these error messages:


Error 23 error LNK1255: link failed because of metadata errors
Error 17 error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std._String_const_iterator,std::allocator >): (0x02000019).
Error 20 error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std._String_iterator,std::allocator >): (0x02000080).
Error 21 error LNK2022: metadata operation failed (8013118D) : Inconsistent layout information in duplicated types (std._String_val >): (0x02000081)

Ummm. no good. Not good at all. I did some research using Google and found few hints but none of them gave me the right solution or just didn't apply to my configuration.

http://stackoverflow.com/questions/4842205/getting-error-when-compiling-debug-mode-c-cli-error-lnk2022
http://stackoverflow.com/questions/11990095/lnk2022-metadata-operation-inconsistent-layout-information-in-duplicated-types

The last stackoverlfow question and answer looked very promising. however , it didn't work for me. So I re-built the solution and copied the warning and error messages, as displayed on the output window of Visual Studio 2012, and went through it one by one.

I found a warning that was interesting:

defaultlib 'msvcrtd.lib' conflicts with use of other libs; use /NODEFAULTLIB:library.

This warning came from the managed C++ Class Project. The one containing the Unit Tests. ummm.

The message tell me that there is a conflict of library between the projects. First thing it came to my mind was to go back and check the Code Generation settings, particularly the  Run Time option.

It was then that I noticed that the Managed class project acting as the Unit Tests Project had a Multi-threaded DLL (/MD) setting on the Run Time option of the Code Generation settings.

On the other hand all of the other projects, native/un-managed C++, had the Run Time as Multi-threaded Debug DLL (/MDd)

So I changed the Run Time setting for the managed class project  to  Multi-threaded Debug DLL (/MDd) and all the link errors disappeared.

For a good explanation of the CRT link libraries (Run Time option) You can visit this link to a QA on
stackoverflow: http://stackoverflow.com/questions/3007312/resolving-lnk4098-defaultlib-msvcrt-conflicts-with

Until next time, bye.




Sunday, 27 January 2013

rand_s

As we know the rand function in C++ generates a pseudo random number. The rand_s function is a version
of the rand function with security enhancements.

If you ever have wanted to use the security enhanced version ,rand_s,  and after including the
necessary reference to the stdlib library  , #include ,  you still get a compile error error C3861:

error C3861: 'rand_s': identifier not found

You will need to include a macro on your code. My personal choice, which  I found more convenient,
is to add the macro _CRT_RAND_S in the "C++\Preprocessor  property page






You can find more information on rand_s on this following MDSN page