Home  /  Questions  /  Question



50   50
Jan 06, 2013


Am I correct in using the design pattern as below?

I am developing an web application with multiple layers using nhibernate as ORM. My layer structure is as follow

1. Model
2. Repository
3. Services
4. UI

with the above layers, the classes and interfaces are placed as below diagram.



sample of code for the above classes.

ProductServices.cs


    public class ProductServices : IProductServices
    {
        protected IProductRepository _ProductRepository;
        protected NHibernate.ISession _Session;

        public ProductServices(IProductRepository productRepository,
            NHibernate.ISession session)
        {
            _ProductRepository = productRepository;
            _Session = session;
            _ProductRepository.SetSession(_Session);
        }

        // cont...
    }


ProductRepository.cs


    public class ProductRepository : IProductRepository
    {
        NHibernate.ISession _Session;

        public void SetSession(NHibernate.ISession session)
        {
            _Session = session;
        }

        public IEnumerable<Product> FindAll()
        {
            return _Session.CreateCriteria<Product>().List<Product>();
        }

        //cont..
    }


From the UI layer, I create the session as request per session and inject into service layer with the help of class constructor. Then set the session of repository with a help of a method.

I can directly set the session for repository using with a constructor as same as I have done for service class. If I do so, I will not have the control over it under the service layer. Also there is a future extension plan for using a **webservice** layer.

**  A question arise here, In repository I need to set the session first. Otherwise all other methods would not function. To resolve this, in each methods, I need to verify weather the session is set or not. This is not looking to me as correct way. Any suggestion on this as well please.

**  Is the above design correct? Or need changes?