AllowOutsidePersistentCall parameter, how it works?

12 views
Skip to first unread message

ronovelo

unread,
Oct 1, 2011, 10:07:08 AM10/1/11
to uNhAddIns
I want to understand how this parameter works. It says "Allow
persistent call outside of the service scope. Usefull in combination
with linq queries".

I have this service class:

/// <summary>
/// Representa el servicio para el catálogo de áreas.
/// </summary>
[PersistenceConversational]
public class AreaService : IAreaService
{
private const string editOperation = "GenAreasEditar";

private readonly IAreaRepository repository;
private readonly SaveAreaRuler saveRuler;

public AreaService(IAreaRepository repository)
{
this.repository = repository;

saveRuler = new SaveAreaRuler(repository);
}

[Authorization(editOperation)]
[PersistenceConversation(ConversationEndMode = EndMode.End)]
public IOperationResponse Save(Area entity)
{
if (entity == null)
return new NotSuccessfulOperationResponse();

if (!entity.IsValid())
return new
NotSuccessfulOperationResponse(entity.Error);

var brokenRules =
saveRuler.GetBrokenRules(entity).ToArray();

if (brokenRules.Any())
return new
NotSuccessfulOperationResponse(brokenRules);

repository.Save(entity);

return new SuccessfulOperationResponse();
}
}

In Save method I call GetBrokenRules method from saveRuler.
SaveAreaRuler code:

/// <summary>
/// Representa las reglas de negocio para almacenar un área.
/// </summary>
public class SaveAreaRuler : IRuler<Area>
{
private readonly IAreaRepository repository;

public SaveAreaRuler(IAreaRepository repository)
{
this.repository = repository;
}

public IEnumerable<string> GetBrokenRules(Area entity)
{
if (EsCodigoExistente(entity))
yield return
string.Format(FormatStrings.ValoresRepetidosMasculino, "Código");

if(EsNombreExistente(entity))
yield return
string.Format(FormatStrings.ValoresRepetidosMasculino, "Nombre");
}

private bool EsCodigoExistente(Area area)
{
return repository.GetLinq()
.Where(a => a.Id != area.Id)
.Any(a => a.Codigo == area.Codigo);
}

private bool EsNombreExistente(Area area)
{
return repository.GetLinq()
.Where(a => a.Id != area.Id)
.Any(a => a.Nombre == area.Nombre);
}
}

You can see that this class hit database for check rules.

1) Do I need use AllowOutsidePersistentCall parameter in AreaService
class? If I don't use it my service works. If I use works too.

2) When I have to use this parameter? Do you have examples?

Thanks in advance and congratulatios, uNhAddins is great!!!

José F. Romaniello

unread,
Oct 5, 2011, 4:16:51 PM10/5/11
to unha...@googlegroups.com
Hello Ronovelo, you don;'t need to use AllowOutsidePersitentCall for the Save method in this case.
The parameter work in the following situation:


public IEnumerable<Something> Execute(){
  return session.Query<Something>().Where(s => s.T...);
}


Because we don't know where you will resolve the Queryable and we don't know where/when you will hit the database... The session will be wrapped so, whenever you call ToList() outside the method boundray, it will start a transaction... execute the operation, and close the transaction.


And this is useful, because in nhibernate every operation should be wraped in to an explicit transaction, not only writes.

I would not encourage to returning queryables outside the service boundary, because it is hard to test and maintain.. But It can be useful in some situations. For instance, if you are working with a grid in winforms that support queryable, you can give the grid a queryable as the datasource and the grid will figure out the filter, and how to page etc the iqueryable.



2011/10/1 ronovelo <rono...@hotmail.com>

ronovelo

unread,
Oct 15, 2011, 5:57:27 PM10/15/11
to uNhAddIns
Thanks, Jose. I understand.

Greetings from Mexico!

On 5 oct, 15:16, José F. Romaniello <jfromanie...@gmail.com> wrote:
> Hello Ronovelo, you don;'t need to use AllowOutsidePersitentCall for the
> Save method in this case.
> The parameter work in the following situation:
>
> public IEnumerable<Something> Execute(){
>   return session.Query<Something>().Where(s => s.T...);
>
> }
>
> Because we don't know where you will resolve the Queryable and we don't know
> where/when you will hit the database... The session will be wrapped so,
> whenever you call ToList() outside the method boundray, it will start a
> transaction... execute the operation, and close the transaction.
>
> And this is useful, because in nhibernate every operation should be wraped
> in to an explicit transaction, not only writes.
>
> I would not encourage to returning queryables outside the service boundary,
> because it is hard to test and maintain.. But It can be useful in some
> situations. For instance, if you are working with a grid in winforms that
> support queryable, you can give the grid a queryable as the datasource and
> the grid will figure out the filter, and how to page etc the iqueryable.
>
> 2011/10/1 ronovelo <ronov...@hotmail.com>
>
>
>
> > You can see that this class hit database for check rules.
>
> > 1) Do I need use AllowOutsidePersistentCall parameter in AreaService
> > class? If I don't use it my service works. If I use works too.
>
> > 2) When I have to use this parameter? Do you have examples?
>
> > Thanks in advance and congratulatios, uNhAddins is great!!!- Ocultar texto de la cita -
>
> - Mostrar texto de la cita -

José F. Romaniello

unread,
Oct 15, 2011, 7:43:46 PM10/15/11
to unha...@googlegroups.com
You are welcome

2011/10/15, ronovelo <rono...@hotmail.com>:

--
Enviado desde mi dispositivo móvil

Reply all
Reply to author
Forward
0 new messages