-
About Repository pattern and Nhibernate
-
I am working on a new project for an intranet application. I am using NHibernate and DDD. I created a simple Repository structure...It user Windsor IOC container for instance repository (of course you can implement your with LINQToSQL or a Fake repository for testing). This is interface and implementation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jobbing.DAL.Repository
{
//Repository Interface
public interface IRepository<T>
{
void Save(T obj);
void Update(T obj);
void Delete(T obj);
T Load<T>(object id);
T GetReference<T>(object id);
void DeleteAll(IList<T> objs);
void UpdateAll(IList<T> objs);
void InsertAll(IList<T> objs);
IList<T> GetAll<T>();
IList<T> GetAllOrdered<T>(string propertyName,bool Ascending);
IList<T> Find<T>(IList<string> criteria);
void Detach(T item);
IList<T> GetAll<T>(int pageIndex, int pageSize);
void Commit();
void Rollback();
void BeginTransaction();
}
}
//Implementation of NHibernate Repository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using NHibernate;
using NHibernate.Expression;
using NHibernate.Engine.Query;
using NHibernate.Mapping;
namespace Jobbing.DAL.Repository
{
public class NHibernateRepository<T> : IRepository<T>
{
private ISession session;
public NHibernateRepository()
{
//session = NHibernateHelper.GetCurrentSession();
session = NHibernateSessionManager.Instance.GetSession();
session.BeginTransaction();
}
#region IRepository<T> Members
public void Save(T obj)
{
session.Save(obj);
}
public void Update(T obj)
{
session.Update(obj);
}
public void Delete(T obj)
{
session.Delete(obj);
}
public T Load<T>(object id)
{
return session.Load<T>(id);
}
public T GetReference<T>(object id)
{
return session.Get<T>(id);
}
public void DeleteAll(IList<T> objs)
{
for (Int32 I = 0; I < objs.Count; ++I)
{
Delete(objs
);
}
}
public void UpdateAll(IList<T> objs)
{
for (Int32 I = 0; I < objs.Count; ++I)
{
Update(objs
);
}
}
public void InsertAll(IList<T> objs)
{
for (Int32 I = 0; I < objs.Count; ++I)
{
Save(objs
);
}
}
public void Detach(T item)
{
session.Evict(item);
}
internal void Flush()
{
session.Flush();
}
public IList<T> GetAll<T>(int pageIndex, int pageSize)
{
ICriteria criteria = session.CreateCriteria(typeof(T));
criteria.SetFirstResult(pageIndex * pageSize);
if (pageSize > 0)
{
criteria.SetMaxResults(pageSize);
}
return criteria.List<T>();
}
public IList<T> GetAll<T>()
{
return GetAll<T>(0, 0);
}
public IList<T> Find<T>(IList<string> strs)
{
System.Collections.Generic.IList<ICriterion> objs = new System.Collections.Generic.List<ICriterion>();
foreach(string s in strs){
ICriterion cr1 = Expression.Sql(s);
objs.Add(cr1);
}
ICriteria criteria = session.CreateCriteria(typeof(T));
foreach (ICriterion rest in objs)
session.CreateCriteria(typeof(T)).Add(rest);
criteria.SetFirstResult(0);
return criteria.List<T>();
}
//public IList<T> GetByCriteria<T>(IList<ICriterion> objs, int pageIndex, int pageSize)
//{
// ICriteria criteria = session.CreateCriteria(typeof(T));
// foreach (ICriterion rest in objs)
// session.CreateCriteria(typeof(T)).Add(rest);
// criteria.SetFirstResult(pageIndex * pageSize);
// if (pageSize > 0)
// {
// criteria.SetMaxResults(pageSize);
// }
// return criteria.List<T>();
//}
//public IList<T> GetByCriteria<T>(IList<ICriterion> objs)
//{
// return this.GetByCriteria<T>(objs, 0, 0);
//}
public void Commit()
{
if (session.Transaction.IsActive)
{
session.Transaction.Commit();
}
}
public void Rollback()
{
if (session.Transaction.IsActive)
{
session.Transaction.Rollback();
session.Clear();
}
}
public void BeginTransaction()
{
Rollback();
session.BeginTransaction();
}
#endregion
#region IRepository<T> Members
public IList<T> GetAllOrdered<T>(string propertyName, bool Ascending)
{
Order cr1 = new Order(propertyName, Ascending);
IList<T> objsResult = session.CreateCriteria(typeof(T)).AddOrder(cr1).List<T>();
return objsResult;
}
#endregion
}
}
On CodeProject you can find more info about helper for NHibernate and module for Open-Session-In-View.
Bye
Antonio
-
Head First Design Patterns: Template Method
-
Template Method...here it is:
Defines the skeleton of an algorithm, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorith without changing the algorithm's structure.
using System;
using System.Collections.Generic;
using System.Text;
namespace TemplateMethod
{
/// <summary>
/// Abstract class
/// </summary>
public abstract class CaffeineBeverageWithHook
{
public void prepareRecipe()
{
boilWater();
braw();
pourInCup();
addCondiments();
}
public abstract void braw();
public abstract void addCondiments();
public void boilWater()
{
System.Console.WriteLine("Boiling water...");
}
public void pourInCup()
{
System.Console.WriteLine("Pouring into cup...");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TemplateMethod
{
/// <summary>
/// Concrete class
/// </summary>
public class CoffeeWithHook : CaffeineBeverageWithHook
{
public override void braw()
{
System.Console.WriteLine("Dripping Coffee through filter...");
}
public override void addCondiments()
{
System.Console.WriteLine("Adding sugar and milk...");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using TemplateMethod;
namespace test
{
class Program
{
static void Main(string[] args)
{
CaffeineBeverageWithHook obj = new CoffeeWithHook();
obj.prepareRecipe();
System.Console.ReadLine();
}
}
}
Bye
Antonio
-
Silverlight free controls
-
I am not a fun of client side tecnology but Silverlight is really interesting. Here you can find some free controls.
Bye
Antonio
-
Head First Design Patterns: Command Pattern
-
Again Desgin Patterns...
Command Pattern:Encapsulate a request as an object, thereby letting you parametrize clients with different requests,queue,or log requests, and support undoable operations.
Command Pattern in C#:
//Command interface
using System;
using System.Collections.Generic;
using System.Text;
namespace Command
{
public interface ICommand
{
void Execute();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Command
{
//Concrete Command
public class LightOffCommand : ICommand
{
#region ICommand Members
public void Execute()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Command
{
/// <summary>
/// Concrete Command
/// </summary>
public class LightOnCommand : ICommand
{
Light lg;
public LightOnCommand(Light my)
{
this.lg = my;
}
#region ICommand Members
public void Execute()
{
this.lg.On();
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Command
{
/// <summary>
/// Null command, concrete command
/// </summary>
public class NoCommand : ICommand
{
#region ICommand Members
public void Execute()
{
System.Console.WriteLine("No command");
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Command
{
//Receiver
public class Light
{
private string _DescLight="Light";
public string DescLight
{
get { return _DescLight; }
set { _DescLight = value; }
}
public Light(string place)
{
this._DescLight = place;
}
public void On()
{
System.Console.WriteLine(this.DescLight + " is ON.");
}
public void Off()
{
System.Console.WriteLine(this.DescLight + " is OFF.");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Command
{
/// <summary>
/// Invoker
/// </summary>
public class RemoteControl
{
ICommand[] onCommands;
ICommand[] offCommands;
public RemoteControl()
{
this.onCommands = new ICommand[7];
this.offCommands = new ICommand[7];
for(int i=0;i<7;i++){
this.offCommands
= new NoCommand();
this.offCommands
= new NoCommand();
}
}
public void stCommand(int slot, ICommand on,ICommand off)
{
this.onCommands[slot]=on;
this.offCommands[slot]=off;
}
public void onButtonPushed(int slot)
{
this.onCommands[slot].Execute();
}
public void offButtonPushed(int slot)
{
this.offCommands[slot].Execute();
}
}
}
Bye
Antonio