Pragmatic Developer

Ali Özgür

Bookmark Blog

Add to Technorati Favorites

Google Talk

Chat with Ali Özgür

Purchase PragmaSQL from

Calendar

«  December 2008  »
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
View posts in large calendar

Tag Cloud

Don't show

    Authors

    Recent Comments

    Banners




    I've already developed some projects with NHibernate and I can say that NHibernate is a real time saver. But I must admit that it takes some time to get used to NHibernate. As an ORM NHibernate has fantastic features but if you want to develop successfull projects with NHibernate you must know that NHibernate is only one part (ORM) of a successfull layered architecture. In the attached solution I tried to give you the basic idea of a layered architecture and how NHibernate can be used within such an architecture

    Attached solution includes the following projects

    • Core: Our domain objects (entities) reside in this layer. Also DAO (Data Access Objects) interfaces are defined in this layer.
    • DAL (Data Access Layer): Default implementation of DAO interfaces reside in this layer.
    • BLL (Business Logic Layer): Business logic code resides in this layer.
    • Common: Some utility calsses reside in this project
    • Tests.Disconnected: Includes unit tests for domain objects and business logic. Notice that unit tests in this project do not require domain objects to be persisted (NHibernate behaviour will not be tested), so the need for a database connection is eliminated which in turn makes our unit tests fast. Notice how we replaced our default DAL with mocked one.
    • Tests.Connected: This project includes unit tests too, but this time we want to test how our domain objects and bussiness logic perform NHibernate. NHibernate provides very cool features like native sql, lazy loading and cascading and we will likely want to test how our domain objects behave when armored with these cool features of NHibernate. It is also very likely that we will have some mapping errors (typos likely) in our Core, these tests will help us catch these errors. Performance bottlenecks possibly caused by our NHibernate mappings (for example we may discover that we need to make a child collection to be lazy loaded) can also be identified with help of these tests.

    In the sample solution you can also find a simple usage of Castle Windsor Inversion of Control (IoC) container. We use IoC to be able to load different implementations of our DAL (DAO implementations). 

    Another very important point you need to understand really well is session management of NHibernate when used in web applications. Thanks to Burrow contribution project this task is made very simple, you do not even need to write single line of NHibernate session management code. You only need to inherit your DAO implementation classes from GenericDao<T> class and you are ready to go. 

    Additional Notes

    • Database script is included in Tests.Connected project under DBScript folder. 
    • Sample project uses NHibernate  2.0 Aplha1 and NHibernate.Burrow is also Alpha1.
    • ASP .NET MVC Preview 3 to run Sales.MVC sample
    • TestDriven .NET to run NUnt tests

    Suggested Readings

    Downloads

    Update History

    • 09 June 2008
      • Castle files under Libs folder updated to Castle RC3
      • SQLite references removed
      • TestFixtureTearDown override of CustomTestBase class in Sales.Tests.Connected assembly commented out

    Posted in: NHibernate  Tags:

    Currently rated 4.8 by 4 people

    • Currently 4.75/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    GoF book says that "Observer pattern should defina a one-to-many dependency between objects so that when one object changes state, all its dependenst are notified and updated automatically". Subscribing to RSS feeds is a nice analogy.  You subscribe to RSS feeds to show interest and you become an observer who demand for notification and RSS feeds become the subject and are responsable for providing information to all subscribers. I think this bit of information describing the pattern is enough, now lets see how we implement observer pattern.

    Lets assume that we are developing a car controller software. One of the requirements is "Provide visual warning to the driver if seat belts are not locked". To meet the requirement we design

    • SeatBelt class which is the subject. This class should notify interested objects about the state changes
    • SeatBeltMonitor class is our observer. This class monitors for status change messages provided by the SeatBelt and displays messages

    Implementing Observer Pattern in conventional way 

    Download GoF_Patterns_Observer1.rar (20,36 kb)

    It is suggested to specify an interface or abstract classes when dealing with patterns so that the objects we create will adhere to some predefined contract. So we start our implementation by specifying two interfaces ISubject and IObserver

    001    public interface ISubject
    002    {
    003        void RegisterObserver(IObserver observer);
    004        void RemoveObserver(IObserver observer);
    005    }
    006
    007    public interface IObserver
    008    {
    009        void Update(bool locked);
    010    }

    As I've mentioned above our SeatBelt class is the subject (source) of the state change and should implement the contract so that interested parties shall define their interest to the state changes More...


    Posted in: C# , GoF Patterns  Tags:

    Currently rated 4.0 by 1 people

    • Currently 4/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    I believe Factory Pattern is one of the most known pattern in development community. Simply Factory Pattern states that we have a factory object which is used to create other objects. But GoF specification for the Factory Pattern is a little bit different. GoF book says that "We should define an interface for creating an object, but let subclasses decide which class to instantiate." 

    How you implement the Factory Pattern depends on your custom needs. 1) If you want to control object instantiation behaviour of your factory provide a static class with static methods or sealed factory class to your clients, else 2) if you want to keep your code closed for modification but open for extension follow the GoF way by providing an interface or abstract factory class.

    Method 1

    Download GoF_Patterns_Factory1.rar (18,45 kb)

    Now lets implement Factory Pattern for our Vehicle instantiation process so that we do not want our clients to have alternative factory implementation at all.

    001   //Abstract Vehicle class
    002    public abstract class Vehicle
    003    {
    004        public abstract string Description { get; }
    005    }
    006    
    007    //Concrete implementation
    008    public sealed class Car : Vehicle
    009    {
    010        internal Car() { }
    011        public override string Description
    012        {
    013            get { return "Car";}
    014        }
    015    }
    016
    017    //Concrete implementation
    018    public sealed class Helicopter : Vehicle
    019    {
    020        internal Helicopter() { }
    021        public override string Description
    022        {
    023            get { return "Helicopter"; }
    024        }
    025    }
    026
    027    //Concrete implementation
    028    public sealed class Jet : Vehicle
    029    {
    030        internal Jet() { }
    031        public override string Description
    032        {
    033            get { return "Jet"; }
    034        }
    035    }

    I've chosen to implement my factory More...


    Posted in: C# , GoF Patterns  Tags:

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    BlogEngine .NET 1.3 uses www.gravatar.com/avatar.php?gravatar_id=[MD5 Hash] to retreive Gravatar images for the comments. But this url is not supported by Gravatar anymore as far as I understand. We have to replace this url with this one http://en.gravatar.com/avatar/.

    In order correct this url open CommentView.ascx.cs file found in "User controls" folder and replace the source code of the Gravatar method with this one

    001  protected string Gravatar(string email, string name, int size)
    002  {
    003    if (email.Contains("://"))
    004      return
    005          string.Format(
    006              "<img class=\"thumb\" src=\"http://images.websnapr.com/?url={0}&amp;size=t\" alt=\"{1}\" />", name,
    007              email);
    008    //http://www.artviper.net/screenshots/screener.php?&url={0}&h={1}&w={1}
    009    MD5 md5 = new MD5CryptoServiceProvider();
    010    byte[] result = md5.ComputeHash(Encoding.ASCII.GetBytes(email));
    011
    012    StringBuilder hash = new StringBuilder();
    013    for (int i = 0; i < result.Length; i++)
    014      hash.Append(result[i].ToString("x2"));
    015
    016    StringBuilder image = new StringBuilder();
    017    image.Append("<img src=\"");
    018        
    019    //Change the url
    020    image.Append("http://en.gravatar.com/avatar/");
    021        
    022    //Change the MD5 hash appending code
    023    image.Append(hash.ToString());
    024    
    025    image.Append("&amp;rating=G");
    026    image.Append("&amp;size=" + size);
    027    image.Append("&amp;default=");
    028    image.Append(Server.UrlEncode(Utils.AbsoluteWebRoot + "themes/" + BlogSettings.Instance.Theme + "/noavatar.jpg"));
    029    image.Append("\" alt=\"\" />");
    030    return image.ToString();
    031  }

    After changing the url in source code you do not nedd to build/recompile BE you simply replace the old file with the modified one. 


    Posted in: BlogEngine.NET  Tags:

    Currently rated 5.0 by 1 people

    • Currently 5/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    Download Source(23,52 kb)

    We have different kind of vehicle implementations inherited from an abstract Vehicle class. Our code looks like this

    001    //Abstract Vehicle class
    002    public abstract class Vehicle
    003    {
    004        public abstract string Description { get; }
    005
    006    }
    007    
    008    //Concrete implementation
    009    public sealed class Car : Vehicle
    010    {
    011        public override string Description
    012        {
    013            get { return "Car"; }
    014        }
    015    }
    016
    017    //Concrete implementation
    018    public sealed class Helicopter : Vehicle
    019    {
    020        public override string Description
    021        {
    022            get { return "Helicopter"; }
    023        }
    024    }
    025
    026    //Concrete implementation
    027    public sealed class Jet : Vehicle
    028    {
    029        public override string Description
    030        {
    031            get { return "Jet"; }
    032        }
    033    }

    Suppose that our client wants to rent these different kind of vehicles and print vehicle information plus rental specific info such as FromDate, ToDate and the customer's name. We could implement this requirement by adding those new properties to our base Vehicle class in order to meet our customer's need. We also have to modify our concrete Vehicle classes so thet they can provide rental information through their Description property.

    Here is our modified Vehicle class More...


    Posted in: C# , GoF Patterns  Tags:

    Currently rated 4.0 by 1 people

    • Currently 4/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5