Académique Documents
Professionnel Documents
Culture Documents
Part 0 Introduction:
Our main goal is to develop a MVC website for a hypermarket using .NET framework and
following this architecture:
DataBase
Front-Office
Back-Office
MyFinance.Web
Reference
MyFinance.Data
MyFinance.Domainn
W
(MyF
BLL : Busi
(MyFinanc
DAL : Da
(MyFinan
5. Click OK
We've made the navigation properties virtual so that we get lazy loading.
DateTime? Type is the nullable DateTime
We used ICollection interface so we can use later any collection implementation.
Web.config
8. Click Add
9. Explore the generated Controller and Views
Step 5 : Run and enjoy
Follow these steps:
1. Right click the MyFinance.Web project
2. Click Set as start-up project
Step 2 : Assuming that we are using a production environment and we have important
data. Then we are not allowed to lose this data. In this case the initialisation
strategy doesn't seem to be a good choice. So lets try something else,
Migration !!!! Lets do it.
1. The first step: we have to enable the Migration: Open the Package
Manager Console the choose the project MyFinance.Data which
contains the class Context. Then execute this command EnableMigrations
Entity Framework Code First generate a new folder Migration which contains
two classes xxx_InitialCreate and Configuration.
-The first class contains an Up () method that contains instructions to create the
table with its original definition, and another method Down () to delete the table.
-The second class define the behavior of the migration for DbContext used. This is
where you can specify the data to insert, enter the providers of other databases
2. The second step: we will change the name of the property public string
Image { get; set; } to public string ImageName { get; set; }.
3. Execute the command Add-Migration ModifyNameImage in the
Package Manager Console.
4. The Add-migration command checks for changes since your last
migration and scaffold a new migration with any changes found. We can
give a name for our migration, in this case, we call migration
"ModifyNameImage".
To create a new Complex Type we have to follow those rules otherwise we have to
configure a complex type using either the data annotations or the fluent API.
Step 2 : Lets update Chemical entity with the complex type as the following :
public class Chemical : Product
{
public string LabName { get; set; }
public Address LabAddress { get; set; }
}
Step 2 : Now , we will add different data annotations to configure those entities.
[DataType(DataType.Currency)]
public double Price { get; set; }
[Range(0, int.MaxValue)]
public int Quantity { get; set; }
[DataType(DataType.ImageUrl),Display(Name = "Image")]
public string ImageName { get; set; }
[Display(Name = "Production Date")]
[DataType(DataType.DateTime)]
public DateTime DateProd { get; set; }
//foreign Key properties
public int? CategoryId { get; set; }
//navigation properties
[ForeignKey("CategoryId ")]
//useless in this case
public virtual Category Category { get; set; }
public virtual ICollection<Provider> Providers { get; set; }
}
MaxLength is used for the Entity Framework to decide how large to make a string
value field when it creates the database.
StringLength is a data annotation that will be used for validation of user input.
DataType : is used to specify a specific type : email, currency, card number
Display is used to change the displayed name of the property
Range is used to limit valid inputs between min and max values.
Foreignkey : To configure the foreign key
The Many to many part configure the association between the Product class and
The Provider class
Step 4 :Finally we configure the complex type Address by creating this class:
public class AddressConfiguration : ComplexTypeConfiguration<Address>
{
public AddressConfiguration()
{
Property(p => p.City).IsRequired();
Property(p => p.StreetAddress).HasMaxLength(50)
.IsOptional();
}
}
Step 6 : Update the database by migration and explore it to see what the
configurations have done.
Step 3 : Update the Context to apply these custom convention so, in the
MyFinanceContext class override the OnModelCreating method like this :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//If you want to remove all Convetions and work only with configuration :
// modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Configurations.Add(new CategoryConfiguration());
modelBuilder.Configurations.Add(new ProductConfiguration());
modelBuilder.Configurations.Add(new AddressConfiguration());
modelBuilder.Conventions.Add(new DateTime2Convention());
}
-Then we will add a RepositoryBase class. In the first hand, we create this
abstract class is a generic class which contain generics CRUD methods. In the
second hand, we will add the different repositories in which we will extend
IRepositoryBaseAsync<T> of a specific entity.
-As we see in this class, we pass a DatabaseFactory object through the
constructor and we create a generic Set since this Set will be used by the
different entities.
-After getting the Context object created by the DatabaseFactory we add some
generics methods.
-Note that in somes methods like public virtual void
Delete(Expression<Func<T, bool>> where)
Step 3 :-Now we will see the last design pattern in our project The Unit Of
Work.
-The idea is that we can use the Unit of Work to group a set of related operations
the Unit of Work keeps track of the changes that we are interested in until we
are ready to save them to the database.
-The pattern Unit Of Work consists in making one or more transactions database
committed or rolled back together.
-The pattern "Unit of work" is used to:
}
public void Dispose()
{
dataContext.Dispose();
}
public IRepositoryBaseAsync<T> getRepository<T>() where T : class
{
IRepositoryBaseAsync<T> repo = new RepositoryBase<T>(dataContext);
return repo;
}
}