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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:56:47+00:00 2026-06-13T12:56:47+00:00

Say I have a lot of boilerplate code like this: class MyClass { private

  • 0

Say I have a lot of boilerplate code like this:

class MyClass
{
    private readonly IDependencyA dependencyA;
    private readonly IDependencyB dependencyB;

    public MyClass(
        IDependencyA dependencyA,
        IDependencyB dependencyB)
    {
        if(dependencyA == null) throw ArgumentNullException("dependencyA");
        if(dependencyB == null) throw ArgumentNullException("dependencyB");
        this.dependencyA = dependencyA;
        this.dependencyB = dependencyB;

        ...
    }

    ...

    public void SomeMethod()
    {
        this.dependencyA.DoSomething(this.dependencyB);
    }
}

Is there a way to use something like PostSharp to remove the boilerplate code and make it look like this:

class MyClass
{
    [ConstructorParametersAreClassMembers]
    public MyClass(
        IDependencyA dependencyA,
        IDependencyB dependencyB)
    {
        ...
    }

    ...

    public void SomeMethod()
    {
        this.dependencyA.DoSomething(this.dependencyB);
    }
}

Is that even possible?

Aside: this is actually the way it works by default in F#.

  • 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-13T12:56:48+00:00Added an answer on June 13, 2026 at 12:56 pm

    This is possible, but not a very attractive method. I understand the pain. I find C# at this point a little to verbose. When applying the SOLID principles you get many small and focused classes. Since they are small, the overhead of writing constructors gets bigger.

    But instead of using a code weaving tool such as PostSharp, you can also create a T4 template that generates the constructor for you. There is a T4ConstructorGenerator NuGet package that adds a T4 template to your project, which generates a constructor for you.

    With this template the following class:

    public class SomeService
    {
        private readonly ITimeProvider timeProvider;
        private readonly ILogger logger;
        private readonly IOrderCalculator calculator;
        private readonly IMailSender mailSender;
    
        public void SomeMethod()
        {
            // using the dependencies
        }
    }
    

    Will get the following constructor:

        public SomeService(
            ITimeProvider timeProvider,
            ILogger logger,
            IOrderCalculator calculator,
            IMailSender mailSender)
        {
            if (timeProvider == null) throw new ArgumentNullException("timeProvider");
            if (logger == null)  throw new ArgumentNullException("logger");
            if (calculator == null) throw new ArgumentNullException("calculator");
            if (mailSender == null) throw new ArgumentNullException("mailSender");
    
            this.timeProvider = timeProvider;
            this.logger = logger;
            this.calculator = calculator;
            this.mailSender = mailSender;
    
            this.OnCreated();
        }
    
        partial void OnCreated();
    

    The template does this by adding a partial class, so the rest original code will not be affected. The template will only add a constructor when:

    • The class (and its corresponding partial classes) does not have any constructor defined.
    • The class is not static.
    • The class contains one or more private fields.

    In the case where your constructors often contain extra initialization (which in fact should be rare when doing Dependency Injection), you can simply implement the partial OnCreated method in the real class, as follows:

    partial void OnCreated()
    {
        // do logic here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a lot and getting really mixed results. Say I have code like
is it possible to do something like this: Let's say I have a lot
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have this entity with a lot of attributes. In the input form
I have an object in C# like this: private ClassWidget { public int ID;
Let's say I have a lot of Javascript code that will be deployed inside
lets say i have a main form which have a lot of functionallity. this
Say you have a lot of (key, value) objects to keep track of, with
Let's say we have a lot of to dos. We use $('body').on('click', '.todo', do_stuff)
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:

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.