系統(tǒng)啟動(dòng)時(shí)執(zhí)行任務(wù):IStartupTask,啟動(dòng)時(shí)執(zhí)行的任務(wù)主要是數(shù)據(jù)庫(kù)的初始化和加載。
IStartupTask調(diào)用IEfDataProvider進(jìn)行數(shù)據(jù)庫(kù)的初始化。
IEfDataProvider,SqlCeDataProvider:獲取數(shù)據(jù)連接工廠,不同類型數(shù)據(jù)庫(kù),連接工廠不同。
接口IStartupTask的實(shí)體類EfStartUpTask的實(shí)現(xiàn)如下:
public class EfStartUpTask : IStartupTask
{
public void Execute()
{
var settings = EngineContext.Current.ResolveDataSettings>();
if (settings != null settings.IsValid())
{
var provider = EngineContext.Current.ResolveIEfDataProvider>();
if (provider == null)
throw new NopException("No EfDataProvider found");
provider.SetDatabaseInitializer();
}
}
public int Order
{
//ensure that this task is run first
get { return -1000; }
}
}
SqlCeInitializer,CreateCeDatabaseIfNotExists初始化數(shù)據(jù)庫(kù)。
IDbContext,NopObjectContext系統(tǒng)數(shù)據(jù)庫(kù)操作上下文。加載所有數(shù)據(jù)庫(kù)映射類:EntityTypeConfigurationTEntityType>。代碼如下:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//dynamically load all configuration
//System.Type configType = typeof(LanguageMap); //any of your configuration classes here
//var typesToRegister = Assembly.GetAssembly(configType).GetTypes()
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null type.BaseType.IsGenericType type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
//...or do it manually below. For example,
//modelBuilder.Configurations.Add(new LanguageMap());
base.OnModelCreating(modelBuilder);
}
此方法是繼承自DbContext。并在系統(tǒng)啟動(dòng)時(shí)調(diào)用,建立數(shù)據(jù)表與實(shí)體的對(duì)應(yīng)關(guān)系。
在類型依賴注冊(cè)類Nop.Web.Framework.DependencyRegistrar中實(shí)現(xiàn)數(shù)據(jù)庫(kù)工廠的創(chuàng)建、數(shù)據(jù)庫(kù)的加載。如下代碼:
//data layer
var dataSettingsManager = new DataSettingsManager();
var dataProviderSettings = dataSettingsManager.LoadSettings();
builder.Register(c => dataSettingsManager.LoadSettings()).AsDataSettings>();
builder.Register(x => new EfDataProviderManager(x.ResolveDataSettings>())).AsBaseDataProviderManager>().InstancePerDependency();
builder.Register(x => (IEfDataProvider)x.ResolveBaseDataProviderManager>().LoadDataProvider()).AsIDataProvider>().InstancePerDependency();
builder.Register(x => (IEfDataProvider)x.ResolveBaseDataProviderManager>().LoadDataProvider()).AsIEfDataProvider>().InstancePerDependency();
if (dataProviderSettings != null dataProviderSettings.IsValid())
{
var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings());
var dataProvider = (IEfDataProvider)efDataProviderManager.LoadDataProvider();
dataProvider.InitConnectionFactory();
builder.RegisterIDbContext>(c => new NopObjectContext(dataProviderSettings.DataConnectionString)).InstancePerHttpRequest();
}
else
{
builder.RegisterIDbContext>(c => new NopObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerHttpRequest();
}
builder.RegisterGeneric(typeof(EfRepository>)).As(typeof(IRepository>)).InstancePerHttpRequest();
接口IEfDataProvider 的實(shí)體類SqlServerDataProvider的數(shù)據(jù)庫(kù)初始化方法如下:
/// summary>
/// Set database initializer
/// /summary>
public override void SetDatabaseInitializer()
{
//pass some table names to ensure that we have nopCommerce 2.X installed
var tablesToValidate = new[] {"Customer", "Discount", "Order", "Product", "ShoppingCartItem"};
//custom commands (stored proedures, indexes)
var customCommands = new Liststring>();
//use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests
customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/SqlServer.Indexes.sql"), false));
//use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests
customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/SqlServer.StoredProcedures.sql"), false));
var initializer = new CreateTablesIfNotExistNopObjectContext>(tablesToValidate, customCommands.ToArray());
Database.SetInitializer(initializer);
}
另外,EntityFramework本事是ORM框架,通過(guò)數(shù)據(jù)庫(kù)訪問(wèn)上下文建立與數(shù)據(jù)庫(kù)的連接及實(shí)體與數(shù)據(jù)表的對(duì)應(yīng)廣西。并通過(guò)創(chuàng)建IRepositoryT>的泛型實(shí)體類來(lái)實(shí)現(xiàn)對(duì)每一種數(shù)據(jù)的處理,也就是所謂的Dao層。業(yè)務(wù)邏輯層通過(guò)每種實(shí)體的數(shù)據(jù)訪問(wèn)倉(cāng)庫(kù)RepositoryT>來(lái)進(jìn)行數(shù)據(jù)庫(kù)操作。
您可能感興趣的文章:- 詳解如何在ASP.NET Core中應(yīng)用Entity Framework
- EntityFramework 6.x學(xué)習(xí)之多個(gè)上下文遷移實(shí)現(xiàn)分布式事務(wù)詳解