21
Castle Manoj Waikar Pune, India.

Castle Manoj Waikar Pune, India.. Introduction Castle aspires to simplify the development of enterprise and web applications. It offers a set of tools

Embed Size (px)

Citation preview

Castle

Manoj WaikarPune, India.

Introduction

Castle aspires to simplify the development of enterprise and web applications.

It offers a set of tools (working together or independently) and integration with other open source projects, thereby helping you get more done with less code.

Castle Stack

Different projects under Castle

Rapid Web Application DevelopmentMonoRail

Object Relational Database MappingActiveRecord – The enterprise data mapping pattern implemented using Nhibernate.

ActiveRecord Generator - A desktop application to generate ActiveRecord classes based on database schema.

Inversion of Control ContainersMicroKernel / Windsor Container

FacilitiesComponentsServices

contd...

Different projects under Castle

General ToolsDynamic Proxy

Aspect Oriented ProgrammingAspectSharp

MonoRail

MonoRail (formerly, Castle on Rails) is an MVC web framework inspired by Ruby on Rails.

MonoRail differs from the standard WebForms way of development as it enforces separation of concerns; controllers just handle application flow, models represent the data, and the view is just concerned about presentation logic. Consequently, you write less code and end up with a more maintainable application.

contd...

MonoRail – How it works

MVC in MonoRail

MonoRail supports MVC out of the box. The following simple steps are needed to build a web application (that inherently uses the MVC pattern) -

Map the extension .rails with IIS.

Structure all the URLs as .../<Controller>/<Action Methods>.railse.g. http://localhost/intranet/home/index.rails

Structure your (C#) projects and solutions as -All the controllers in the Controllers sub-directory.All the views (.aspx or .rails) under a Views sub-directory.All the Master Pages under the /Views/Layouts sub-directory.All the rescues in the /Views/Rescues sub-directory.

MonoRail Features

FiltersFilters are executed before and / or after your actions. It's useful for security, dynamic content and to keep away repetitive code.

contd...

namespace Yournamespace{ public class SecurityFilter : IFilter { public bool Perform(ExecuteEnum exec, IRailsEngineContext context, Controller controller) { if (context.Session.Contains("user")) { return true; } else { context.Response.Redirect("account", "login"); }

return false; } }}

MonoRail Features

contd...

[Filter(ExecuteEnum.BeforeAction, typeof(SecurityFilter))] public class HomeController : Controller {

public void MyAction(){}

[SkipFilter]public void SomeOtherAction(){}

}

MonoRail Features

RescuesRescue is a way to associate a nice page to display some nasty error information.

contd...

[Rescue("generalerror")] public class HomeController : Controller { public void Index() { }

[Rescue("databaseerror")] public void Save() { } }

MonoRail Features

LayoutsLayouts allow you to build templates for your site by specifying common HTML and controls (such as structural HTML and navigation controls), in one place, that are available for any view to use.

NVelocity Layouts

There are other features too like Data Binding, support for AJAX, JavaScript validations.

Welcome!<p>$childContent</p>Footer

ActiveRecord

ActiveRecord is a well-known pattern described in Patterns of Enterprise Application Architecture (by Martin Fowler). Basically all static methods act on the whole set of records, while instances represents each row.

ActiveRecord is an implementation of the (above) ActiveRecord pattern for .Net.

ActiveRecord uses NHibernate, but you don't need to write any fancy XML mapping as it handles everything for you. You just need to decorate your classes with attributes to declare behavior and meaning.

ActiveRecord vs. NHibernate

How does it differ from pure NHibernate usage?

Fast development (it handles the mapping and infers as much as it can so you don't have to dig into documentation or deal with tons of xmls everytime something changes on your schema).

Predefined common methods like Create, Update, Save, Delete.

Easy to implement methods like Find, FindAll, FindByName etc.

Session and transaction scopes that abstract the (NHibernate) ISession and translates it to a more natural idiom.

ActiveRecord Generator

ActiveRecord Generator is a desktop application that generates ActiveRecord classes (code) based on a database schema.

It currently uses OleDb to extract the meta information and to infer the relationships.

Currently works only with MS Sql Server.

Inversion of Control

A principle that dictates how you design and implement system components and their interdependencies in a way that decreases coupling.

A principle that dictates that an external entity should send messages to programmer's objects in order to perform some specialised action or to allow the programmer to override some logic.

The major difference between an object-oriented framework and a class library is that the framework calls the application code. Normally the application code calls the class library. This inversion of control is sometimes named the Hollywood principle, Do not call us, we call You.

contd...

MicroKernel / Windsor Container

The MicroKernel offers an extensible inversion of control container core.

The Windsor Container aggregates the MicroKernel providing a simpler interface and a set of important features that rely on MicroKernel extension points.

The concern of the Windsor container is to offer a façade to the kernel and deal with external configuration, proxies and automatic component/facilities install.

How to use...

NOTE: If you just need the container (no external configuration, no proxies) then MicroKernel is the right choice. However most common applications require the features that Windsor Container provides.

The more straightforward usage is to instantiate the container and register the components:

and then to get the component:

IWindsorContainer container = new WindsorContainer();container.AddComponent("mail", typeof(IMailServer),

typeof(SmtpMailServer));

SmtpMailServer instance = container[“mail”] as SmtpMailServer;

Dynamic Proxy

The DynamicProxy project was created to overcome the CLR's proxy (in)capabilities. There are proxies in the CLR world, but they can be considered a bit intrusive as it forces one to extend MarshalByRefObject or ContextBoundObject.

DynamicProxy can be used to generate proxies on the fly for one or more interfaces or even concrete classes (but only virtual methods will be intercepted).

To achieve good performance, DynamicProxy generates a Multicast delegate for each method that is going to be intercepted. Thus no invocation happens using reflection and delegates have an extremely good performance compared to reflection based invocations.

AspectSharp

Aspect-oriented programming (AOP) is a new programming paradigm that allows modular implementation of crosscutting concerns.

Aspect# is an AOP (Aspect Oriented Programming) framework for the CLI (.Net and Mono). It relies on DynamicProxy and offers a built-in language to declare and configure aspects, and is compliant with AopAlliance.

Aspect# promotes -Separation of concernsCode reuseDecomposition

Related Links

Castle Projecthttp://www.castleproject.org/index.php/Main_Page

Introducing Castle – 1http://codeproject.com/csharp/IntroducingCastle.asp

Introducing Castle – 2http://www.codeproject.com/csharp/IntroducingCastleII.asp

I want my AOP (three part series)http://www.javaworld.com/javaworld/jw-01-2002/jw-0118-aspect.html