Implement a small amount of IOC for MVC 3

I am working on a design that will allow me to mock my database so that I can test my views. I do not want to read the full book about the IOC, because now I do not have time. So this is my home cooking.

Controller:

    public ActionResult Milestone()
    {
        var result = SJMServiceFactory.GetService("Milestone");
        return View(result);
    }

Factory:

public static class SJMServiceFactory
{
    public static DatabaseCollection_Result<T> GetService(string serviceName)
    {
        switch(serviceName)
        {
            case("Milestone"): return MileStoneService.GetMilestone();
            case ("MilestoneMock"): return MileStoneService.GetMilestone(true);
            default : return default(T);
        }
    }
}

Milestone

public class MileStoneService
{
    public MileStoneService()
    {

    }
    public static DatabaseCollection_Result<Milestone> GetMilestone(bool Mock)
    {
        if (Mock)
        {
            DatabaseCollection_Result<Milestone> mileStones = new DatabaseCollection_Result<Milestone>();
            Milestone milestone1 = new Milestone();
            milestone1.Name = "New";
            Milestone milestone2 = new Milestone();
            milestone2.Name = "Assessment";
            mileStones.Results.Add(milestone1);
            mileStones.Results.Add(milestone2);
            return mileStones;
        }
        else
            return null;
    }
}

I suppose I need to return an interface from my factory instead of this Generic type, which I tried and did not execute. I don’t know how to create an interface that works for all my models, is this the wrong direction?

+3
source share
2 answers

Do not be afraid of the learning curve. IoC is a fairly simple concept.

Ninject , , . , - , - , .

YMMV, , Ninject - , DIY.

IoC Ninject .

1: :

public class ServiceModule : NinjectModule
{
    public override void Load() {
        Bind<Common.Billing.AuthorizeNet.IBillingGatewayParametersFactory>().To<AuthorizeNetParameterFactory>();
        Bind<Common.Billing.IBillingGateway>().To<Common.Billing.AuthorizeNet.BillingGateway>();
    }
}

2: :

public class BillPayingService
{
    private readonly IBillingGateway _billingGateway;

    public BillPayingService(
            IBillingGateway billingGateway
        )
    {
        _billingGateway = billingGateway;
    }

    public void PayBills()
    {
       // ....
    }
}

3: :

public static class Ioc
{
    public static void Initialize()
    {
        var modules = new INinjectModule[] {
            new ServicesModule(),
            new DataModule()
                            new VideoProcessing.NinjectModule()
        };


        IKernel kernel = new Ninject.StandardKernel(modules);
    }
}
+5

( - IoC - ):

:

private readonly IMilestoneService milestoneSerivce;

public MilestoneController(IMilestoneService milestoneService)
{
    this.milestoneService = milestoneService;
}

public ActionResult Milestone()
{
    var result = milestoneService.GetMilestones();
    return View(result);
}

IMilestoneService.cs

public interface IMilestoneService
{
    DatabaseCollection_Result<Milestone> GetMilestones();
}

MilestoneService.cs

public class MilestoneService : IMilestoneService
{
    public DatabaseCollection_Result<Milestone> GetMilestones()
    {
        return null;
    }
}

MockMilestoneService.cs:

public class MockMilestoneService : IMilestoneService
{
    public DatabaseCollection_Result<Milestone> GetMilestones()
    {
        DatabaseCollection_Result<Milestone> mileStones = new DatabaseCollection_Result<Milestone>();
        Milestone milestone1 = new Milestone();
        milestone1.Name = "New";
        Milestone milestone2 = new Milestone();
        milestone2.Name = "Assessment";
        mileStones.Results.Add(milestone1);
        mileStones.Results.Add(milestone2);
        return mileStones;
    }
} 

Global.asax:

ObjectFactory.Configure(x => {
    x.For<IMilestoneService>().Use<MilestoneService>();
    // uncomment for test
    //x.For<IMilestoneService>().Use<MockMilestoneService>();
});

StructureMap, , Ninject . Ninject, , , - :

Bind<IMilestoneService>().To<MilestoneService>();

, , , ​​ Moq, , Assertions of the ViewResult .

, ok.

+7

Source: https://habr.com/ru/post/1792669/


All Articles