加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

Modular Web Application with ASP.NET Core

发布时间:2020-12-15 19:42:32 所属栏目:asp.Net 来源:网络整理
导读:table class="text" tr class="li1" td class="ln"pre class="de1"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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
<tr class="li1">
<td class="ln"><pre class="de1">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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

(options => ? ? { ? ? ? ? options.ViewLocationExpanders.Add(new ModuleViewLocationExpander()); ? ? }); ``` ? ``` public class ModuleViewLocationExpander : IViewLocationExpander ? ? { ? ? ? ? private const string _moduleKey = "module"; ? ? ? ? ? public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context,IEnumerable viewLocations) ? ? ? ? { ? ? ? ? ? ? if (context.Values.ContainsKey(_moduleKey)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? var module = context.Values[_moduleKey]; ? ? ? ? ? ? ? ? if (!string.IsNullOrWhiteSpace(module)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? var moduleViewLocations = new string[] ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ?"/Modules/Modular.Modules." + module + "/Views/{1}/{0}.cshtml",? ? ? ? ? ? ? ? ? ? ? ?"/Modules/Modular.Modules." + module + "/Views/Shared/{0}.cshtml" ? ? ? ? ? ? ? ? ? ? }; ? ? ? ? ? ? ? ? ? ? ? viewLocations = moduleViewLocations.Concat(viewLocations); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? return viewLocations; ? ? ? ? } ? ? ? ? ? public void PopulateValues(ViewLocationExpanderContext context) ? ? ? ? { ? ? ? ? ? ? var controller = context.ActionContext.ActionDescriptor.DisplayName; ? ? ? ? ? ? var moduleName = controller.Split('.')[2]; ? ? ? ? ? ? if(moduleName != "WebHost") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? context.Values[_moduleKey] = moduleName; ? ? ? ? ? ? } ? ? ? ? } ? ? } ``` 3. Each module contains a ModuleInitializer.cs where services for that module is registered ? ``` ?// Register dependency in modules ? ? var moduleInitializerInterface = typeof(IModuleInitializer); ? ? foreach(var module in modules) ? ? { ? ? ? ? // Register dependency in modules ? ? ? ? var moduleInitializerType = module.Assembly.GetTypes().Where(x => typeof(IModuleInitializer).IsAssignableFrom(x)).FirstOrDefault(); ? ? ? ? if(moduleInitializerType != null && moduleInitializerType != typeof(IModuleInitializer)) ? ? ? ? { ? ? ? ? ? ? var moduleInitializer = (IModuleInitializer)Activator.CreateInstance(moduleInitializerType); ? ? ? ? ? ? moduleInitializer.Init(services); ? ? ? ? } ? ? } ``` Please note that services within modules already registered by Autofac 4. And this is how I serve static files for modules ? ``` // Serving static file for modules foreach(var module in modules) { ? ? var wwwrootDir = new DirectoryInfo(Path.Combine(module.Path,"wwwroot")); ? ? if (!wwwrootDir.Exists) ? ? { ? ? ? ? continue; ? ? } ? ? ? app.UseStaticFiles(new StaticFileOptions() ? ? { ? ? ? ? FileProvider = new PhysicalFileProvider(wwwrootDir.FullName),? ? ? ? RequestPath = new PathString("/"+ module.SortName) ? ? }); } ``` 5. For Entity Framework Every domain entities need to inherit from Entity,then on the "OnModelCreating" method,we find them and register them to DbContext ? ``` ?private static void RegisterEntities(ModelBuilder modelBuilder,IEnumerable typeToRegisters) ? ? { ? ? ? ? var entityTypes = typeToRegisters.Where(x => x.GetTypeInfo().IsSubclassOf(typeof(Entity)) && !x.GetTypeInfo().IsAbstract); ? ? ? ? foreach (var type in entityTypes) ? ? ? ? { ? ? ? ? ? ? modelBuilder.Entity(type); ? ? ? ? } ? ? } ``` Sometimes,we might also need to do some custom mappings for our model,let take look at the sample below ? ``` public class ModuleACustomModelBuilder : ICustomModelBuilder ? ? { ? ? ? ? public void Build(ModelBuilder modelBuilder) ? ? ? ? { ? ? ? ? ? ? modelBuilder.Entity() ? ? ? ? ? ? ? ? .Property(x => x.Name).HasColumnName("TestName"); ? ? ? ? } ? ? } ``` All the classes that implement the ICustomModelBuilder will be hooked and called in the "OnModelCreating" of the ModularDbContext ? ``` ?private static void RegisterCustomMappings(ModelBuilder modelBuilder,IEnumerable typeToRegisters) ? ? { ? ? ? ? var customModelBuilderTypes = typeToRegisters.Where(x => typeof(ICustomModelBuilder).IsAssignableFrom(x)); ? ? ? ? foreach(var builderType in customModelBuilderTypes) ? ? ? ? { ? ? ? ? ? ? if (builderType != null && builderType != typeof(ICustomModelBuilder)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? var builder = (ICustomModelBuilder)Activator.CreateInstance(builderType); ? ? ? ? ? ? ? ? builder.Build(modelBuilder); ? ? ? ? ? ? } ? ? ? ? } ? ? } ``` 6. Strong typed view There is a known issue with 1.0.0 on how MVC finds compilation assemblies for class libraries. We can workaround by adding the modules assemblies to the list of compilation assemblies directly. ? ``` var mvcBuilder = services.AddMvc() ? ? .AddRazorOptions(o => ? ? { ? ? ? ? foreach (var module in modules) ? ? ? ? { ? ? ? ? ? ? o.AdditionalCompilationReferences.Add(MetadataReference.CreateFromFile(module.Assembly.Location)); ? ? ? ? } ? ? }); ``` Yeah,and now we are done. Please checkout the source code for more details. ? ? ? ?(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

<table class="text">

    推荐文章
      热点阅读