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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:31:36+00:00 2026-06-10T11:31:36+00:00

I have a complex entity ( Message ) that has quite a hefty constructor

  • 0

I have a complex entity (Message) that has quite a hefty constructor (13 parameters), some of these parameters are in turn entities (Verb).

There are a number of things I need a repository of another Entity for (e.g. for some defaults).

What is a good way of using IoC and keeping the code clean / best practice?

Simplified code (I’d put the default verb list as a static object constructed by the static constructor personally, its the simplest if naff example of why my Message entity needs some sort of IRepositry).

private static IVerbRepository verbRepository;

static Message()
{
    using (IKernel kernel = new StandardKernel())
    {
        verbRepository = kernel.Get<IVerbRepository>();
    }
}

public Message(int id, string precis,
    DateTime created, DateTime validTo,
    PriorityType priority, Source source, SID createdBy,
    string content = null, string helpText = null,
    DateTime? validFrom = null, bool emailOnExpiry = false,
    IEnumerable<SID> targetUsers = null,
    IOrderedEnumerable<MessageVerb> verbs = null) : base(id)
{
    Verbs = verbs ??
            new List<MessageVerb>
            {
                new MessageVerb(
                verbRepository.GetByName("Acknowledge"), true,
                string.Empty)
            }.OrderBy(x => x.IsDefault);

    Precis = precis;
    Created = created;
    ValidTo = validTo;
    Priority = priority;
    Source = source;
    CreatedBy = createdBy;
    Content = content;
    HelpText = helpText;
    ValidFrom = validFrom;
    EmailOnExpiry = emailOnExpiry;
    TargetUsers = targetUsers;
}

The common practice seems to be to add an additional parameter to the constructor. I don’t get how this is better? It means everytime you want to create a message, you need to write code to retrieve the repository. Assumning you wrap this around in a Factory, it doesn’t make sense to allow (logically if not actually) have different repositories for message entites?

Edit 1

Based on the first solutions the code would like this,

public Message(IVerbRepository verbRepository ...) {  }

Elsewhere in the same domain assembly

public MessageService
{
    private IVerbRepository VerbRepository {get; set;}
    public static IOrderedEnumerable<MessageVerb> DefaultVerb {get;}
}

Much higher up outside of my DDD (let’s say my web services that accept creations of messages)

public class MessageWebService
{
    private static IVerbRepository _verbRepository;

    public void AddMessage(some parameters)
    {
        var message = new Message(_verbRepository, ...);
    }
}
  • 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-06-10T11:31:38+00:00Added an answer on June 10, 2026 at 11:31 am

    There’re two flavors of IoC: Service Locator, which you’re using, and Dependency Injection, which you’re asking about.

    As of now, Service Locator is considered a kind of anti-pattern, because it introduces implicit dependencies (dependencies you can’t figure out by looking at class’s public interface) and makes testing harder.

    Dependency Injection, on the other hand, does not have these problems, but tends to be more verbose in class declaration.

    Now, regarding your question.

    I don’t get how this is better?

    It is explicit. You can see that your class depends on this or that service. BTW, your class doesn’t, it only uses verb repository for locating default verb (which can be easily moved outside of class).

    Assumning you wrap this around in a Factory, it doesn’t make sense to allow (logically if not actually) have different repositories for message entites?

    It does when you think of testing. You should be easily able to substitute any service your CUT is depending on with testable implementation (stub).

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

Sidebar

Related Questions

I have some complex message to show inside of a flash message so instead
I have a Entity Framework data mode that I insert to it some stored
I have a complex data structure along with some other data fields that used
I have quite a complex entity structure where several classes inherit from a base
We have a complex app that serves AJAX JSON streams (using ADO to grab
I have a complex RIA client that communicates with a WCF SOAP web service,
I have some complex regular expressions which I need to comment for readability and
I have a project containing POCO entities. A database context has been created for
I have an entity framework Complex Type(Activity) I have mapped to a SP which
The goal is to retrieve all users (not messages) that have sent a message

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.