Tuesday, August 16, 2011

How to register COM-class within the application manifest file

You don't always need to register referenced COM-classes in the windows registry. Sometimes it's much more simple and reliable to use application manifest for referencing COM-classes. This feature is called Registration-free COM and well described in this article.
In few words, you can add application manifest file to you .NET application project (Add New Item -> Application Manifest File). Then you can reference COM this way:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <file name="MyPrivateCOM.dll">
    <comClass clsid="{AC78B8E2-6C4D-11d1-ADE2-0000F8754B99}" threadingModel="Apartment" />
 </file>
</asmv1:assembly>


* This source code was highlighted with Source Code Highlighter.
I provided a simplified version of the manifest file with a COM-reference only. Though, it's correct and working sample. Of course, application manifests allows to do much more than just referencing COM-classes, but it's an another big and interesting topic.

Saturday, March 5, 2011

How to install SQL Server Management Studio Express 2008

In the SQL Server 2005 installing of SSMSE (SQL Server Management Studio Express) was very easy, just all about launching the installation file. In the SQL Server 2008 process of installing SSMSE is a bit different, since installation is represented as a part of the SQL Server Installation Center Utility.
In order to install SSMSE 2008 you need to perform the following steps:
  1. Download the installation package and launch it.
  2. Click "New SQL Server stand-alone installation or add features to an existing installation".
  3. Wait untill installation setup support files and configuration checks, then click OK and Install.
  4. Click "Perform a new installation of SQL Server 2008", but not "Add features to an existing instance of SQL Server 2008" (!!!).
  5. Click Next in the Product Key dialog even if all the options are disabled.
  6. In the feature selection dialog enable Management Tools feature and click Next.
  7. Wait untill installation performs an additionnal configuration checks. Then click Install. Note: You might receive a configuration error, if you have SQL Server 2005 Express tools already installed on the machine. In my case I removed SQL Server 2005 Express tools. But it was not enough. I also used solution provided here (removing registry key). It was helpfull.
  8. Wait untill installation completed. Enjoy!

Thursday, December 16, 2010

Using RouteValueDictionary to test anonymous objects in C#

For example, you have the following anonymous type declaration & instantiation:
var someObject = new {someProperty= "someValue"};
How to inspect value of the id property? You need a wrapper, that is able to index anonymous objects. For example, RouteValueDictionary.

Here is code:
var wrapper = new RouteValueDictionary(someObject);
Assert.AreEqual("someValue", wrapper["someProperty"]);

If your ASP.NET MVC controller returns JsonResult, you can use this approcah to test contents of the Data property, which usually references to some anonymous object.

Thursday, September 9, 2010

Beyond Multiple Choice: Lab Exam Demonstration for Developers

Microsoft demonstrates a proof of concept for a lab that tests your hands-on development skills in the Visual Studio environment.









Link: http://j.mp/cGIn69.

You can leave your feedback by completing the survey at http://j.mp/beRxTG.

Friday, August 20, 2010

Great ASP.NET MVC 2 (+ EF 4.0) sample

Recently I``ve found a great sample project on the codeplex that shows how to build web applications with ASP.NET MVC 2. I speak about MVC Music Store.



First of all, this project was interesting for me because it demonstrates how to organize data access via Entity Framework 4. There are also a lot of nice stuff in this project, including:

  • Templating, Data Annotations, Validation;
  • View models;
  • Client-side validation, jQuery and AJAX helper methods;

Project includes 80-page PDF walktrough tutorial that describes in details how to create sample application starting from clicking File -> New Project.

You need Visual Studio 2010 and ASP.NET 4.0 to run samples.

ASP.NET MVC 2 is the latest official release at the current moment (ASP.NET MVC 3 is still under development). So, this tutorial is very actual now. I recommend it to everyone who are not familiar with ASP.NET MVC technology. Moreover, this tutorial might be useful if you would like to understand how to use Entity Framework 4 within your ASP.NET MVC website.

Friday, May 28, 2010

Dependency injection and Microsoft Enterprise Library validation app block

Sometimes, when you use EntLib (Microsoft Enterprise Library) validation block, you may realize, that the built-in set of validators in the EntLib does not satisfy all the your needs. Of course, you can create a custom validation, inheriting from the abstract Validator and ValidationAttribute classes. This is a good way to create a validation logic, that can be used in the different parts of your project. But what to do, if you need to access  some business components inside of validator, which should be resolved via the dependency injection (DI) mechanism? Since the EntLib is responsible for instantiating validators instances, it doesn`t know anything about your registered dependency classes, and will not be able to resolve such dependencies. In this case you have two possible solutions:
  • Access DI container via a static class, that has a reference to it;
  • Use SelfValidationAttribute in the class you have to validate;
I do not recommend the first approach with a static class, because it makes your domain model dependent on the application infrastructure. From my point of view, the less classes know about dependency injection container - the better. The second approach is more prefferable, it is very suitable for the progressive domain models and view-models from the MV-VM pattern. All that you need to do is:
  1. Define in your class a public void method that takes a single parameter of type ValidationResults;
  2. Decorate this method with a SelfValidationAttribute;
  3. Decorate your class with a HasSelfValidationAttribute;
Let`s consider the following example. We have a CreateProductViewModel class that is used for representing a product creation form to user. Let`s assume, that we want to deny user to create a new product, if there is a product with the same name already exists in the store database. Moreover, we want to use a business component, which is responsible for the product-related operations. Since we have a large composite application, we use a DI mechanism to provide a loosely coupled architecture and increase testability. Below you can find the code of this sample class.

  1. [HasSelfValidation]
  2. public class CreateProductViewModel
  3. {
  4.   private readonly IProductComponent
    _productComponent;
  5.  
  6.   public CreateProductViewModel(IProductComponent productComponent)
  7.   {
  8.     _productComponent = productComponent;
  9.   }
  10.  
  11.   [StringLengthValidator(1, 20)]
  12.   public string ProductName { get; set; }
  13.  
  14.   [SelfValidation]
  15.   public void
    IsDuplicateProductName(ValidationResults results)
  16.   {
  17.     var product =
    _productComponent.GetProductByName(ProductName);
  18.  
  19.     if (product != null)
  20.       results.AddResult(new ValidationResult("Product with same name already exists", null,
    "ProductName",
  21.                          
    null, null));
  22.   }
  23. }
* This source code was highlighted with Source Code Highlighter.
Here is the IsDuplicateProductName method that performs a validation logic by acquiring a product component. Product component is an instance of the class, which implements IProductComponent interface and most probably becomes resolved via the constructor dependency injection, but this is not so significant. You can get full validation results by invoking the code like this:

  1. var validator = ValidationFactory.
    CreateValidatorFromAttributes<CreateProductViewModel>();
  2.  
  3. ValidationResults results;
  4. validator.Validate(createProductViewModelInstance, results);
* This source code was highlighted with Source Code Highlighter.

Sunday, May 9, 2010

Specifying the default browser for running ASP.NET MVC projects in VS2008

By the default Visual Studio 2008 runs web sites/applications in the Internet Explorer. When you are opening ASP.NET MVC project there is no way in the IDE to change this behaviour. So, if you want to make your ASP.NET MVC project to run in the Chrome, for example, you need to do the following:
  1. Open/create a simple (non-MVC) web site in the Visual Studio.
  2. Select any .aspx file, or the site root in the Solution Explorer window.
  3. Go to File -> Browse with...

Then you need to specify your default browser. If you don`t find the browser what you want in the available browsers list, you can add a new browser by specifying path of it`s executable:


Changes you made are global and affect every web project in the Visual Studio. Now you can switch to your ASP.NET MVC project and enjoy it`s running in your favorite browser.