© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
M B V R K

DDD Specification with AND Operator

Hi dear friends,
Sorry because I know this topic is a bit of pain,

I'm working with
DDD
DDD
, as you know DDD came with a useful pain solution called
Specifications
Specifications
,
Now I have the following interface that represents a Specification:
using System.Linq.Expressions;

namespace ExpenseService.Domain.Shared.Interfaces;

/// <summary>
/// Represents a specification that defines a condition that an entity must satisfy.
/// </summary>
/// <typeparam name="T">The type of entity.</typeparam>
public interface ISpecification<T> where T: class
{
    bool IsSatisfiedBy(T entity);

    Expression<Func<T, bool>> ToExpression();
    
    ISpecification<T> And(ISpecification<T> other);
}
using System.Linq.Expressions;

namespace ExpenseService.Domain.Shared.Interfaces;

/// <summary>
/// Represents a specification that defines a condition that an entity must satisfy.
/// </summary>
/// <typeparam name="T">The type of entity.</typeparam>
public interface ISpecification<T> where T: class
{
    bool IsSatisfiedBy(T entity);

    Expression<Func<T, bool>> ToExpression();
    
    ISpecification<T> And(ISpecification<T> other);
}


And I have created an
Abstract class
Abstract class
who represents this specification as the following:
using System.Linq.Expressions;
using ExpenseService.Domain.Shared.Interfaces;

namespace ExpenseService.Domain.Specifications;


public abstract class Specification<T> : ISpecification<T> where T : class
{
    
    public abstract Expression<Func<T, bool>> ToExpression();


    public virtual bool IsSatisfiedBy(T entity)
    {
        var predicate = ToExpression().Compile();
        return predicate(entity);
    }
    
    
    public ISpecification<T> And(ISpecification<T> other)
    {

    }
}
using System.Linq.Expressions;
using ExpenseService.Domain.Shared.Interfaces;

namespace ExpenseService.Domain.Specifications;


public abstract class Specification<T> : ISpecification<T> where T : class
{
    
    public abstract Expression<Func<T, bool>> ToExpression();


    public virtual bool IsSatisfiedBy(T entity)
    {
        var predicate = ToExpression().Compile();
        return predicate(entity);
    }
    
    
    public ISpecification<T> And(ISpecification<T> other)
    {

    }
}


The issue:
The issue is that
And
And
method I don't know how to well implement it, I will do a brief explaination,
All I want that method do is to take an
ISpecification<T>
ISpecification<T>
and returns another/ a new
ISpecification<T>
ISpecification<T>
that represents the First specification's expression combined with the second Specification's expression by
AND
AND
operator.

I have tried the
Github Copilot
Github Copilot
but it suggests on me to create a new specification called
AndSpecification
AndSpecification
and that method should returns a new instance of that
AndSpecification
AndSpecification
.

From your experience, what is the ideal/efficient implementation for that
And
And
method ??
Massive thanks in advance <3
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Is this DDD Specification a good practice or not?
C#CC# / help
3y ago
❔ DDD: How to apply a Domain specification on a Infrastructure EF Core?
C#CC# / help
3y ago
hangfire in DDD
C#CC# / help
11mo ago
❔ CRUD(REST) + DDD
C#CC# / help
3y ago