开发者

Removing a dependency from a constructor

I'm using webforms and I'm wondering how I can remove the followoing concrete reference to a repository. In the past I've used castle windsor with MVC but I don't think I can use that here?

Code behind:

ICustomerRepository repos;

public Workout_Admin()
    // here is the offending concrete implementation
    : this(new SqlCustomerRepository()) { }

public Workout_Admin(ICustomerRepository repos)
{
    this.repos = repos;
}

UPDATED ---

I've updated the static method as suggeted, as well as adding the additional code to the windsor factory

WindsorContainer container;

public WindsorControllerFactory()
{
    container = new WindsorContainer(
        new XmlInterpreter(new ConfigResource("castle")));

    var controllerTypes =
        from t in Assembly.GetExecutingAssembly().GetTypes()
        where typeof(IController).IsAssignableFrom(t)
        select t;

    foreach (Type t in controllerTypes)
    {
        container.AddComponentLifeStyle(t.FullName, t,
            LifestyleType.Transient);
    }

    CommonServiceLocatorPageHandlerFactory.Container = container;
}

The issue that keeps arriseing is with loading the assembly from the config file. The CommonServiceLocatorPageHandlerFactory is in and assembly called yourfit, folder called factory. And here are the relevant configs

<httpHandlers>
  <add verb="*" path="*.aspx"
    type="YourFit.Factory.CommonServiceLocatorPageHandlerFactory, YourFit"/>
</httpHandlers>
<handlers>
  <remove name="UrlRoutingHandler"/>        
    <add name="CSLPageHandler" verb="*" path="*.aspx"
      type="YourFit.Factory.CommonServiceLocatorPageHandlerFactory, YourFit"/>
</handlers>

and the error is:

Could not load type 'YourFit.Factory.CommonServiceLocatorPageHandlerFactory' from assembly 'YourFit'.

I know I开发者_JAVA百科'm most likely being really stupid. Thanks so much for your time on this.


You can do this. The ASP.NET compilation engine however needs a default constructor, but you can make it protected. You can inject the dependencies in the other constructor by defining a custom PageHandlerFactory that injects dependencies in the overloaded (public) constructor. Your class would look like this:

public class Workout_Admin : Page
{
    ICustomerRepository repos;

    protected Workout_Admin() { }

    public Workout_Admin(ICustomerRepository repos)
    {
        this.repos = repos;
    }
}

Here is an article that shows you how to do this (for Castle Windsor, just change the code in the GetInstance method to call the Windsor container). Note that you need to run in full trust for this.

UPDATE

You can change the private static object GetInstance(Type type) method that the article describes to the following:

public static IWindsorContainer Container;

private static object GetInstance(Type type)
{
    return Container.Resolve(type);
}

In your application's startup path (where you configure the Castle Windsor container) you than must set the static Container property:

// Create
IWindsorContainer container = ...

// Configure

// Set container to page handler factory
CommonServiceLocatorPageHandlerFactory.Container = container;

I hope this makes sense.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜