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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:42:48+00:00 2026-06-13T03:42:48+00:00

I have classes for entities like Ship , Shampoo , Horse etc and each

  • 0

I have classes for entities like Ship, Shampoo, Horse etc and each of them will have a manager class like ShipManager, ShampooManager, HorseManager etc. All the manager classes implement IManager and entities implement IEntity.

Now I have a static function written just to save the entities which would look like:

public static bool Save<S, T>(S entity, string msg) where S : IEntity 
                                                    where T : IManager<S>, new()
{
    string possibleError;
    switch (new T().Save(entity, out possibleError))
    {
        //---------------------
    }
    return true;
}

The inelegance here is that I have to call the Save like Save<Radio, RadioManager> etc. What I would love to have is something like this:

public static bool Save<T>(T entity, string msg) where T : IEntity
{
    string possibleError;
    switch ((/*find manager here*/).Save(entity, out possibleError))
    {
        //---------------------
    }
    return true;
}

There are few ways to do this, but I would love to have something that involves generics and type inference. I am thinking it would be good to couple the entity classes with corresponding manager classes by design to achieve some type safety as well.

Here is what I could do, with the help of an extension method:

public static bool Save<T>(this IManager<T> mgr, T entity, string msg) where T : IEntity
{
    string possibleError;
    switch (mgr.Save(entity, out possibleError))
    {
        //---------------------
    }
    return true;
}

So that I can call:

FooManager fMgr = new FooManager();
fMgr.Save(foo, "success");

But here I always need to instantiate the IManager and then call the Save method on its instance. I would love to avoid that much repetitive code and give the duty to a static function. How can I design the classes to have a relationship between IManager and IEntity so that manager of entity is automatically inferred?

Edit: Please note that I dont have the luxury to move the Save method in manager class to entity class (which would have made life easier). Our entire design is based on one entity class and a corresponding manager class. So I am thinking of sticking to it.

  • 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-13T03:42:50+00:00Added an answer on June 13, 2026 at 3:42 am

    Here is how I solved it.

    I created a function Manager in the IEntity like this:

    IManager<T> Manager<T>() where T : IEntity;
    

    Now whenever I implement IEntity in any of the entity classes, I am forced to implement their respective Manager as well. For eg.

    public class Foo : IEntity
    {
        public IManager<T> Manager<T>() where T : IEntity
        {
            return (IManager<T>)new FooManager();
        }
    }
    

    Now I can call:

    public static bool Save<T>(T entity, string msg) where T : IEntity, new()
    {
        string possibleError;
        switch (entity.Manager<T>().Save(entity, out possibleError))
        {
            //--------------------------------------
        }
        return true;
    }
    

    Not the best of designs, but this does the job..

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

Sidebar

Related Questions

I have two classes orchestrated by a main class and I would like to
I have classes for entities like Customer, InternalCustomer, ExternalCustomer (with the appropriate inheritance) generated
I have many repository classes that look like the following: public class ProfileRepository :
I have to 2 entities like this: class A { int id { get;
I have a list full of many classes and in each class I would
In one of my projects, I have some classes that represent entities that cannot
I have two classes that represent two different database entities. Their relationship is 1:m
In my program I have two classes: Collector and Entity. Collector stores Entities in
I have a few entities which are managed by custom NSManagedObject classes. They are
I am serializing Lists of classes which are my data entities. I have a

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.