I have a class course like this:
public class course
{
public int CourseID { get; set; }
public string Name { get; set; }
public Event Schedule {get; set;} //Event is coming from library Dday.iCal
}
Entity framework cannot correctly understand on how to save this property. ( I want to serialize it to string when saving, and keep it as event when worknig with it in my application.) So I have two methods, say, SerializeToString() and DeserializeFromString(). I want those methods to be applied only when saving to database.
What I came up with the following. Basically I'm trying to have a separate property as a string that will be saved in the database and Event will be ignored, but it doesn't save anything to the database now. I'm not even sure if this is a good approach to do things, or there's something better that can be done.:
public class course
{
public int CourseID { get; set; }
public string Name { get; set; }
private Event _Schedule;
[NotMapped]
public Event Schedule {
get
{
if (!String.IsNullOrEmpty(CourseSchedule))
{
return DeserilizeFromString(CourseSchedule);
}
return new Event();
}
set
{
_schedule = value;
}
}
private string _courseSchedule;
public string CourseSchedule {
get
{
return _courseSchedule;
}
private set
{
if (Schedule != null)
{
_courseSchedule = SerializeToString(Schedule);
}
else
{
_courseSchedule = null;
}
}
}
An author on asp.net actually has an implementation of what your trying to do, almost to a tee. You may want to follow a few points in that project to get you started. The link to the project is here.
Some things to note, is it does utilize the DbContext Api that was implemented in Entity Framework. Some of the abstraction mentioned above is like this:
Your Solution:
Model
View
Controller
Data Access Layer (DAL)
The tutorial will actually go through the implementation with a Course Controller, Unit Of Work Class, and Repositories. By the end of the tutorial it will implement those automatic properties with DbContext and looks like this:
// Model:
public abstract class Person
{
[Key]
public int PersonID { get; set; }
[Required(ErrorMessage = "Last name is required.")]
[Display(Name = "Last Name")]
[MaxLength(50)]
public string LastName { get; set; }
[Required(ErrorMessage = "First name is required.")]
[Column("FirstName")]
[Display(Name = "First Name")]
[MaxLength(50)]
public string FirstMidName { get; set; }
public string FullName
{
get
{
return LastName + ", " + FirstMidName;
}
}
}
// Repository:
public class StudentRepository : IStudentRepository, IDisposable
{
private SchoolContext context;
public StudentRepository(SchoolContext context)
{
this.context = context;
}
public IEnumerable<Student> GetStudents()
{
return context.Students.ToList();
}
public Student GetStudentByID(int id)
{
return context.Students.Find(id);
}
public void InsertStudent(Student student)
{
context.Students.Add(student);
}
public void DeleteStudent(int studentID)
{
Student student = context.Students.Find(studentID);
context.Students.Remove(student);
}
public void UpdateStudent(Student student)
{
context.Entry(student).State = EntityState.Modified;
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
// Interface for Repository:
public interface IStudentRepository : IDisposable
{
IEnumerable<Student> GetStudents();
Student GetStudentByID(int studentId);
void InsertStudent(Student student);
void DeleteStudent(int studentID);
void UpdateStudent(Student student);
void Save();
}
// Context to Generate Database:
public class SchoolContext : DbContext
{
public DbSet<Course> Courses { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Instructor> Instructors { get; set; }
public DbSet<Student> Students { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<OfficeAssignment> OfficeAssignments { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Instructor>()
.HasOptional(p => p.OfficeAssignment).WithRequired(p => p.Instructor);
modelBuilder.Entity<Course>()
.HasMany(c => c.Instructors).WithMany(i => i.Courses)
.Map(t => t.MapLeftKey("CourseID")
.MapRightKey("PersonID")
.ToTable("CourseInstructor"));
modelBuilder.Entity<Department>()
.HasOptional(x => x.Administrator);
}
}
// Unit Of Work
public class UnitOfWork : IDisposable
{
private SchoolContext context = new SchoolContext();
private GenericRepository<Department> departmentRepository;
private CourseRepository courseRepository;
public GenericRepository<Department> DepartmentRepository
{
get
{
if (this.departmentRepository == null)
{
this.departmentRepository = new GenericRepository<Department>(context);
}
return departmentRepository;
}
}
public CourseRepository CourseRepository
{
get
{
if (this.courseRepository == null)
{
this.courseRepository = new CourseRepository(context);
}
return courseRepository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
That is some of the content in the lesson, I believe it will answer your question pretty explicitly while giving you an understanding of why the abstraction works, since it does implement the Fluent Api.
Hope that helps.
You should keep your models as minimalistic as possible, just the auto-properties and attributes. For more complex business logic it's good to add another layer to your MVC pattern. This one is usually called Repository (hard to find a good tutorial on Repository Pattern though.. :( )and comes between model and controller controller.
This also is very useful for performing unit tests. When properly implemented it allows you to do replace database dependency with collection during tests. This approach will require a bunch of additional work on the project.
One more approach (a simpler one) would be to add a ViewModel layer. Do it this way:
class MyModel
{
public string Text { get; set; }
}
class MyViewModel : MyModel
{
public new string Text
{
get { return base.Text; }
set { base.Text =value.ToUpper(); }
}
}
class Program
{
static void Main(string[] args)
{
MyViewModel mvm = new MyViewModel();
mvm.Text = "hello there";
var s = ((MyModel) mvm).Text; // "HELLO THERE"
}
}
In DataContext use MyModel in controller use MyViewModel.
If you have a model that looks like this
using (LolEntities context = new LolEntities)
{
...
}
Somewhere in your application, this model is defined, usually something like this:
public partial class LolEntities : ObjectContext
(1) Notice that the class is partial, so you could just create another partial class with the same name and override:
public override int SaveChanges(SaveOptions options)
(2) Or you can just capture the event:
using (DemoAZ_8_0Entities context = new DemoAZ_8_0Entities())
{
context.SavingChanges += ...
}
and do your formatting before it gets sent back to the DB.
In your model just make sure to include a property that properly maps to the column in the DB.
Maybe introducing some abstraction over this logic, you could recreate the unit of work and repository pattern and add the desired logic in a more convenient way. For example in the Course repository class you can costumize the add and find method serializing and deserializing the event field.
I am going to focus on the repository pattern, you can find a lot of information about
the design of the whole data access layer on the web.
For example, to manage courses, your application should depends on a ICourseRepository interface like this
interface ICourseRepository
{
void Add(Course newCourse);
Course FindByID(int id);
}
And you provide the folowing implementation:
class CourseRepository
{
// DbContext and maybe other fields
public void Add(Course c)
{
// Serialize the event field before save the object
_courses.Add(c); // calling entity framework functions, note
// that '_courses' variable could be an DBSet from EF
}
public Course FindById(int id)
{
var course = /// utilize EF functions here to retrieve the object
// In course variable deserialize the event field before to return it ...
}
}
Note that the ObjectContext in EF is an implementation of this pattern, if you are not intresting in change the ORM in the future you can just override the Save method on EF.
If you want to know more about this kind of pattern you can visit the Martin Fowler site:
Unit Of Work pattern
Repository pattern
Related
What pattern/s can I apply on my code? I have been reading on the patterns Bridge, Command, and Builder, and it looks like I can apply them on the code block below, but I struggle at how I can apply them to my code. I've also tried adding a generic method, but I struggle with consuming it with my repository.
This is a webform backend code. I am using .Net 3.5 (can't upgrade), and C#7. Here is the code:
Entities
Note: I'm using data transfer objects because these classes have a lot of properties. Also, SpecialRequestDTO inherits StandardRequestDTO.
public class StandardRequest
{
public int RequestType { get; protected set; }
public string Name { get; protected set; }
private StandardRequest(StandardRequestDTO dto) { Name = dto.Name; }
public static StandardRequest Create(StandardRequestDTO dto) => new StandardRequest(dto);
}
public class SpecialRequest : StandardRequest
{
public string Desc { get; protected set; }
private SpecialRequest(SpecialRequestDTO dto) : base((StandardRequestDTO) dto) { Desc = dto.Desc; }
public static SpecialRequest Create(SpecialRequestDTO dto) => new SpecialRequestDTO(dto);
}
Repository
public class Repository
{
public void SaveStandardRequest(StandardRequest request)
{
var query = $"INSERT INTO Requests (Name, RequestType) Values(#{nameof(request.Name)}, #{nameof(request.RequestType)})";
// sqlcommand code etc
}
public void SaveSpecialRequest(SpecialRequest request)
{
var query = $"INSERT INTO Requests (Name, RequestType, Desc) VALUES(#{nameof(request.Name)}, #{nameof(request.Name)}, #{nameof(request.Desc)})";
// sqlcommand code etc
}
}
Index.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string requestTypeStr = Request.Form[nameof(requestTypeStr)];
if (string.IsNullOrEmpty(requestTypeStr))
return;
}
if (requestTypeStr == 0)
{
ValidateStandardRequestFields();
var dto = CreateStandardRequestDTO();
_repository.SaveStandardRequest(dto);
}
if (requestTypeStr == 1)
{
ValidateSpecialRequestFields();
var dto = CreateSpecialRequestDTO();
_repository.SaveSpecialRequest(dto);
}
}
I took most of what I could from your post and this is what I could come up with.
Your DTO objects don't seem very clear to me, so I've left it out, but they have a role to play when it comes to saving the objects in the repository and I've left that part out.
Interface
public interface IRequest
{
int RequestType { get; set; }
string Name { get; set; }
void ValidateFields();
}
Two types of Request
public class StandardRequest : IRequest
{
public int RequestType { get ; set ; }
public string Name { get ; set; }
public void ValidateFields()
{
//validation logic
}
}
public class SpecialRequest: IRequest
{
public string Desc { get; set; }
public int RequestType { get; set; }
public string Name { get; set; }
public void ValidateFields()
{
//validation logic
}
}
Factory to create the Request objects
public class RequestFactory
{
public static IRequest CreateRequest(string requestTypeStr)
{
switch (requestTypeStr)
{
case "0": return new SpecialRequest();
default: return new StandardRequest();
}
}
}
Class to handle the interactions of the IRequest object, aptly named RequestInteractions, I know a poor name choice!
This class is what validates and saves the requests.
public class RequestInteractions
{
private IRequest _requestObj;
private Repository _repository;
public RequestInteractions(IRequest requestObj, Repository repository)
{
_requestObj = requestObj;
_repository = repository;
}
public bool ValidateAndSave()
{
try
{
_requestObj.ValidateFields();
_repository.SaveRequest(_requestObj);
return true;
}
catch (Exception e)
{
throw;
}
}
}
Repository - like I said, this needs to be fleshed out. The IRequest (through the DTO) should be able to tell you how it needs to be persisted. You'll have to fill this in.
public class Repository
{
public void SaveRequest(IRequest requestObject)
{
//The respective DTO should help you figure out what to save based on the type of IRequest
}
}
Tying it all together
var repository = new Repository();
var requestObject = RequestFactory.CreateRequest("");
var requestInteractions = new RequestInteractions(requestObject,repository);
requestInteractions.ValidateAndSave();
Benefit of this approach - You need to create a new Request class (and
a DTO) when you get a new Request to add to the system, the rest of
the plumbing need not be touched.
Downside - Well, a lot of code compared to what you have.
I have a problem when adding new values with a many to many mapping in Entity Framework. I know about the unit of work pattern but in our solution we would like to keep a simple repository pattern and not a unit of work class that contains everything. Is this possible or should I just implement Unit of Work right away?
If I don't use iSupplierRepository below a supplier will be added, but it will always add a new one even though there already exists one with that name.
Error:
The relationship between the two objects cannot be defined because
they are attached to different ObjectContext objects.
Repository example:
public class SupplierRepository : IntEntityRepository<Supplier, DbContext>, ISupplierRepository
{
public SupplierRepository(DbContext context) : base(context, context.Suppliers)
{
}
}
Inherited repositories:
public class IntEntityRepository<TEntity, TContext> : EntityRepository<TEntity, TContext, int>
where TEntity : class, IEntity<int>
where TContext : BaseIdentityDbContext
{
public IntEntityRepository(TContext context, IDbSet<TEntity> set) : base(context, set)
{
}
public override async Task<TEntity> GetAsync(int id)
{
return (await GetAsync(entity => entity.Id == id)).SingleOrDefault();
}
...
public abstract class EntityRepository<TEntity, TContext, TId> : IEntityRepository<TEntity, TId>
where TEntity : class, IEntity<TId>
where TContext : BaseIdentityDbContext
{
protected TContext Context { get; }
protected IDbSet<TEntity> Set { get; }
protected EntityRepository(TContext context, IDbSet<TEntity> set)
{
Context = context;
Set = set;
}
public abstract Task<TEntity> GetAsync(TId id);
...
Unity:
container.RegisterType<ISupplierRepository, SupplierRepository>();
container.RegisterType<IContactRepository, ContactRepository>();
Controller:
private readonly IContactRepository iContactRepository;
private readonly ISupplierRepository iSupplierRepository;
public ContactsController(IContactRepository iContactRepository, ISupplierRepository iSupplierRepository)
{
this.iContactRepository = iContactRepository;
this.iSupplierRepository = iSupplierRepository;
}
[HttpPut]
[Route("UpdateContact/{id}")]
public async Task<IHttpActionResult> UpdateContact(ContactViewModel contactVm, int id)
{
try
{
var supplierList = new List<Supplier>();
foreach (var contactVmSupplier in contactVm.Suppliers)
{
var supplier = await iSupplierRepository.GetAsync(contactVmSupplier.Id);
supplierList.Add(supplier);
}
var contactOriginal = await iContactRepository.GetAsync(id);
var updatedContact = Mapper.Map<ContactViewModel, Contact>(contactVm, contactOriginal);
updatedContact.Suppliers = supplierList;
await iContactRepository.UpdateAsync(updatedContact);
return Ok();
}
catch (Exception e)
{
throw new Exception("Could not update a contact", e);
}
}
Viewmodels:
public class ContactViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<SupplierViewModel> Suppliers { get; set; }
}
public class SupplierViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
Models:
public class Contact : IEntity<int>
{
public Contact()
{
Suppliers = new List<Supplier>();
}
[Key]
public int Id { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public string Name { get; set; }
public ICollection<Supplier> Suppliers { get; set; }
}
public class Supplier: IEntity<int>
{
public Supplier()
{
Contacts = new List<Contact>();
}
[Key]
public int Id { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public string Name { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
}
If you install the Unity bootstrapper for ASP.NET Web API package, a UnityHierarchicalDependencyResolver is available which will use a new child container for each IHttpController resolution effectively making all registrations with a HierarchicalLifetimeManager resolved per request so that all repository instances in a controller will use the same DbContext.
The NuGet package will also install some bootstrapping code in App_Start which uses WebActivatorEx. You can either use this approach or change to align with what you are using right now. Based on your posted code it would look something like:
public static void ConfigureUnity(HttpConfiguration config)
{
var container = new UnityContainer();
container.RegisterType<DbContext>(new HierarchicalLifetimeManager());
container.RegisterType<ISupplierRepository, SupplierRepository>();
container.RegisterType<IContactRepository, ContactRepository>();
config.DependencyResolver = new UnityHierarchicalDependencyResolver(container);
}
Solved it like this, dependency injection is from the tutorial Dependency Injection in ASP.NET Web API 2.
https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/dependency-injection
App_Start -> WebApiConfig
public static void Register(HttpConfiguration config)
{
UnityConfig.ConfigureUnity(config);
...
UnityConfig:
public static void ConfigureUnity(HttpConfiguration config)
{
var context = new DbContext();
var container = new UnityContainer();
container.RegisterType<ISupplierRepository, SupplierRepository>(new InjectionConstructor(context));
container.RegisterType<IContactRepository, ContactRepository>(new InjectionConstructor(context));
config.DependencyResolver = new UnityResolver(container);
}
Update: Use Randy Levy's answer instead.
My recommendation here is not to use Repository or UoW at all. EF already has them implemented. You'll encounter a lot of issues trying to re-implement them.
As to specific issue you encounter with exception: you have to use the same DbContext for your entities. At the same time, you wouldn't like to use DbContext as Singleton and use it per-request instead. A possible solution for it might be found here.
Application_BeginRequest(...)
{
var childContainer = _container.CreateChildContainer();
HttpContext.Items["container"] = childContainer;
childContainer.RegisterType<ObjectContext, MyContext>
(new ContainerControlledLifetimeManager());
}
Application_EndRequest(...)
{
var container = HttpContext.Items["container"] as IUnityContainer
if(container != null)
container.Dispose();
}
I have an solution with 2 projects, one domain class and one webUI.
In the domain class I have 2 models, a db context and a databas initializer.
List.cs:
namespace Todo.Domain
{
public class List
{
public int ListID { get; set; }
public string Day { get; set; }
public ICollection<Task> Tasks { get; set; }
}
}
Task.cs:
namespace Todo.Domain
{
public class Task
{
public int TaskID { get; set; }
public int ListID { get; set; }
public string TodoTask { get; set; }
}
}
EFDbContext.cs:
namespace Todo.Domain
{
public class EFDbContext : DbContext
{
public EFDbContext() : base("TodoList") { }
public DbSet<List> Lists { get; set; }
public DbSet<Task> Tasks { get; set; }
}
}
Initializer:
namespace Todo.Domain
{
public class TodoDbInit : System.Data.Entity.DropCreateDatabaseIfModelChanges<EFDbContext>
{
protected override void Seed(EFDbContext context)
{
var list = new List<List>
{
new List { Day="MÃ¥ndag" }
};
list.ForEach(s => context.Lists.Add(s));
context.SaveChanges();
var task = new List<Task>
{
new Task { TodoTask="Fisk" }
};
task.ForEach(s => context.Tasks.Add(s));
context.SaveChanges();
}
}
}
Now, when I start my application, I want the database to be created. I have placed a setInitializer Global.asax:
namespace Todo.WebUI
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer(new TodoDbInit());
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
When I run my application, the database Is not created. I don't know why. Have I done something wrong?
You must make a call against your database in one of your controllers in order for it to be created (creation is on-demand). If you wish to manually create the database on application start see the answer posted here: Entity Framework code first, isn't creating the database
The following code is only setting the initializer that you wish to use against your database.
Database.SetInitializer(new TodoDbInit());
In order for it to actually be used you will need to create and access entities of your db context.
I am using StructureMap for my dependency resolving. I am faced with an issue I don't know how to solve as the only way to make a form posting I understand will end up being without dependency injection which negate the whole essence of having StructureMap in my project.
The error is that I don't have Add method in my IdbContext Model. Below is my approach.
I have a Model named Module defined as below
public class Module
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual DateTime? DateCreated { get; set; }
public virtual string ModuleDescription { get; set; }
}
and an interface defined as below.
public interface ISolnetDataSource
{
IQueryable<Module> Modules { get; }
void Save();
}
A DbContext like below
public class CMSDB : DbContext, ISolnetDataSource
{
public CMSDB() : base("DefaultConnection")
{
}
public DbSet<Module> Modules { get; set; }
void ISolnetDataSource.Save()
{
SaveChanges();
}
IQueryable<Module> ISolnetDataSource.Modules
{
get { return Modules; }
}
}
and my controller defined as
[HttpPost]
public ActionResult Index(CreateModulesViewModels module)
{
ViewBag.ListModule = _db.Modules.ToList();
if (ModelState.IsValid)
{
var model = new CreateModulesViewModels();
var CreateModule = new Module();
CreateModule.Name = module.Name;
CreateModule.ModuleDescription = module.ModuleDescription;
CreateModule.DateCreated = DateTime.Now;
//_db.modules.Add(CreateModule);
_db.Save();
return View(model);
}
return View(module);
}
The challenge I have with this is that I could'nt do _db.modules.Add(CreateModule) (the commented line in the controller) so as to add a new record. What I'm doing wrong. I want to do this using the best approach applicable.
What about:
_db.modules.Entry(CreateModule).State = System.Data.EntityState.Added;
I have an entity class Person and its corresponding DTO class PersonDto.
public class Person: Entity
{
public virtual string Name { get; set; }
public virtual string Phone { get; set; }
public virtual string Email { get; set; }
public virtual Sex Sex { get; set; }
public virtual Position Position { get; set; }
public virtual Division Division { get; set; }
public virtual Organization Organization { get; set; }
}
public class PersonDto: Dto
{
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public Guid SexId { get; set; }
public Guid PositionId { get; set; }
public Guid DivisionId { get; set; }
public Guid OrganizationId { get; set; }
}
After receiving a DTO object I have to convert it into a person entity. Now I do it completely manually. The code looks like this.
public class PersonEntityMapper: IEntityMapper<Person, PersonDto>
{
private IRepository<Person> _personRepository;
private IRepository<Sex> _sexRepository;
private IRepository<Position> _positionRepository;
private IRepository<Division> _divisionRepository;
private IRepository<Organization> _organizationRepository;
public PersonEntityMapper(IRepository<Person> personRepository,
IRepository<Sex> sexRepository,
IRepository<Position> positionRepository,
IRepository<Division> divisionRepository,
IRepository<Organization> organizationRepository)
{
... // Assigning repositories
}
Person Map(PersonDto dto)
{
Person person = CreateOrLoadPerson(dto);
person.Name = dto.Name;
person.Phone = dto.Phone;
person.Email = dto.Email;
person.Sex = _sexRepository.LoadById(dto.SexId);
person.Position = _positionRepository.LoadById(dto.PositionId);
person.Division = _divisionRepository.LoadById(dto.DivisionId);
person.Organization = _organizationRepository.LoadById(dto.OrganizationId);
return person;
}
}
The code is in fact trivial. But as the number of entities grows so does the number of mapper classes. The result is lots of similar code. Another issue is that when there are mode associations I have to add constructor parameteres for additional repositories. I tried to inject a some kind of a repository factory instead, but it smelled a bad-known Service Locator so I reverted to an original solution.
Unit testing of these mappers also results in a number of similar-looking test methods.
With all this been said I wonder if there exists a solution that can reduce the amount of manually written code and make the unit testing easier.
Thanks in advance.
UPDATE
I'd accomplished the task with Value Injecter but then I realized that I could safely remove it and the rest would still work. Here is the resulting solution.
public abstract class BaseEntityMapper<TEntity, TDto> : IEntityMapper<TEntity, TDto>
where TEntity : Entity, new()
where TDto : BaseDto
{
private readonly IRepositoryFactory _repositoryFactory;
protected BaseEntityMapper(IRepositoryFactory repositoryFactory)
{
_repositoryFactory = repositoryFactory;
}
public TEntity Map(TDto dto)
{
TEntity entity = CreateOrLoadEntity(dto.State, dto.Id);
MapPrimitiveProperties(entity, dto);
MapNonPrimitiveProperties(entity, dto);
return entity;
}
protected abstract void MapNonPrimitiveProperties(TEntity entity, TDto dto);
protected void MapPrimitiveProperties<TTarget, TSource>(TTarget target, TSource source, string prefix = "")
{
var targetProperties = target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).OrderBy(p => p.Name);
var sourceProperties = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).OrderBy(p => p.Name);
foreach (var targetProperty in targetProperties) {
foreach (var sourceProperty in sourceProperties) {
if (sourceProperty.Name != string.Format("{0}{1}", prefix, targetProperty.Name)) continue;
targetProperty.SetValue(target, sourceProperty.GetValue(source, null), null);
break;
}
}
}
protected void MapAssociation<TTarget, T>(TTarget target, Expression<Func<T>> expression, Guid id) where T : Entity
{
var repository = _repositoryFactory.Create<T>();
var propertyInfo = (PropertyInfo)((MemberExpression)expression.Body).Member;
propertyInfo.SetValue(target, repository.LoadById(id), null);
}
private TEntity CreateOrLoadEntity(DtoState dtoState, Guid entityId)
{
if (dtoState == DtoState.Created) return new TEntity();
if (dtoState == DtoState.Updated) {
return _repositoryFactory.Create<TEntity>().LoadById(entityId);
}
throw new BusinessException("Unknown DTO state");
}
}
Mapping of each entity is performed with a concrete class derived from BaseEntityMapper. The one for Person entities looks like this.
public class PersonEntityMapper: BaseEntityMapper<Person, PersonDto>
{
public PersonEntityMapper(IRepositoryFactory repositoryFactory) : base(repositoryFactory) {}
protected override void MapNonPrimitiveProperties(Person entity, PersonDto dto)
{
MapAssociation(entity, () => entity.Sex, dto.SexId);
MapAssociation(entity, () => entity.Position, dto.PositionId);
MapAssociation(entity, () => entity.Organization, dto.OrganizationId);
MapAssociation(entity, () => entity.Division, dto.DivisionId);
}
}
Explicitly calling MapAssociation protects against future properties renamings.
You can have a look on the two most used Object-Object mapper:
AutoMapper
AutoMapper is a simple little library built to solve a deceptively
complex problem - getting rid of code that mapped one object to
another. This type of code is rather dreary and boring to write, so
why not invent a tool to do it for us?
Value Injecter
ValueInjecter lets you define your own convention-based matching
algorithms (ValueInjections) in order to match up (inject) source
values to destination values.
There is a comparison article on SO: AutoMapper vs ValueInjecter
You can use GeDA for mapping any entity to a DTO object, it comes with either annotations or DSL support.
http://inspire-software.com/confluence/display/GeDA/FAQ
There are only basic examples on the wiki but jUnits of source code are full of useful examples
You can get it from sourceforge or google code manually or via maven dependency
Details are here: http://inspire-software.com/confluence/display/GeDA/GeDA+-+Generic+DTO+Assembler