Click here to Skip to main content
13,189,324 members (64,806 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm doing an system using nHibernate for persistence.
In my code I'm separating the business classes/objects from repository. I have a set of repository classes with internally use nhibernate, but this is transparent to my application.
I have a need to log every CRUD operation in my repository, so I created a log class and a log repository, but all repositories need to have two things in order to work properly with the log:
1) An instance of the user class, to register who is the user that perfomed the crud operation
2) An instance of the HTTPContext object, to get some information about session, server and browser.

I know it is the wrong way to do this, but I dont know the best way. If I put log logic in the base repository class I have the repository coupled with HTTPSession and can't use my business objects in a windows form application.
If I do log logic outside repository I need to do this in the application layer, and maybe replicate log-related code throgh my application.

Besides, how can I "inject" the user and session information in my repository class? I am doing this on the constructor.

Below follows an example of my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LiderTel.Modelo;
namespace LiderTel.Persistencia
    public class BaseRepositorio<T> where T: Entity
        private Sessao _sessaoInjetadaRepositorio;
        private Usuario _usuarIoinjetadoRepositorio;
        protected virtual Sessao GetSession()
            return _sessaoInjetadaRepositorio;
        protected virtual Usuario GetUser()
            return _usuarIoinjetadoRepositorio;
        public BaseRepositorio(Sessao sess, Usuario usu)
            _sessaoInjetadaRepositorio = sess;
            _usuarIoinjetadoRepositorio = usu;
        public BaseRepositorio(Sessao sess)
            : this(sess, sess==null?null:sess.UsuarioCriador)
        public BaseRepositorio()
            : this(SessionServices.GetSessaoCorrente(),  SessionServices.GetSessaoCorrente()==null?null:SessionServices.GetSessaoCorrente().UsuarioCriador)

        private string GetUserID()
            return (GetUser() != null) ? GetUser().ID.ToString() : "";
        public virtual void Add(T ent)
            ent.Criacao = DateTime.Now;
            ent.UsuarioCriador = GetUser();
            ent.Excluido = false;
            ent.Ativo = true;

            Log log = new Log();
            log.Acao = string.Format("{0} {1} created by {2}.", ent.GetType().Name, ent.ID.ToString(), GetUserID());
            log.TipoEvento = TipoEventoLog.Inclusao;
            log.Entidade = ent.ID;
            if (GetSession() != null)
                log.SessaoID = GetSession().ID;
            log.ComoEstava = ent.InternalToString();
            LogRepository logrep = new LogRepository(GetSession(), GetUser());

sorry about my english
Posted 28-Sep-12 7:43am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171016.2 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100