Archives

Archives / 2010 / August
  • Unit testing for a project that some classes inside it is marked with internal keyword

    Have you ever written a component that used for a other solution? This is meaning you write a component (for example, a third party, a extensions,...). So we usually use the "internal" keyword inside all classes in your component. Is it right?  And as you know if you use the "internal" keyword, you will not access from outside. Now I assume that you have a testing project out side the component project. How can you access to the internal classes inside your component project? If you try access to this component project, you will get one error "Cannot access internal class <ClassName> here"
    This post only a tips and tricks, so you can use it to access to the component that mark with the "internal" keyword. It is just simple is you must tell to the component project to assign enough permission to a assembly that want to access to your component.
    In order to make this, you are only need open the AssemblyInfo.cs file, and append one line to the end of this file. And this line is:
    [assembly: InternalsVisibleTo("<Namespace of the assembly that you want to access to this assembly>")]
    For example, I have the component project with namespace is Cik.Domain and the Unit Testing project is Cik.Domain.Testing. So I must open the AssemblyInfo.cs file inside the Cik.Domain project and add one line at the end of this file as: [assembly: InternalsVisibleToCik.Domain.Testing")]
    Now compile all projects, and all things will work well. You shall have enough visible to use some classes inside your component and do unit testing for it. I hope this tips and tricks will be useful for you.

  • The short example about EF4 CTP4 using DataAnnotation configuration

    In the last post at here, here and here, I wrote about EF4 (Entity Framework 4) with code-first approaching. In 3 parts, I used the configuration class for mapping with database (this feature is like Fluent NHibernate). But in the CTP4, we also use the DataAnnotation (new feature in .NET 4) for mapping with database. If we use the DataAnnotation, we shall be easy to mapping with much Convention over Configuration rules. You can go to this link for read all convention’s EF4 CTP. As Andrew suggest me, the example for mapping of my last post is very simple and did not have complex. So I downloaded the Northwind database from Microsoft and implemented in this post. Why do we use the DataAnnotation? As you know, Microsoft integrated it into .NET 4, and now they used it in all new technology, such as Silverlight, WPF, ASP.NET MVC… And in the future I think it will become popularly. And we shall be familiar with it, use it easily. In this post, I will explorer about mapping use the DataAnnotation, and some convention in relationship for ORM mapping.