寻找有关我的服务器设计的一般反馈。关于使用通用方法 [英] Looking for some general feedback regarding my server design esp. regarding the use of a generic method

查看:237
本文介绍了寻找有关我的服务器设计的一般反馈。关于使用通用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是我之前发布的一个后续行为此处。马丁霍霍已要求我提供有关我的系统的更多信息。他有建议,可能会有一个更好的方式来实现我正在尝试的。所以,如果在这里有一个问题,我想我想知道这是否是糟糕的设计?如果是这样,什么可以改进和如何(我从插图中最好地学习)。谢谢。



我正在为iPhone应用程序设计中间件。



开发人员希望使用泛型,而Group则根据传入参数返回JSON字符串,而不是显式调用客户端中的各种对象。该参数表示用户在登录时看到的第一个屏幕。我们正在呼叫登录屏幕Dashboard。



所以,当客户端调用服务器方法时:

 合同.GroupDto IDashboardService.GetGroupById(string groupId)
{
var obj = GroupRepository.GetGroupById(groupId);

return new Contracts.GroupDto
{
...
};
}

服务器使用GroupRepository方法GetGroupById返回一个通用对象类型: p>

  public static IList< G> GetGroupById< G>(int groupId)
{

DashboardGroupType type =(DashboardGroupType)groupId;
IList< G> result = new List< G>();

var obj = default(G);

switch(type)
{
case DashboardGroupType.Countries:
break;
case DashboardGroupType.Customers:
//这返回一个名为IEnumerable< Customer>
obj =(G)CustomerRepository.GetAllCustomers();
break;
case DashboardGroupType.Facilities:
//这返回一个典型的IEnumerable< Facility>
obj =(G)FacilityRepository.GetAllFacilities();
break;
case DashboardGroupType.Heiarchy:
break;
case DashboardGroupType.Lines:
break;
case DashboardGroupType.Regions:
//这返回一个典型的IEnumerable< string>
obj =(G)CustomerRepository.GetRegionsHavingCustomers();
break;
case DashboardGroupType.States:
// //这返回一个名为IEnumerable< Customer>
obj =(G)CustomerRepository.GetStatesHavingCustomers();
break;
case DashboardGroupType.Tanks:
break;
默认值:
break;
}

result.Add(obj);


返回结果;

}

返回的对象类型基于传入的参数GetGroupById。例如,如果值为1,该方法将查看DashboardGroupType枚举:



并传递参数1,服务器查看以下枚举:

  public enum DashboardGroupType 
{
Countries = 0,
Regions = 1,
客户= 2,
设施= 3,
线= 4,
坦克= 5,
州= 6,
Heiarchy = 7
}

,并向呼叫客户端返回类型为IEnumerable的区域列表。



有关此设计的任何想法(特别是关于IList GetGroupById(int groupId)方法)?如果您有建议,我将很乐意解释您的改进。

提前感谢

解决方案

不要使用switch语句,它可能会引入错误并且很难维护
我建议你为GetGroupById()创建一个默认行为的基类(抽象),并为每个组DashboardGroupType派生类
当你只需要存储时,枚举是合适的作为一个变量/属性,不需要执行基于该值的任何逻辑
在你的派生类中,你可以覆盖默认行为或保持默认行为(返回null)你可能会想到使用null对象设计模式也是如此。



您将会欣赏设计,当有变化出现时,变化是唯一的常数:)


$ b $你可以参考Martin Fowler的重构


This questions is a follow-up to my previous post here. Martinho has asked me to provide more information regarding my system. He has suggestion that there might be a better way to achieve what I am attempting. So, if there is a question here, I guess I am wondering if this is poor design? If so, what can be improved and how (I learn best from illustrations). Thanks.

I am designing middleware for an iPhone application at work.

Rather than explicitly calling various objects from the client, the developers want to use generics where a "Group" returns a JSON string based on a passed-in parameter. The parameter represents the first screen a user sees when he logs in. We are calling the login screen the "Dashboard".

So, when the client calls the server method:

 Contracts.GroupDto IDashboardService.GetGroupById(string groupId)
        {
            var obj = GroupRepository.GetGroupById(groupId);

            return new Contracts.GroupDto
            {
                ...
            };
        }

The server uses the GroupRepository method GetGroupById to return a generic object type:

public static IList<G> GetGroupById<G>(int groupId)
        {

            DashboardGroupType type = (DashboardGroupType)groupId;
            IList<G> result = new List<G>();

            var obj = default(G);

            switch (type)
            {
                case DashboardGroupType.Countries:
                    break;
                case DashboardGroupType.Customers:
                    // this returns a list of typ  IEnumerable<Customer>
                    obj = (G) CustomerRepository.GetAllCustomers();
                    break;
                case DashboardGroupType.Facilities:
                    // this returns a list of typ  IEnumerable<Facility>
                    obj = (G) FacilityRepository.GetAllFacilities();
                    break;
                case DashboardGroupType.Heiarchy:
                    break;
                case DashboardGroupType.Lines:
                    break;
                case DashboardGroupType.Regions:
                    // this returns a list of typ  IEnumerable<string>
                    obj = (G) CustomerRepository.GetRegionsHavingCustomers();
                    break;
                case DashboardGroupType.States:
                    // // this returns a list of typ  IEnumerable<Customer>
                    obj = (G) CustomerRepository.GetStatesHavingCustomers();
                    break;
                case DashboardGroupType.Tanks:
                    break;
                default:
                    break;
            }

            result.Add(obj);


            return result;

        }

The object type returned is based on the parameter passed in to GetGroupById. For example, if the value is 1, the method looks at the DashboardGroupType enum:

and passes the parameter of 1, the server looks at the following enum:

 public enum DashboardGroupType
    {
        Countries = 0,
        Regions = 1,
        Customers = 2,
        Facilities = 3,
        Lines = 4,
        Tanks = 5,
        States = 6,
        Heiarchy = 7
    }

and returns a list of regions of type IEnumerable to the calling client.

Any thoughts regarding this design (especially regarding the IList GetGroupById(int groupId) method? If you have suggestions, I would appreciate an illustration of your improvement.

Thanks in advance.

解决方案

Do not use switch statement. It may introduce bugs and hard to maintain. I suggest you to create a base class (abstract) with default behavior for GetGroupById() and derive classes for each of the groups DashboardGroupType. Enum are appropriate when you just need to store as a variable/property and don't have to do perform any logic based on the value. In your derived classes you can override the default behavior or keep it to default (return null). You may think of using null object design pattern as well.

You will appreciate the design when some change comes up and change is the only constant :)

You can refer to Martin Fowler's book on" Refactoring"

这篇关于寻找有关我的服务器设计的一般反馈。关于使用通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆