Dependency Injection Question - ASP.NET

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2009-08-12T10:37:20Z Indexed on 2010/04/09 20:03 UTC
Read the original article Hit count: 336

I'm starting a web application that contains the following projects:

Booking.Web Booking.Services Booking.DataObjects Booking.Data

I'm using the repository pattern in my data project only. All services will be the same, no matter what happens. However, if a customer wants to use Access, it will use a different data repository than if the customer wants to use SQL Server.

I have StructureMap, and want to be able to do the following:

Web project is unaffected. It's a web forms application that will only know about the services project and the dataobjects project.

When a service is called, it will use StructureMap (by looking up the bootstrapper.cs file) to see which data repository to use.

An example of a services class is the error logging class:

public class ErrorLog : IErrorLog
{
    ILogging logger;

    public ErrorLog()
    {

    }

    public ErrorLog(ILogging logger)
    {
        this.logger = logger;
    }

    public void AddToLog(string errorMessage)
    {
        try
        {
            AddToDatabaseLog(errorMessage);
        }

        catch (Exception ex)
        {
            AddToFileLog(ex.Message);
        }

        finally
        {
            AddToFileLog(errorMessage);
        }
    }

    private void AddToDatabaseLog(string errorMessage)
    {
        ErrorObject error =
            new ErrorObject
            {
                ErrorDateTime = DateTime.Now,
                ErrorMessage = errorMessage
            };

        logger.Insert(error);
    }

    private void AddToFileLog(string errorMessage)
    {
        // TODO: Take this value from the web.config instead of hard coding it
        TextWriter writer = new StreamWriter(@"E:\Work\Booking\Booking\Booking.Web\Logs\ErrorLog.txt", true);
        writer.WriteLine(DateTime.Now.ToString() + " ---------- " + errorMessage);
        writer.Close();
    }
}

I want to be able to call this service from my web project, without defining which repository to use for the data access. My boostrapper.cs file in the services project is defined as:

public class Bootstrapper
{
    public static void ConfigureStructureMap()
    {
        ObjectFactory.Initialize(x =>
        {
            x.AddRegistry(new ServiceRegistry());
        }
        );
    }

    public class ServiceRegistry : Registry
    {
        protected override void configure()
        {
            ForRequestedType<IErrorLog>().TheDefaultIsConcreteType<Booking.Services.Logging.ErrorLog>();
            ForRequestedType<ILogging>().TheDefaultIsConcreteType<SqlServerLoggingProvider>();
        }
    }
}

What else do I need to get this to work? When I defined a test, the ILogger object was null.

Thanks,

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about dependency-injection