Exemplo n.º 1
0
        private async Task SeedSingleSeed(int seedingStep, ServiceProvider serviceProvider, SeedInfo seedInfo)
        {
            Assert(seedingStep > 0);
            Assert(serviceProvider != null);
            Assert(seedInfo != null);

            Preparing?.Invoke(this, new SeedingEventArgs(seedingStep, seedInfo));

            var seed = (ISeed)ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, seedInfo.SeedType);

            foreach (var seedOutputProperty in seedInfo.SeedOutputProperties)
            {
                // We always want to create a new instance of a seed output class.
                // Why? Because in a general case seed output classes will have dependency constructors
                // that can potentially have transient dependencies.
                var propertyValue = ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, seedOutputProperty.PropertyType);
                seedOutputProperty.SetValue(seed, propertyValue);
            }

            if (await seed.OutputAlreadyExists())
            {
                Skipping?.Invoke(this, new SeedingEventArgs(seedingStep, seedInfo));
                return;
            }

            Seeding?.Invoke(this, new SeedingEventArgs(seedingStep, seedInfo));

            await seed.Seed();

            SeedingEnded?.Invoke(this, new SeedingEventArgs(seedingStep, seedInfo));
        }