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 148901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:02:28+00:00 2026-05-11T09:02:28+00:00

This is mainly a thought experiment. So this all is sample code. My goal

  • 0

This is mainly a thought experiment. So this all is sample code. My goal was to use the specification pattern to eliminate giant blocks of conditional code inside a factory. So with this sample I have a StatusData object that I want to get an implementation of IStatusUpdate that is appropriate for for it.

I have the following set of tests:

    [TestMethod]     public void Factory_Interface_Should_Return_IStatusUpdate()     {       var factory = MockRepository.GenerateMock<IUpdateFactory<StatusData>>();       var obj = MockRepository.GenerateStub<IStatusUpdate>();        var data = new StatusData();       factory.Stub(x => x.Get(data)).Return(obj);        var item = factory.Get(data);        Assert.IsInstanceOfType(item, typeof(IStatusUpdate));     }      [TestMethod]     public void StatusUpdateFactory_Should_Return_IStatusUpdate()     {       var factory = new StatusUpdateFactory();       var data = new StatusData();        var item = factory.Get(data);        Assert.IsInstanceOfType(item, typeof(IStatusUpdate));        }      [TestMethod]     public void StatusUpdateFactory_Should_Return_NewStatusUpdate_When_Status_Is_New()     {       var data = new StatusData(Status.New);       var factory = new StatusUpdateFactory();        var item = factory.Get(data);        Assert.IsInstanceOfType(item, typeof(NewStatusUpdate));     } 

My Factory implementation so far looks like this:

public class StatusUpdateFactory:IUpdateFactory<StatusData>   {     public IStatusUpdate Get(StatusData item)     {       IList<ISpecification<StatusData>> specs = GetSpecifications();        foreach (var spec in specs)       {         if (spec.IsSatisfiedBy(item))           //how do I do this?            return new NewStatusUpdate();        }       return null;     }      private IList<ISpecification<StatusData>> GetSpecifications()     {       var returnList = new List<ISpecification<StatusData>>();       var specTypes = this.GetType().Assembly.GetTypes()                         .Where(z => z.IsInstanceOfType(typeof(ISpecification<StatusData>)))                         .ToList();         specTypes.ForEach(x => returnList.Add(Activator.CreateInstance(x) as ISpecification<StatusData>));        return returnList;      }   } 

Where I am falling down is once I have discovered a specification that is satisfied by the status object, how do I map that specification to a type that implements IStatusUpdate.. I am stumped.

Someone rightly suggested that I need a mapping of specifications to IStatusUpdate implementers. This mapping seems to be a responsibility of the factory, hanging it off the specification smells like a violation of SRP. I could create a Mapper class that has that responsibility but that doesn’t seem very generic and also raises the question how do I map the mapper to the spec.

There is still one little leap here I am missing.

  • 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. 2026-05-11T09:02:29+00:00Added an answer on May 11, 2026 at 9:02 am

    So I’m assuming we’re really focusing on this set of lines:

    if (spec.IsSatisfiedBy(item))           return new NewStatusUpdate(); 

    and I assume you’re asking how in this current form this can be done. It seems like item should support like

    interface ISpecSupport<T> {     bool ItemSpecsContain(ISpecification<T> spec); } 

    Then the spec.IsSatisfiedBy method can take in this interface type and run the method.

    In other words I guess I’m saying that the object should carry some sort of description of what it is (in terms of specs). I’m guessing that’s a list of some sort but I’m not sure. I’m sure you’ve probably thought of this so if you can add anything that’d be helpful.

    Also, instead of the above maybe you could rearrange it like so:

    if (item.Satisfies(spec))     return new NewStatusUpdate(); 

    Then this way you don’t have to use the much maligned visitor pattern (I think that’s what I was describing before this). It’s more direct since the item seems like it would own the specs and this way you’re allowing the item to decide if it meets the spec.

    If you don’t want this logic held within the object (which I would understand) AND you are using a property bag of some sort (or you’re cool with reflection) you could dig into the details of the object with an independent spec validator. Actually, an independent validator might not be a bad idea to begin with. I’m not so sure that the ability to know if a spec matches an item is a responsibility that should remain with an individual spec.

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

Sidebar

Ask A Question

Stats

  • Questions 206k
  • Answers 206k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, first off, the grammar is used to construct a… May 12, 2026 at 9:05 pm
  • Editorial Team
    Editorial Team added an answer Informational messages (like the rows affected count info) are reported… May 12, 2026 at 9:05 pm
  • Editorial Team
    Editorial Team added an answer Steven Colebourne, the author of Joda time is the spec… May 12, 2026 at 9:05 pm

Related Questions

This is mainly a performance questions. I have a master list of all users
(Edited to add an example and hopefully make it a bit clearer) I'm mainly
I'm looking for a class where I can override a method to do the
I'm writing an iPhone app that is mainly centered around grid patterns, so I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.