Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:17:27+00:00 2026-05-29T04:17:27+00:00

We have a project that uses an Interceptor object to tell NHibernate to do

  • 0

We have a project that uses an Interceptor object to tell NHibernate to do some general works before saving an entity.This interceptor has a single task to do.Now there’s another task that should be added to this interceptor (NHibernate doesn’t support multiple interceptors) but I don’t want to make this interceptor complicated instead I would like to use composition pattern that will manage all registered interceptors.something like this :

public bool Onload(object entity,object id,object[] state,string propertyNames,IType[] types_
{
var result=false;
foreach(var interceptor in _registeredInterceptors)
result=result || interceptor.OnLoad(entity,id,state,propertyNames,types);
return result;
}

public bool OnFlushDirty(object entity,object id,object[] state,string propertyNames,IType[] types_
{
var result=false;
foreach(var interceptor in _registeredInterceptors)
result=result || interceptor.OnFlushDirty(entity,id,state,propertyNames,types);
return result;
}

by looking at this code I realized that there might be a better way preventing me from repeating myself.
the question is can I make this code more simpler and abstract using Lambda expressions and yield keyword?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:17:28+00:00Added an answer on May 29, 2026 at 4:17 am

    One way of doing it could look like this:

    public bool Execute(IList<IInterceptor> interceptors, Func<IInterceptor, bool> func)
    {
        bool result = false;
        foreach (IInterceptor interceptor in interceptors)
        {
            result = result || func(interceptor);
        }
    
        return result;
    }
    

    And in the parent interceptor:

    public bool Onload(object entity, object id, object[] state, string propertyNames, IType[] types_
    {
        return Execute(_registeredInterceptors, x => x.OnLoad(entity, id, state, propertyNames, types);
    }
    
    public bool OnFlushDirty(object entity, object id, object[] state, string propertyNames, IType[] types_
    {
        return Execute(_registeredInterceptors, x => x.OnFlushDirty(entity, id, state, propertyNames, types);
    }
    

    Update

    If you want the result type to be generic you could do it like this:

    public static T Execute<T>(IList<IInterceptor> interceptors, Func<IInterceptor, T> func)
    {   
        T result = default(T);
        foreach (IInterceptor interceptor in interceptors)
        {
            // your logic based on type T
        }
    
        return T;
    }
    

    Execution of the generic version would look exactly the same as of the bool one because of Type inference.

    Is this what you had in mind?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that uses MVC 3 and Entity Franework. The site works
I have a project that uses Java and Groovy intermixed. This works very well.
We have a project that uses JPA/Hibernate on the server side, the mapped entity
I have a project that uses log4net and works fine on the developer machines.
I have a project that uses an Access DB file for reference tables. This
I have a project that uses some legacy script for processing the source code.
I have a project that uses data from Entity Framework and present them in
I have a project that uses GLSL shaders. This project is designed not to
I have a project that uses some legacy COM components. When I included them
I have a project that uses a DLL that I have created. Everything works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.