English 中文(简体)
How to properly implement a statemachine/Factory with structuremap?
原标题:

Lets say i have a traditional statemachine/Factory implemented like

public class StateMachine
{
    public void ProcessState(StateEnum CurrentState)
    {
        switch (CurrentState)
        {
            case StateEnum.New:
                ProcessNewState();
                break;
            case StateEnum.Waiting:
                ProcessWaitingState();
                break;
            ++++ More states.....
        }
    }

    void ProcessWaitingState()
    {
        //Do State work
    }

    void ProcessNewState()
    {
        //Do State work
    }
}

But now i want to implement this using best practices (TDD, SOLID...) and structuremap.

To follow the Single responsibility principle I want to put the processing code for each state in separate classes. Also each class has their own set of unique dependencies and they can be pretty complex them self. So putting them in separate classes a good idea I think.

So, how I can i properly do this in Structuremap?

I know this is basically a Factory class that i want to convert to a Objectfactory.

I am using latest Structuremap version 2.54 and in this version a lot of things seems to be deprecated (or not recommended anymore), like the use of ForRequestedType. So I am looking for the correct way of thing this using the new notation.

最佳回答

Based on both your question and answer, you could use a string representation of the enum value as your name for the named instance, in the registry:

For<IStateWorker>().Add<WorkerState_NewState>().Named(StateEnum.New.ToString()); 

And in your factory code:

public void ProcessState(StateEnum CurrentState)  
{  
   Worker = ObjectFactory.GetNamedInstance<IStateWorker>(CurrentState.ToString());
   Worker.Execute();
}  

Consider the above to be pseudocode, as I obviously don t have your source code, and I normally use the "ForRequestedType<>().AddInstances()" syntax to register types.

Since your IStateWorkers are now managed by the container, you can add constructor dependencies to each one without having to worry about how they get resolved (outside of registering them, of course).

问题回答

I found what I was looking for

First I register using:

For<IStateWorker>().Add<WorkerState_NewState>().Named("new");
For<IStateWorker>().Add<WorkerState_WaitingState>().Named("waiting");

Then I call it using:

        Worker = ObjectFactory.GetNamedInstance<IStateWorker>("waiting");
        Worker.Execute();
        Worker = ObjectFactory.GetNamedInstance<IStateWorker>("new");
        Worker.Execute();

That works.

But my question remains if there s an better way of doing it?





相关问题
Structuremap (or any IoC, really) architecture question

I m going to use structuremap for a project I m working on. The basic gist is that I have a repository pattern with an NHibernate implementation, but I want to use StructureMap to load the ...

Inject static property with StructureMap?

is it possible inject static property, like I do below, because it does not work for me? public static IMerchantModule MerchantModule { get; set; } public RequestBaseValidationRules() { ...

Injecting Subsonic SimpleRepository class to controller

I m tryingot get started with IoC, I have an MVC project in which is use subsonic, I m trying to inject subsonic simplerepository to my controllers but I m getting this error: StructureMap Exception ...

StrucutureMap RhinoMock Record/Playback, Example needed

I m looking for some examples on how to do the following Mock Tests using StructureMap or Unity with NUnit. I have the following code structure public interface IDAL { List<Model> Method1(...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

热门标签