开发者

How can I do this? I need to call a Service Class but I need to pass in parameters

开发者 https://www.devze.com 2023-03-06 14:14 出处:网络
I need to call something in my application start to start my quartz.net scheduler. The problem is that I need to pass in a repository into my service layer that normally is done with ninject and depe

I need to call something in my application start to start my quartz.net scheduler.

The problem is that I need to pass in a repository into my service layer that normally is done with ninject and dependency injection.

//global.aspx

 public class MvcApplication : System.Web.HttpApplication
    {
        private readonly IScheduledRemindersService scheduledRemindersService;

        public MvcApplication(IScheduledRemindersService 
            scheduledRemindersService)
        {
            this.scheduledRemindersService = scheduledRemindersService;
        }

        protected void Application_Start()
        {
           //other default stuff here like mvc routers.
            scheduledRemindersService.RemindersSchedule();

        }
 }

    private readonly IReminderRepo reminderRepo;
    public ScheduledRemindersService(IReminderRepo reminderRepo)
    {
        this.reminderRepo  = reminderRepo;
    }


private readonly IReminderRepo reminderRepo;


    public ScheduledRemindersService(IReminderRepo reminderRepo)
    {
        this.reminderRepo = reminderRepo;
    }

I have NHibernate set to so when It seems IReminderRepo it shoudl bind it and in IReminderRepo I have

private readonly ISession session;

public ReminderRepo(ISession session)
{
    this.session = session;
}

This will also get automatically binded through nhibernate.

This won't work though as the global.aspx only allows no argument constructors.

So how can inject the right classes to these interfaces? Especially the nhibernate session that is the most important thing I need.

Edit

public class NhibernateSessionFactoryPro开发者_如何学JAVAvider : Provider<ISessionFactory>
    {   
        protected override ISessionFactory CreateInstance(IContext context)
        {
            var sessionFactory = new NhibernateSessionFactory();
            return sessionFactory.GetSessionFactory();
        }
    }

  public class NhibernateModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ISessionFactory>().ToProvider<NhibernateSessionFactoryProvider>().InSingletonScope();
            Bind<ISession>().ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();
        }
    }

// in the global.aspx

   protected IKernel CreateKernel()
    {
        var modules = new INinjectModule[]
                          {
                             new NhibernateModule(),
                             new ServiceModule(),
                             new RepoModule(),
                          };

        return new StandardKernel(modules);
    }

// in RepoModule()

  Bind<IReminderRepo>().To<ReminderRepo>();

// in serviceModule

   Bind<IScheduledRemindersService>().To<ScheduledRemindersService>()


We use a method like this in our Global.asax.cs file for exactly this purpose (it gets called from Application_Start:

    private static void SetupScheduling(IKernel kernel)
    {
        var scheduler = SchedulerUtil.Scheduler;
        scheduler.JobFactory = kernel.Get<IJobFactory>();
        scheduler.Start();
    }

And we have IJobFactory bound to the following class:

public class QuartzJobFactory : IJobFactory
{
    private readonly IObjectFactory<IJob> _jobFactory;
    private readonly LogService _logService;

    public QuartzJobFactory(IObjectFactory<IJob> jobFactory,
        LogService logService)
    {
        _jobFactory = jobFactory;
        _logService = logService;
    }

    /// <summary>
    /// Called by the scheduler at the time of the trigger firing, in order to
    ///             produce a <see cref="T:Quartz.IJob"/> instance on which to call Execute.
    /// </summary>
    /// <remarks>
    /// <p>It should be extremely rare for this method to throw an exception -
    ///             basically only the the case where there is no way at all to instantiate
    ///             and prepare the Job for execution.  When the exception is thrown, the
    ///             Scheduler will move all triggers associated with the Job into the
    ///             <see cref="F:Quartz.TriggerState.Error"/> state, which will require human
    ///             intervention (e.g. an application restart after fixing whatever 
    ///             configuration problem led to the issue wih instantiating the Job. 
    ///             </p>
    /// </remarks>
    /// <param name="bundle">The TriggerFiredBundle from which the <see cref="T:Quartz.JobDetail"/>
    ///             and other info relating to the trigger firing can be obtained.
    ///             </param><throws>SchedulerException if there is a problem instantiating the Job. </throws>
    /// <returns>
    /// the newly instantiated Job
    /// </returns>
    public IJob NewJob(TriggerFiredBundle bundle)
    {
        Type jobType;
        try
        {
            Require.ThatArgument(bundle != null);
            Require.ThatArgument(bundle.JobDetail != null);
            jobType = bundle.JobDetail.JobType;
        }
        catch (Exception e)
        {
            // This shouldn't ever happen, but if it does I want to know about it.
            _logService.LogCritical(() => e.ToString());
            throw;
        }
        try
        {
            return _jobFactory.GetObject(jobType);
        }
        catch (Exception e)
        {
            _logService.LogCritical(() => "An exception was thrown while creating job of type {0}: {1}"
                .With(jobType, e));
            throw;
        }
    }
}

And IObjectFactory is a simple interface that just abstracts away the Kernel so we're not depending on Ninject everywhere:

/// <summary>
/// Similar to IFactory, but this factory produces types based on a dynamic
/// <see cref="Type"/> object. If the given Type object is not of the given
/// "T" type, an exception will be thrown.
/// </summary>
/// <typeparam name="T">A parent-level type representing what sort of values
/// are expected. If the type could be anything, try using <see cref="object"/>
/// </typeparam>
public interface IObjectFactory<out T>
{
    T GetObject(Type type);
}

IObjectFactory is then bound to a class like this:

/// <summary>
/// This implementation of the generic <see cref="IFactory{T}"/> and 
/// <see cref="IObjectFactory{T}"/> classes uses Ninject to supply instances of 
/// the given type. It should not be used explicitly, but will rather be used 
/// by the DI framework itself, to provide instances to services that depend on 
/// IFactory objects.
/// This implementation takes the injection context as a constructor argument, so that
/// it can reuse elements of the context when it is asked to supply an instance
/// of a type.
/// In order for this to work, you will need to define bindings from <see cref="IFactory{T}"/>
/// and <see cref="IObjectFactory{T}"/> to this class, as well as a binding from 
/// <see cref="IContext"/> to a method or factory that returns the current binding 
/// context.
/// </summary>
/// <typeparam name="T">The Type of the service to be produced by the Get method.</typeparam>
public class InjectionFactory<T> : IFactory<T>, IObjectFactory<T>
{
    private readonly IKernel _kernel;
    private readonly IParameter[] _contextParameters;

    /// <summary>
    /// Constructs an InjectionFactory
    /// </summary>
    /// <param name="injectionContext">The context in which this injection is taking place.</param>
    public InjectionFactory(IContext injectionContext)
    {
        _contextParameters = injectionContext.Parameters
            .Where(p => p.ShouldInherit).ToArray();
        _kernel = injectionContext.Kernel;
    }

    public T Get()
    {
        try
        {
            return _kernel.Get<T>(_contextParameters.ToArray());
        }
        catch (Exception e)
        {
            throw e.Wrap(() => "An error occurred while attempting to instantiate an object of type <{0}>".With(typeof(T)));
        }
    }

    public T GetObject(Type type)
    {
        if (type == null)
        {
            throw new ArgumentNullException("type");
        }
        if (!typeof (T).IsAssignableFrom(type))
        {
            throw new InvalidCastException(type.FullName + " is not a child type of " + typeof (T).FullName);
        }
        try
        {
            return (T)_kernel.Get(type, _contextParameters);
        }
        catch (Exception e)
        {
            throw e.Wrap(() => "An error occurred while attempting to instantiate an object of type <{0}>".With(typeof(T)));
        }
    }
}

... using a binding that looks like this:

    Bind(typeof (IObjectFactory<>)).To(typeof (InjectionFactory<>));
    // Injection factories ask for the injection context.
    Bind(typeof (IContext)).ToMethod(c => c.Request.ParentContext);

So the overall effect is that we use the Ninject kernel to create the IJobFactory, which uses constructor injection to take an IObjectFactory<IJob>, which is invoked to produce any IJobs that Quartz requires. Those job classes can therefore use constructor-based injection, and Ninject is indirectly instantiating them.


i don't think you have understood IoC very well. When you say the ISession will get auto binded through NHibernate - what are you thinking happens? NHibernate won't manage that for you. Session lifestyle management is your domain.

I don't think there is enough code to really help you. the service as well as the repository would need to be managed by your IoC to be able to inject one into the other - unless you have a direct reference to the IoC - which is not recommended.


You should get the instance of IScheduledRemindersService from your IoC container. The container should take charge of injecting all of the dependencies for you as it creates/resolves the instance.

I'm not familiar with NInject so I can't tell you how it should be done, but you should do something along the lines of:

protected void Application_Start()
{
    // 1- register IoC container     
    // 2- resolve instance of IScheduledRemindersService     
    // 3- invoke RemindersService() method on instance    
}
0

精彩评论

暂无评论...
验证码 换一张
取 消