When we use the Entity Framework (EF) in multilayer information systems sometimes we want to extend classes generated by EF by adding some common properties or functions. Such operation can`t be conduct on *.edmx data model so we need to make some improvement in our solution. Let`s begin...
Now, when our classes are connected to each other we can simple call it`s instance in our Console Application. But to do this we need to create an instance on Model at runtime and create object factory so we implement to in future:) I hope You understood the principle.
Source code
Thank You.
Lets assumed that in our soulution we have only three layer (three project):
- Client console application which has reference to the second layer - 'ConsoleApplication' project name
- Class library project with class interfaces only - 'Interfaces' project name
- Class library class implementation and data model referenced to 'Interfaces' project - 'Classes' project name.
Picture 1. Solution structure. |
Now when we have all solution structure we can focus on data model. In the 'Classes' project we create a new folder named 'Model' and inside add new item of ADO.NET Entity Data Model named 'Learning.edmx' - it may be empty or generated from database (in this example I created the empty one). Now it`s time to add some new entities to our model so lets assumed that we want to create a CarBrand entity and a CarModel with one to many relation (i hope You know how to do that).
Picture 2. Entity data model. |
Next step is to create a new folder named 'Objects' (name is not matter) and inside it create two partial classes as presented in the code-sniped below. (For this example I type the class definition in one file but in real project always create separated file for each class!!).
Code Snippet
- namespace Classes.Objects
- {
- public partial class CarBrand
- {
- }
- public partial class CarModel
- {
- }
- }
After complete class implementation we can create an interfaces for each of them. We do this in the 'Interfaces' project.
Code Snippet
- namespace Interfaces.IObjects
- {
- public interface ICarBrand
- {
- int CarBrandID { get; set; }
- string Name { get; set; }
- IList<ICarModel> CarModels { get; set; }
- }
- public interface ICarModel
- {
- int CarModelID { get; set; }
- string Name { get; set; }
- ICarBrand ParentBrand { get; set; }
- }
- }
Please notice that in the line 12 and 19 I added two new properties. First one is list of car models for a single car brand object instance and the second one is parent car brad for car model. Both interfaces as public and are located in the 'IObject' folder.
Not it`s time to connect interfaces with the class definitions. Because the 'Classes' project has a reference to the 'Interfaces' by using using Interfaces.IObjects we are able to add suitable interface to each class. But it`s end because to connect custom our class to entity generated class we must inherit from special Entity type named EntityObject which is located int System.Data.Objects.DataClasses namespace. So after all changed we already have done out classes presents as follow...
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Interfaces.IObjects;
- using System.Data.Objects.DataClasses;
- namespace Classes.Objects
- {
- public partial class CarBrand : EntityObject, ICarBrand
- {
- public IList<ICarModel> CarModels
- {
- get
- {
- throw new NotImplementedException();
- }
- set
- {
- throw new NotImplementedException();
- }
- }
- }
- public partial class CarModel : EntityObject, ICarModel
- {
- public ICarBrand ParentBrand
- {
- get
- {
- throw new NotImplementedException();
- }
- set
- {
- throw new NotImplementedException();
- }
- }
- }
- }
Notice that we have implement both interfaces but also leave default implementation (line 15 and 33) - we back on it. Now it`s time to make a connection between out classes and classes generated by EF. After open data model press F4 on empty space and set Namespace property to Classes.Objects (namespace which contains are custom classes). After this do the same after clicking F4 on selected *.edmx file in Solution Explorer - example below. When both task completed just rebuild 'Classes' project.
Picture 3. Setting custom namespace to model. |
Now it`s time to make last implementation in out custom class. Please notice that each of out custom class has now a properties defined in data model!! We have to implement two properties ParentBrand in CarModel class and CarModels in CarBrand class. Because out classes (generated by EF and our custom) see each other properties so we can call their own properties as follow.
Code Snippet
- namespace Classes.Objects
- {
- public partial class CarModel : EntityObject, ICarModel
- {
- public ICarBrand ParentBrand
- {
- get
- {
- return this.CarBrand as ICarBrand; //conver to Interface
- }
- set
- {
- this.CarBrand = value as CarBrand; //convert to class
- }
- }
- }
- public partial class CarBrand : EntityObject, ICarBrand
- {
- public IList<ICarModel> CarModels
- {
- get
- {
- return this.CarModels.Cast<ICarModel>().ToList();
- }
- set
- {
- this.CarModels = (IList<ICarModel>)value;
- }
- }
- }
- }
Now, when our classes are connected to each other we can simple call it`s instance in our Console Application. But to do this we need to create an instance on Model at runtime and create object factory so we implement to in future:) I hope You understood the principle.
Source code
Thank You.