定义一个接口:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace AutoFacTest
{
public interface IAddressService
{
string GetAddress(int contactNumber);
}
}

实现类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace AutoFacTest
{
public class AddressService : IAddressService
{
public string GetAddress(int contactNumber)
{
string result = string.Empty; if (contactNumber == )
result = "上海浦东"; return result;
}
}
}

先来定义一个容器:

 using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;
using Autofac.Extras.AggregateService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace AutoFacTest.Common
{
public class ObjectContainer
{
public static IContainer Container
{
get
{
return _container;
}
} private static IContainer _container; private ObjectContainer()
{
} public static void Initialize(Action<ContainerBuilder> action)
{
var builder = new ContainerBuilder(); if (action != null)
{
action(builder);
} _container = builder.Build();
} public static bool IsRegistered<TService>()
{
ThrowIfNotInitialized();
return _container.IsRegistered<TService>();
} public static bool IsRegistered(Type serviceType)
{
ThrowIfNotInitialized();
return _container.IsRegistered(serviceType);
} public static TService Resolve<TService>(params Parameter[] parameters)
{
ThrowIfNotInitialized();
return _container.Resolve<TService>(parameters);
} public static object Resolve(Type serviceType, params Parameter[] parameters)
{
ThrowIfNotInitialized();
return _container.Resolve(serviceType, parameters);
} public static TService ResolveNamed<TService>(string serviceName, params Parameter[] parameters)
{
ThrowIfNotInitialized();
return _container.ResolveNamed<TService>(serviceName, parameters);
} public static object ResolveNamed(string serviceName, Type serviceType, params Parameter[] parameters)
{
ThrowIfNotInitialized();
return _container.ResolveNamed(serviceName, serviceType, parameters);
} public static bool TryResolve<TService>(out TService service)
{
ThrowIfNotInitialized();
return _container.TryResolve<TService>(out service);
} public static bool TryResolve(Type serviceType, out object service)
{
ThrowIfNotInitialized();
return _container.TryResolve(serviceType, out service);
} private static void ThrowIfNotInitialized()
{
if (_container == null)
throw new InvalidOperationException("Container should be initialized before using it.");
}
}
}

Global中注册AddressService和IAddressService的依赖关系,自动将实现类AddressService注入到Ioc容器中。

using AutoFacTest.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Extras.AggregateService; namespace AutoFacTest
{
public class Global : HttpApplication
{
private System.ComponentModel.IContainer components = null;
protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
RegisterDependencies();
} private void RegisterDependencies()
{
ObjectContainer.Initialize(builder =>
{
builder.RegisterType<AddressService>().As<IAddressService>
().InstancePerDependency();
});
}

}
}

使用:

  IAddressService svc = Common.ObjectContainer.Resolve<IAddressService>();
string address = svc.GetAddress();

再也不用手动new那么多 object。再也不用手写factory,AutoFac就是这么方便。

05-11 13:19