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

  • Home
  • SEARCH
  • 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 7002675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:57:33+00:00 2026-05-27T20:57:33+00:00

I’m looking for a tutorial on how to build validation service layer. I want

  • 0

I’m looking for a tutorial on how to build validation service layer. I want the layer to be present inside my ‘domain’ assembly. At the moment I’ve got my domain model and (maybe not the best) generic repository implementation.

My repository implementation is next:

public sealed class Repository<T> : Interface.IRepository<T> where T : Entity<T>
{
    private ISessionFactory sessionFactory;        

    public Repository(ISessionFactory sessionFactory)
    {                      
        this.sessionFactory = sessionFactory;
    }

    public T Get(Guid id)
    {
        using(var session = this.sessionFactory.OpenSession())
        {
            return session.Get<T>(id);
        }
    }
    public IQueryable<T> Get(Expression<Func<T, Boolean>> predicate)
    {
        using(var session = this.sessionFactory.OpenSession())
        {
            return session.Query<T>().Where(predicate);
        }
    }
    public IQueryable<T> All()
    {
        using(var session = this.sessionFactory.OpenSession())
        {
            return session.Query<T>();
        }
    }
    public void Add(T entity)
    {
        // execute validation here?

        using(var session = this.sessionFactory.OpenSession())
        using(var transaction = session.BeginTransaction())
        {
            session.Save(entity);
            transaction.Commit();
        }
    }
    public void Remove(T entity)
    {
        using(var session = this.sessionFactory.OpenSession())
        using(var transaction = session.BeginTransaction())
        {
            session.Delete(entity);
            transaction.Commit();
        }
    }
    public void Update(T entity)
    {
        // make changes &
        // execute validation here?

        using(var session = this.sessionFactory.OpenSession())
        using(var transaction = session.BeginTransaction())
        {                
            session.Update(entity);
            transaction.Commit();
        }
    }
} 

I wanted to use FluentValidation to validate entities. As far as I understand within scope of repository there are only two places where validation should be made: when adding and updating an entity.

First I thought to put IValidator<T> as a parameter to base entity class:

public abstract class Entity<T> where T : Entity<T>
{
    // ...        
    public virtual String ValidationMessage
    {
        get;
        private set;
    }

    public virtual Boolean Validate(IValidator<T> validator)
    {
        try
        {
            validator.ValidateAndThrow(this as T);
            return true;
        }
        catch(ValidationException ex)
        {
            this.ValidationMessage = ex.Message;
            return false;
        }
    } 
    // ...      
} 

But it doesn’t seem to be right.

How do I make this right from design point of view? Any advice or tutorial is appreciated.

Thanks!

  • 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-27T20:57:33+00:00Added an answer on May 27, 2026 at 8:57 pm

    IMHO, the problem lies much before you reach the repository. The thing is that somewhere you allowed the creation of an invalid domain object and now you need to validate it to make sure you’re not persisting it unless its invariant is satisfied.

    Besides the technical aspect, this has deeper implications at the conceptual level of what you’re doing.
    Remember that an object models an entity of the real world: does it makes sense for, say, a person to have a negative age? We’ll both agree that no person exists with a negative age, thus remembering the mapping between real world and software, we get to see that no Person object should exists with a negative age because that model doesn’t have its counterpart in the world.

    Coming back to your scenario, think for a moment about the repository: does it fits the responsibility of entity validation in it? That seems much more than a repository should do.

    What I like to do is to validate all the arguments that make up a domain object before it gets created. In that way I make sure that, if the object was instantiated, then it must be a valid one. You can do the same whenever a message is sent to an object that will cause its state to change.

    About the validation, you have some choices:

    • Pass an instance of the validation class to the constructor of the object. It doesn’t feels right to have it passed around each time you need to validate it, because that would mean that potentially more than one class will have knowledge of the right IValidator<T> to use for validation.
    • Have a class in charge to build the object for you: make your constructor private, and use of static creation method to accomplish correct instantiation.

    It all depends on how complex the validation needs to be. Either way, you’ll want to encapsulate that decision so its not spread all over your codebase (instantiate a domain object in only one place).

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
i want to parse a xhtml file and display in UITableView. what is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.