autofac依赖注入

C#
本文总阅读量:
  1. 1. C# Asp.net autofac依赖注入
    1. 1.1. service类创建实体,接口,具体实现类
    2. 1.2. mvc 引用中添加autofac mvc5引用,再App_Start里面添加AutofacConfig.cs配置文件
    3. 1.3. Application_Start初始化
    4. 1.4. 添加product控制器
    5. 1.5. 添加对应视图
    6. 1.6. 启动调试,此时通过autofac创建的对象已经可以呈现出来了。

C# Asp.net autofac依赖注入

service类创建实体,接口,具体实现类

1
2
3
4
5
6
7
8
9
10
11
public class Product
{
public int ProductID { get; set; }

public string Name { get; set; }

public string Description { get; set; }

public decimal Price { get; set; }

}
1
2
3
4
public interface IProductService
{
IEnumerable<Product> Products { get; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class ProductService:IProductService
{
public IEnumerable<Product> Products
{
get
{
//仅作演示
return new List<Product> {
new Product {ProductID = 1, Name = "Football", Description = "Football description", Price = 25},
new Product {ProductID = 2, Name = "Stuff board", Description = "Stuff board description", Price = 179 },
new Product {ProductID = 3, Name = "Running shoes", Description = "Running shoes description", Price = 95 },
new Product {ProductID = 4, Name = "Basketball", Description = "Basketball description", Price = 125},
new Product {ProductID = 5, Name = "Volleyball", Description = "Volleyball description", Price = 59 },
new Product {ProductID = 6, Name = "Basketball shoes", Description = "Basketball shoes description", Price = 195 },
new Product {ProductID = 7, Name = "Volleyball shoes", Description = "Volleyball shoes description", Price = 205},
new Product {ProductID = 8, Name = "Swimming clothes", Description = "Swimming clothes description", Price = 199 },
new Product {ProductID = 9, Name = "Climbing boot", Description = "Climbing boot description", Price = 900 },
new Product {ProductID = 10, Name = "Football", Description = "Football description", Price = 25},
new Product {ProductID = 11, Name = "Stuff board", Description = "Stuff board description", Price = 179 },
new Product {ProductID = 12, Name = "Running shoes", Description = "Running shoes description", Price = 95 },
new Product {ProductID = 13, Name = "Basketball", Description = "Basketball description", Price = 125},
new Product {ProductID = 14, Name = "Volleyball", Description = "Volleyball description", Price = 59 },
new Product {ProductID = 15, Name = "Basketball shoes", Description = "Basketball shoes description", Price = 195 },
new Product {ProductID = 16, Name = "Volleyball shoes", Description = "Volleyball shoes description", Price = 205},
new Product {ProductID = 17, Name = "Swimming clothes", Description = "Swimming clothes description", Price = 199 },
new Product {ProductID = 18, Name = "Climbing boot", Description = "Climbing boot description", Price = 900 }
};
}
}
}

mvc 引用中添加autofac mvc5引用,再App_Start里面添加AutofacConfig.cs配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static class AutoFacConfig
{

public static void Initialize()
{
//初始化容器
var builder = new ContainerBuilder();

//注册每个对象实例
var container = RegisterServices(builder);

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

private static IContainer RegisterServices(ContainerBuilder builder)
{
//注册controller
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterInstance(new ProductService()).As<IProductService>();

return builder.Build();
}
}

Application_Start初始化

1
2
3
4
5
6
7
8
9
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

AutoFacConfig.Initialize();
}

添加product控制器

控制器中index返回整个list

先往控制器中添加传输层数据私有字段

1
private IProductService productService;

初始化

1
2
3
4
public ProductController(IProductService productService)
{
this.productService = productService;
}

当程序需要productService时,会通过container自动获取对象

添加对应视图

选list模板,暂时用entities里面的模型类,自己调整layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<table class="striped responsive-table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
</tr>
}

启动调试,此时通过autofac创建的对象已经可以呈现出来了。