strategy for choosing proper object and proper method

Posted by zerkms on Stack Overflow See other posts from Stack Overflow or by zerkms
Published on 2010-04-15T04:14:04Z Indexed on 2010/04/15 5:03 UTC
Read the original article Hit count: 349

Filed under:
|

in the code below at first if statements block (there will be more than just "worker" condition, joined with else if) i select proper filter_object. After this in the same conditional block i select what filter should be applied by filter object. This code is silly.

public class Filter
{
    public static List<data.Issue> fetch(string type, string filter)
    {
        Filter_Base filter_object = new Filter_Base(filter);

        if (type == "worker")
        {
            filter_object = new Filter_Worker(filter);
        }
        else if (type == "dispatcher")
        {
            filter_object = new Filter_Dispatcher(filter);
        }

        List<data.Issue> result = new List<data.Issue>();

        if (filter == "new")
        {
            result = filter_object.new_issues();
        }
        else if (filter == "ended")
        {
            result = filter_object.ended_issues();
        }

        return result;
    }
}

public class Filter_Base
{
    protected string _filter;

    public Filter_Base(string filter)
    {
        _filter = filter;
    }

    public virtual List<data.Issue> new_issues()
    {
        return new List<data.Issue>();
    }

    public virtual List<data.Issue> ended_issues()
    {
        return new List<data.Issue>();
    }
}

public class Filter_Worker : Filter_Base
{
    public Filter_Worker(string filter) :
        base(filter)
    { }

    public override List<data.Issue> new_issues()
    {
        return (from i in data.db.GetInstance().Issues
                where (new int[] { 4, 5 }).Contains(i.RequestStatusId)
                select i).Take(10).ToList();
    }
}

public class Filter_Dispatcher : Filter_Base
{
    public Filter_Dispatcher(string filter) :
        base(filter)
    { }
}

it will be used in some kind of:

Filter.fetch("worker", "new");

this code means, that for user that belongs to role "worker" only "new" issues will be fetched (this is some kind of small and simple CRM). Or another:

Filter.fetch("dispatcher", "ended"); // here we get finished issues for dispatcher role

Any proposals on how to improve it?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#