Showing posts with label Pattern. Show all posts
Showing posts with label Pattern. Show all posts

June 01, 2009

nHibernate – To be or not ?

There are lots of chaos around ORM like nHibernate. Some of in favor of nHibernate where as some hate it like nothing. The triumph of nHibernate is totally depends on architecture of your project & complexity of the database.

Advantages of nHibernate

  • Rapid application development – you don’t have to write any SQL or stored procedure!
      • Mapping can be made easy with ActiveRecord (attribute-based mapping) - you can reduce the time of writing XML or any relational attributes.
  • Better for maintenance – no stored proc, no SQL therefore any table changes affects only entities & associated layers
  • Clear separation of UI & DB layer. If you rename any field in any table then you have to change only entity mapping. Where as in traditional relational model any table field change affects all layers.
  • Easily migrate your code between different databases
  • Provides business entity form of representation of DB tables so you get all advantages of OOP.
  • Massive gain in performance with distributed cache (e.g. memcache) (assuming most data is non-volatile)
  • Think about nested query

Disadvantages

    ORM is not a good choice if
  • your database has complex(deep) relationships
  • high frequency applications where data is volatile
  • old/legacy/migrated database – In ORM first you have to develop object model & then database. If you don’t want to break existing poor relations then ORM is not a good choice
  • disconnected layers – smart client won’t supports lazy loading; transferring data & message size (Creating DAO classes mapped with domain may improve performance but requires lots of coding & therefore future maintenance)
  • consider time require to train developers
  • consider time for optimization

Conclusion

DAO and ORM are both valid approaches to persistence and the choice between them needs to be considered on a per-project basis. DAO approach is industry standard & proven approach. Since DAL is most vital part of any architecture you need to be VERY careful if you are going to use ORM like nHibernate.

Some refrences & sample code

August 19, 2008

Patterns & practices: Improving Web Services Security

This guide shows you how to make the most of WCF (Windows Communication Foundation) with end-to-end application scenarios, it shows you how to design and implement authentication and authorization in WCF, how to improve the security of your WCF services through prescriptive guidance including guidelines, Q&A, practices at a glance, and step-by-step how tos.

May 30, 2008

High scalable architectures

Last week I came across interesting web site by Todd Hoff who was former IBM and Intuit Employee.

His site http://highscalability.com/ on high scalable architectures has some big shot web sites explaining lore, art and science behind it. Some examples are:

His personal web site is at http://possibility.com/Tmh/ and Wiki stuff is at http://www.possibility.com/wiki/index.php?title=Main_Page

April 03, 2008

Web Application Performance Design Inspection Questions

Here is Wiki stuff on Web Application Performance Design Inspection Questions

November 03, 2007

Using Enterprise Library Validation attributes

Class

Purpose

Description/Example

NotNullValidator

Checks to ensure a supplied value is not null.

[NotNullValidator(Ruleset = "Default", MessageTemplateResourceName = "EMPLOYEE_ERR_EMPLOYEE_CODE_NULL", MessageTemplateResourceType = typeof(Properties.MyValidation))]

public virtual string EmployeeCode {

  1. You can use it to check null string or null object (entity)

  2. Advisable to use it with ValidatorComposition

ContainsCharactersValidator

Checks a string for the presence of the characters specified in the CharacterSet property.

(e.g. does not contain any of /\?<>”:)

StringLengthValidator

Ensures that the length of a string is within specified limits.

(e.g. string is at least 8 characters long)

If business entity is generating any non localized string then you have to replace it with following constant:

GLOBAL_VALIDATION_ERROR_STRING_LENGTH

And the associated message for US culture will be

"\"{0}\" must be between {3} and {5} characters"

Example – output for null Employee Code will be:

EmployeeCode must be between 1 and 25 characters

Note with {0} it displays property name as-is e.g. EmployeeCode without space in between. If you want property name specific then you need to add constant accordingly in your application resource file.

E.g.

MY_VALIDATION_ERROR_EMPLOYEE_CODE_STRING_LENGTH

And message for US culture will be -

Employee Code must be between 1 and 25 characters

RangeValidator

Checks whether a value falls within a specified range.

(e.g. must be from 10-20 or 1/1/1950 to 12/31/1999)

DateTimeRangeValidator

Lets you check whether a supplied DateTime object falls within a specified range.

RegexValidator

Checks whether a value matches the pattern specified by a regular expression.

(e.g. value is a valid e-mail address)

[RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplate = "Invalid Email.", Ruleset = "Default")]

public string EmailAddress {

RelativeDateTimeValidator

Checks whether a value falls within a specified date/time range.

(e.g. birth date is more than 18 years ago)

// Check for 18

[RelativeDateTimeValidator(18, DateTimeUnit.Year)]

Or

// check for 18 to 30

[RelativeDateTimeValidator(18, DateTimeUnit.Year, 30, DateTimeUnit.Year)]

EnumConversionValidator

Checks whether a string can be converted to an enum.

(e.g. string can be converted to a value in the Color enum type)

PropertyComparisonValidator

Compares the value of a property with another property.

(e.g. StartDate < EndDate)

In the below example the validation will succeed if the StartDate is less than the EndDate OR greater than DOB.

[ValidatorComposition(CompositionType.Or)]

[PropertyComparisonValidator("EndDate", ComparisonOperator.LessThan)]

[PropertyComparisonValidator("DOB", ComparisonOperator.GreaterThan)]

public DateTime StartDate {

get { return m_StartDate; }

set { m_StartDate = value; }

}

TypeConversionValidator

Checks whether a string can be converted to a specific type.

(e.g. string can be converted to a DateTime)

[DataMember]

private string mydate; [TypeConversionValidator(typeof(DateTime),MessageTemplate="Not a valid date",Ruleset="Default")]

public string MyDate {

DomainValidator

Checks whether a value matches one of a list of supplied values.

(e.g. must be one of state from {OH, MI, NY, IL})

ObjectValidator

Validates an object reference.

ValidatorComposition

Allows you to create a composite validator by combining one or many validators. You can specify either "And" or "Or" type validation through CompositionType enumeration.

In the below example the validation will succeed if the StartDate is less than the EndDate OR greater than DOB.

[ValidatorComposition(CompositionType.Or)]

[PropertyComparisonValidator("EndDate", ComparisonOperator.LessThan)]

[PropertyComparisonValidator("DOB", ComparisonOperator.GreaterThan)]

public DateTime StartDate {

get { return m_StartDate; }

set { m_StartDate = value; }

}

ObjectCollectionValidator

Ensures that an object is a collection of a given type, and invokes validation on each element in the collection.

§ All validation rules can be negated

E.g String Length must not be between 5 and 10 characters

[StringLengthValidator(5, 10, Ruleset = "Default", MessageTemplate = "Validation Failed",Negated=true)]

public virtual string SomeProp {

August 06, 2007

ADO .NET Entity Framework

The upcoming version of ADO .NET comes with entity framework, you can read some more on this MSDN article.

March 20, 2007

Web Service Pattern

Here are some resources on Web Service Software Factory -

June 19, 2006

Designing Data Tier Components and Passing Data Through Tiers

When designing a distributed application, you need to decide how to access and represent the business data associated with your application. This document provides guidance to help you choose the most appropriate way of exposing, persisting and passing that data through the tiers of an application.

http://msdn2.microsoft.com/en-us/library/ms978496.aspx

June 03, 2006

Caching Architecture Guide for .NET Framework Applications

This document provides caching guidance for developers and architects using the Microsoft® .NET Framework. It introduces the concepts involved in caching, discusses the technologies that can be used to provide caching facilities, and describes the mechanisms you should use implement to cache data in a distributed application. It contains recommendations and best practices for all aspects of caching in .NET-based applications.

http://msdn2.microsoft.com/en-us/library/ms978498.aspx

June 01, 2006

Authentication in ASP.NET: .NET Security Guidance

This article discusses the importance of security considerations when designing a server application. Both Microsoft Internet Information Services (IIS) and ASP.NET provide security models that will allow you to authenticate your users appropriately and obtain the correct security context within your application.

http://msdn2.microsoft.com/en-us/library/ms978378.aspx

Microsoft patterns & practices Home

http://msdn.microsoft.com/practices/

February 15, 2006

Enterprise Library for .NET Framework 1.1

The patterns & practices Enterprise Library is a library of application blocks designed to assist developers with common enterprise development challenges. Application blocks are a type of guidance, provided as source code that can be used "as is," extended, or modified by developers to use on enterprise development projects. Enterprise Library features new and updated versions of application blocks that were previously available as stand-alone application blocks. All Enterprise Library application blocks have been updated with a particular focus on consistency, extensibility, ease of use, and integration.

http://msdn2.microsoft.com/en-us/library/ms954836.aspx

November 24, 2004

Aspect Oriented Programming and .NET

Check this article on Aspect Oriented Programming and .NET

November 17, 2003

Caching Architecture for .NET Framework Applications Roadmap

This MSDN article provides caching guidance for developers and architects using the .NET Framework.

October 30, 2003

Behavioral Pattern – Template Model

The Template pattern allows the building of a class or algorithm that can operate on multiple classes that support a predefined set of methods.

• The template class only uses these methods and other primitives to build functionality.

• The advantage of this pattern is that code that is identical, except the type of object that it operates on, can be written only once, instead of having to be written for every possible type.

• Templates are natively supported by many languages, and ATL and STL are built entirely on the template pattern.

Links:

http://www.dofactory.com/Patterns/PatternTemplate.aspx

Template Method pattern discussion

Behavioral Pattern – Visitor

  • A way to separate an algorithm from an object structure
  • Specifies how iteration occurs over the object structure

Links:

http://www.dofactory.com/Patterns/PatternVisitor.aspx

Visitor pattern discussion

October 20, 2003

Behavioral Pattern – State

Also known as the objects for states pattern

  • Used to represent the state of an object
  • A clean way for an object to partially change its type at runtime

Links:

October 19, 2003

Behavioral Pattern – Observer

The observer pattern (also known as publish/subscribe) is a design pattern used to observe the state of an object in a program

  • Objects register to observe an event which may be raised by another object
  • This pattern is mainly used to implement a distributed event handling system

Links:

October 18, 2003

Behavioral Pattern – Memento

A memento is an alternate representation of another object, often in a format suitable for transmission across an external interface.

• Provides the ability to restore an object to its previous state

Example:

Finite sate machine or random number generator

Links:

October 13, 2003

Behavioral Pattern – Mediator

Provides a unified interface to a set of interfaces in a subsystem

The mediator pattern consists of a mediator class that is the only class that has detailed knowledge of the methods of other classes. Classes send messages to the mediator when needed and the mediator passes them on to any other classes that need to be informed. The mediator class promotes looser coupling between a number of other classes.

Links

http://www.dofactory.com/Patterns/PatternMediator.aspx

Mediator pattern discussion