Dofactory.com
Dofactory.com
 Back to list
Views:   4K
Replies:  0
Archived

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?


Muneer Shaheed, Jan 06, 2013
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.