What would be the most elegant way to refactor the following method?
public void AddOrUpdateDesignCVT(DesignInformation designInformation,
CommonTransformerProperties commonTransformerProperties,
SecondaryWindings secondaryWindings,
CapacitorVoltageDivider capacitorVoltageDivider,
IntermediateVoltageTransformer intermediateVoltageTransformer,
SpecialDesignInformation specialDesignInformation,
TransformerLimits transformerLimits)
{
GenerateWindingInstructions(designInformation);
Design design;
using (var context = new DesignContext())
{
if (!DesignExist(designInformation.DesignID))
{
context.DesignInformation.Add(designInformation);
context.CommonTransformerPropeteries.Add(commonTransformerProperties);
context.SecondaryWindings.Add(secondaryWindings);
context.CapacitorVoltageDivider.Add(capacitorVoltageDivider);
context.IntermediateVoltageTransformer.Add(intermediateVoltageTransformer);
context.SpecialDesignInformation.Add(specialDesignInformation);
context.TransformerLimits.Add(transformerLimits);
context.SaveChanges();
return;
}
}
using (var newcontext = new DesignContext())
{
design = GetDesingByNameFromDatabase(newcontext, designInformation.DesignID, designInformation.Technology);
UpdateSecondaryWindings(design, newcontext, designInformation, secondaryWindings);
UpdateSpecialWires(design, newcontext, designInformation, specialDesignInformation);
newcontext.SaveChanges();
}
int dbId = design.DesignInformation.DataBaseID;
design.DesignInformation = designInformation;
design.DesignInformation.DataBaseID = dbId;
design.CommonTransformerProperties = commonTransformerProperties;
design.CommonTransformerProperties.DataBaseID = dbId;
design.SecondaryWindings = secondaryWindings;
design.SecondaryWindings.DataBaseID = dbId;
design.CapacitorVoltageDivider = capacitorVoltageDivider;
design.CapacitorVoltageDivider.DataBaseID = dbId;
design.IntermediateVoltageTransformer = intermediateVoltageTransformer;
design.IntermediateVoltageTransformer.DataBaseID = dbId;
design.SpecialDesignInformation = specialDesignInformation;
design.SpecialDesignInformation.DataBaseID = dbId;
using (var newcontext = new DesignContext())
{
newcontext.Entry(design.CommonTransformerProperties).State = System.Data.Entity.EntityState.Modified;
newcontext.Entry(design.SecondaryWindings).State = System.Data.Entity.EntityState.Modified;
newcontext.Entry(design.CapacitorVoltageDivider).State = System.Data.Entity.EntityState.Modified;
newcontext.Entry(design.IntermediateVoltageTransformer).State = System.Data.Entity.EntityState.Modified;
newcontext.Entry(design.SpecialDesignInformation).State = System.Data.Entity.EntityState.Modified;
newcontext.Entry(design.DesignInformation).State = System.Data.Entity.EntityState.Modified;
newcontext.SaveChanges();
return;
}
}