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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:43:26+00:00 2026-05-27T01:43:26+00:00

Please find below my code. Employee class implements IEmployee interface. namespace MiddleWare.ServiceContracts { [ServiceContract(Namespace

  • 0

Please find below my code. Employee class implements IEmployee interface.

    namespace MiddleWare.ServiceContracts

    {
        [ServiceContract(Namespace = "http://mywebsite.com/MyProject")]

        public interface IMiscellaneous
        {
           [OperationContract]
            [ServiceKnownType(typeof(MiddleWare.Classes.Employee))]
            IEnumerable<IEmployee> Search_Employee
            (string SearchText);

    }


    namespace MiddleWare.ServiceClasses
    {
       public class Miscellaneous : IMiscellaneous
        {
           public IEnumerable<IEmployee> Search_Employee
            (string SearchText)
            {
               List<IEmployee> emp = new List<IEmployee>();

               IEmployee TempObject = (IEmployee)Activator.CreateInstance(typeof(IEmployee));  
               TempObject.EmployeeId = "12345678";

               emp.Add(TempObject);
              return emp;
           }
       }
    }

As is visible the above code does compile but wont work because interface instance cannot be created.How can I achive DI(Dependency Injection) here…If I write..

IEmployee TempObject = (IEmployee)Activator.CreateInstance(typeof(Employee));

Then this class will be dependent not only on the Interface but also the class…assuming that one fine day Employee class becomes Employee2.There will be code changes at two places..
1)[ServiceKnownType(typeof(MiddleWare.Classes.Employee2))]

2)IEmployee TempObject = (IEmployee)Activator.CreateInstance(typeof(Employee2));

I want to avoid that. Can we do something at implementation of IOperationBehavior or is there a Ninject way of achieving this or am I trying to achieve impossible?

  • 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-27T01:43:27+00:00Added an answer on May 27, 2026 at 1:43 am

    From your updated requirements, there is nothing related to DI in this question…

    So, to create a type based on the service known types of a service contract you can use:

    public class EntityLoader<TServiceContract>
        {
            private static readonly HashSet<Type> ServiceKnownTypes = new HashSet<Type>();
            static EntityLoader()
            {
                var attributes = typeof(TServiceContract).GetMethods().SelectMany(m => m.GetCustomAttributes(typeof(ServiceKnownTypeAttribute), true)).Cast<ServiceKnownTypeAttribute>();
                foreach (var attribute in attributes)
                {
                    ServiceKnownTypes.Add(attribute.Type);
                }
            }
    
            public TEntity CreateEntity<TEntity>()
            {
                var runtimeType = ServiceKnownTypes.Single(t => typeof(TEntity).IsAssignableFrom(t));
                return (TEntity)Activator.CreateInstance(runtimeType);
            }
        }
    

    Which is then useable like so:

        [ServiceContract(Namespace = "http://mywebsite.com/MyProject")]
        public interface IMiscellaneous
        {
            [OperationContract]
            [ServiceKnownType(typeof(Employee))]
            IEnumerable<IEmployee> SearchEmployee(string SearchText);
        }
    
        public class Miscellaneous : IMiscellaneous
        {
            private readonly EntityLoader<IMiscellaneous> _entityLoader = new EntityLoader<IMiscellaneous>();
            public IEnumerable<IEmployee> SearchEmployee(string SearchText)
            {
                List<IEmployee> employees = new List<IEmployee>();
    
                IEmployee employee = _entityLoader.CreateEntity<IEmployee>();
                employee.EmployeeId = "12345678";
    
                employees.Add(employee);
                return employees;
            }
        }
    

    Obviously, the above code assumes that ALL of your service entities will contain public parameterless constructors and that there will only be one ServiceKnownType that implements each interface.

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

Sidebar

Related Questions

Please find the below code <?xml version=1.0 encoding=utf-8?> <mx:Application xmlns:mx=http://www.adobe.com/2006/mxml layout=absolute> <mx:Script> <![CDATA[ [Bindable]
Please find my code below. My problem is that I the inner jQuery.get() doesn't
Please find below the piece of code that I use to tokenize a string.
Please find the below code i tried to add toolbar into the uipicker but
Please find the code below $(#chkCopy).toggle(function () { copyDetails(); }, function () { emptyCopyDetails();
I am using DB4O to store my objects. Please find below code to retrieve
I am using File::Find in the code below to find the files from /home/user/data
Run time error Cannot find column 0. below is my code string connectiostring =
Could you please help me find out the time complexity of the Fleury' algorithm
ereg and eregi functions will be deleted from Php. Please help to find alternatives

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.