0

I have different services which each one has different types. For example UpdateService has three different types (HighFreq,MediumFreq,MinFreq).

I have made an enumaration for my services and my types for example:

 public enum UpdateServiceType: byte
  {

    MinUpdateFreq= 1,

    MediumUpdateFreq = 2,

    HighUpdateFreq = 3

   }

and

public enum MyServices: byte
 {

    UpdateService = 1,

    SpecialService= 2,

    }

Then on conditions on saved the service and associated type on database like :

serviceId= 1
ServiceType =2

Now I want to create each service but I don't know which pattern to use : factory pattern, service locator, or even I don't whether I have done the right thing to use enum or not?

what should i use instead of enum? How should i create the appropriate service? what are the best practices? How can I use an ioc like unity in this situation?

Update:

After Reading Great post by Mark Seeman I find Metadata Role Hint appropriate for my problem because it explicitly describes the class. But then again I came to another problem. the factory that mark suggested was something like this:

public class UpdatePackageFactory : UpdatePackageFactory
{
    private readonly IEnumerable<IUpdatePackage> _candidates;

    public UpdatePackageFactory(IEnumerable<IUpdatePackage> candidates)
    {
        _candidates = candidates;
    }

    public IUpdatePackage GetUpdatePackage(UpdateFreq UpdateFreq)
    {
        return (from c in this._candidates
                let handledMethod = c.GetType().GetCustomAttribute<HandleUpdateFreqAttribute>()
                where handledMethod != null && handledMethod.UpdateFreq == UpdateFreq
                select c).SingleOrDefault();
    }


}

it takes and enum like

public enum UpdateFreq : byte
{

    MinUpdateFreq= 1,

    MediumUpdateFreq = 2,

    HighUpdateFreq = 3

}

But now I want to make another factory which makes the above factory the factory it self is dependent on an enum like

 public enum ServiceFactory: byte
{

    UpdateService= 1,

    SpecialService = 2,

    HighUpdateFreq = 3

}

Using the above mentioned method I can make another factory that builds factory but the problem is each factory receives its own enum How to make factories that accept different enums. for example the would be a method on my factory which takes two enums as argument the first one would always be myservices but the second argument would change proportional to which factory it is. one time it may be updateFreq ,another time specialType or .....

  public IUpdatePackage GetUpdatePackage(MyServices myServices,UpdateFreq updateFreq )
    {

    }

Or making another example here Abstract Factory factories have enum

enum MANUFACTURERS
{
    NOKIA,
    SAMSUNG,
    HTC,

 }

but what if each factory then again has its own enum for example NokiFactory accept an enum with different items on it an Htc accept another enum with different naming an so on.

How should I write such factory?

Amir Jalali
  • 3,132
  • 4
  • 33
  • 46
  • 1
    Related: http://stackoverflow.com/a/22705794/126014 – Mark Seemann Apr 28 '14 at 16:23
  • See also: http://stackoverflow.com/a/1927167/126014 – Mark Seemann Apr 28 '14 at 16:26
  • @MarkSeemann I read your great post on this subject and several others like the one that kelly sommers had similar problem http://blog.ploeh.dk/2011/12/19/FactoryOverload/ but it didnt came up with a solution that shows how to achieve this with two different enums – Amir Jalali Apr 30 '14 at 05:47
  • 1
    I'm not sure I understand how that problem is different than the first one... However, in general, consider designing your system so that it relies on polymorphic types instead of enums: http://stackoverflow.com/a/2268571/126014 Also, you could always employ the Abstract Factory pattern: http://stackoverflow.com/a/2269105/126014 – Mark Seemann Apr 30 '14 at 07:00

0 Answers0