简体   繁体   中英

Return object and List<object> from using Factory pattern in c#

I have two classes

public class LoginModel : IBaseInterface
{
   public string EmailAddress { get; set; }
   public string Password { get; set; }
}

and

public class UsersModel : IBaseInterface
{
    public int UserID { get; set; }
    public string UserName { get; set; }
    public string EmailAddress { get; set; }
    public int RoleID { get; set; }
    public string RoleName { get; set; }
    public int ProjectID { get; set; }
    public string ProjectName { get; set; }
    public int PMID { get; set; }
    public string PMEmailAddress { get; set; }
    public string PMEmailName { get; set; }
    public int DMID { get; set; }
    public string DMEmailAddress { get; set; }
    public string DMEmailName { get; set; }
}

IBaseInterface is an intergace and has nothing in definition.

public static IBaseInterface Create(ObjectsCollection Type)
    {
        switch (Type)
        {
            default:
                return null;
            case ObjectsCollection.UsersModel:
                return new UsersModel();
            case ObjectsCollection.LoginModel:
                return new LoginModel();
        }
    }

Everything was working fine for objects. But how to return a List<UsersModel> and List<LoginModel> using this Factory?

Would be nice if someone please help me to resolve this issue.

Thanks

Test this...I hope it helps

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var type1 = Create<LoginModel>().InternalList;

            if (type1 != null)
                Console.WriteLine("OK");

            Console.WriteLine("Finish");

            Console.ReadLine();
        }

        public static T Create<T>() where T : AbstractFactory
        {
            switch (typeof(T).Name)
            {
                default:
                    return default(T);

                case "UsersModel":
                    return new UsersModel() as T;

                case "LoginModel":
                    return new LoginModel() as T;
            }
        }
    }

    internal abstract class AbstractFactory : IBaseInterface
    {
        public List<IBaseInterface> InternalList { get; set; }
    }

    internal interface IBaseInterface
    {
    }

    internal class UsersModel : AbstractFactory
    {
        public UsersModel()
        {
            InternalList = new List<IBaseInterface>();
        }

        public int UserID { get; set; }
        public string UserName { get; set; }
        public string EmailAddress { get; set; }
        public int RoleID { get; set; }
        public string RoleName { get; set; }
        public int ProjectID { get; set; }
        public string ProjectName { get; set; }
        public int PMID { get; set; }
        public string PMEmailAddress { get; set; }
        public string PMEmailName { get; set; }
        public int DMID { get; set; }
        public string DMEmailAddress { get; set; }
        public string DMEmailName { get; set; }
    }

    internal class LoginModel : AbstractFactory
    {
        public LoginModel()
        {
            InternalList = new List<IBaseInterface>();
        }

        public string EmailAddress { get; set; }
        public string Password { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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