简体   繁体   English

C#使用Activator.CreateInstance工厂模式如何

[英]C# Using Activator.CreateInstance factory pattern how to

I have user class say, User 我有用户类说,用户

public class User
{
public int Id {get;set;}   
public string Name {get;set;}

}

and an interface which returns a group of users by looking at the given user's Id 以及通过查看给定用户的Id来返回一组用户的界面

public intervace IPeople
{
   IList<User> GetPeople (int id);
   string Key {get;}
}

Here are few implementations of IPeople 以下是IPeople的一些实现

public class Friends : IPeople
{
   public IList<User> GetPeople (int id)
   {
      return ListOfUsersWhoAreFriends();
   }

    public string Key {get { return "Friends"; }
}


public class Fans : IPeople
{
   public IList<User> GetPeople (int id)
   {
      return ListOfFans();
   }
   public string Key {get { return "Fans"; }

}

Now in my calling method 现在在我的调用方法中

 string key="SomeKey" ; //It could be friends or fans
 int id =1;
 IPeople[] allPeople = GetAllInstancesOf<IPeople>();

 IList<User> requiredUsers = allPeople.FirstOrDefault(m=>m.Key == key).GetPeople(id);

So to say, I create instances of all the derived classes and then check which one I need, before I call the function on it. 所以说,在我调用函数之前,我创建所有派生类的实例,然后检查我需要哪一个。 Its working fine, but I feel, from performance point of view, its not a very good solution, as I create instance by reflection. 它的工作正常,但我觉得,从性能的角度来看,它不是一个非常好的解决方案,因为我通过反射创建实例。 What is the better way to achieve it. 实现它的更好方法是什么。 Some factory method If yes, how ?? 一些工厂方法如果是,如何?

Help will be appreciated. 帮助将不胜感激。

Regards 问候

Parminder 成员Parminder

I am guessing you are using reflection in your GetAllInstancesOf class? 我猜你在GetAllInstancesOf类中使用反射? Not sure where you are populating your list of People from or why you need a key and an Id, but for a factory pattern in this instance, I would use the following 不确定在哪里填充人员列表或者为什么需要密钥和Id,但对于此实例中的工厂模式,我会使用以下内容

public IList<User> UserFactory(string kindOfPeople)
{
  switch (kindOfPeople.ToLower())
  {
    case "fans":
      return ListOfFans();
    case "friends":
      return ListOfUsersWhoAreFriends();
    default:
      return new List<User>();
  }
}

If the kinds of people (fans, friends, family etc.) is limited and doesn't change very often in your application, then I would make it an enum and pass that into the factory 如果人们(粉丝,朋友,家人等)的种类有限并且在您的申请中不经常更改,那么我会把它变成一个枚举并将其传递到工厂

public enum KindsOfPeople { Friends = 0, Fans = 1}

PS: This assumes that both Fans and Friends derive from the User class. PS:这假设Fans和Friends都来自User类。 This wasn't shown in your code. 这未在您的代码中显示。 A nice way of using the factory pattern is if all the possible classes that are returned from the factory inherit from a common base class. 使用工厂模式的一种好方法是,从工厂返回的所有可能的类都继承自公共基类。

I feel, I found a better solution. 我觉得,我找到了更好的解决方案。 Here it goes. 在这里。

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public interface IPeople
{
    IList<User> GetPeople(int id);

}


public class Friends : IPeople
{
    public IList<User> GetPeople(int id)
    {
        return new List<User>();
    }



    public class Fans : IPeople
    {
        public IList<User> GetPeople(int id)
        {
            return new List<User>();
        }
    }


    public class Program
    {
        static void Main(string[] args)
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<Friends>().Named<IPeople>("Friends");
            builder.RegisterType<Fans>().Named<IPeople>("Fans");

            IContainer c = builder.Build();

            var x = c.ResolveNamed<IPeople>("Friends");

        }
    }
}

As I am using Autofac, I looks after the register with named keys. 当我使用Autofac时,我使用命名键来查看寄存器。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM