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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:03:59+00:00 2026-05-28T14:03:59+00:00

Is there any way to achieve a generic factory when the class it returns

  • 0

Is there any way to achieve a generic factory when the class it returns requires a parameter in the constructor? All my factories look like this:

public static class UserServiceFactory
{
    public static UserService GetService()
    {
        UserRepository userRepository = new UserRepository();
        return new UserService(userRepository);
    }

}

I tried something like this:

 public static TServiceClass GetService<TServiceClass>()
        where TServiceClass : class, new()
    {
        TServiceClass serviceClass = null;

        string repositoryName = typeof (TServiceClass).ToString().Replace("Service", "Repository");
        Type type = Type.GetType(repositoryName);

        if (type != null)
        {
            object repository = Activator.CreateInstance(type);
            serviceClass =  (TServiceClass)Activator.CreateInstance(typeof (TServiceClass), new[]{repository});
        }

        return serviceClass;
    }

However, this of course won’t work because I can’t use a class that has no parameterless constructor as a generic parameter, but I feel like I am close. I thought about passing the Service class like GetService(Type serviceClassType) but then I can’t declare the method’s return type and would have to cast it when I call it, which I want to avoid.

Is there another approach? Is this even possible?

  • 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-28T14:04:00+00:00Added an answer on May 28, 2026 at 2:04 pm

    You could do something like this, and take both types as type parameters:

    public static TService GetService<TRepository, TService>() where TRepository:new()
    {
        var repository = new TRepository();
        return (TService)Activator.CreateInstance(typeof(TService), repository);
    }
    

    Or, if you want to rely on convention that the service and repository are the same name (just for illustration):

    public static TService GetService<TService>()
    {
        var repositoryName = String.Concat(typeof(TService).Namespace, Type.Delimiter, typeof(TService).Name.Replace("Service", "Repository"));
        object repository = Activator.CreateInstance(Type.GetType(repositoryName));
        return (TService)Activator.CreateInstance(typeof(TService), repository);
    }
    

    This feels not obvious and unintuitive, and perhaps there are a better, more robust solutions available.

    Perhaps using an inversion of control container would suit as a better approach.

    Using a container, I would just resolve UserService and let the container inject the appropiate repository. If UserService is a dependency from something else, let the DI container resolve that, and a modern container will resolve dependencies of dependencies.

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

Sidebar

Related Questions

I would like to ask is there any way to achieve this functionality: I
Is there any way to make a code like this work: public class Func2<A,
Is there any way to achieve function overloading in C? I am looking at
Is there any way to have something that looks just like a file on
Is there any way to force a listview control to treat all clicks as
Is there any way to achieve the specified behaviour? If there is some trick
Is there any way to achieve the master page concept of ASP.NET in PHP?
Is there any way in C# to pass a random method as a parameter?
Is there any way to perform a LIKE operation with XQuery in the same
Is there any way to force operator precedence in Scala like you do in

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.