1

I am building two C# three-tier applications that get their data a very old database Customer table that has about 100 columns. They perform some logic in the business layer and a presentation layer displays the data.

The layout of the customer table is -

CustomerID
Firstname
Lastname
DateOfBirth
Othervalue1
Othervalue2
Othervalue3
.
.
Othervalue95
Creationdate
Updatedate

For these two applications I only need the customer table, but I am building the a new data access layer with Entity Framework. Future projects will need access to other tables and will add to this access layer.

I will use the Unit of work and repository patters.

My problem is the following -

Application A needs one subset of the customer table columns

and

Application B needs a different subset of the customer table columns (there is some overlap with Application A's needs)

How do I perform mapping from the data layer to these two independent business layers? I know I can use automapper to perform mapping from an data entity class to a business layer class, but I will have two different business layer Customer classes.

I have been reading a bit about DTO's but I don't see where there should go in this n-tiered application.

4

4 回答 4

3

You can use any of these based on your application design
1 - Map customer table to two or more Entities
2 - Have a base entity like CustomerBase and 2 or more sub entities.
3 - DTOs are Data Transfer Objects and they are (normally) mutable. Changing them wont result a CRUD operation on the DB.
DTOs are used in scenarios like: Customer entity is a heavyweight object(say 100 columns) and you just need a subset of that data (say 20 columns). Conversion between the DTO object and the actual Entity could be implemented in many ways like auto-mapper, manually operator oveloading and more ...

Hope this helps

于 2013-02-10T21:42:12.880 回答
1

Heres what I would do:

In Application A - extract the data you need, and map them to a new class (a DTO) Do the same with Application B.

If the two applications have no other relation, than sharing af database, you should do nothing else. that is: keep Application A unawer of the existence og Application B, and vice versa.

The DTO is just a fancy name for the object you share across an n-tier application.

于 2013-02-10T21:59:58.950 回答
1

You could use IOC and inject the relevant mapper into your data layer.

Do you need to use UoW and repository pattern? EF is a data access abstraction with unit of work in its own right. You could access DBContext in your business layer and use Linq to project data into DTOs.

于 2013-02-10T22:20:13.630 回答
1

Do you need read only access on the legacy system?

In this case I suggest to use an Anti Corruption Layer pattern on each applications and two distinct entity framework models where you can map only the required columns.

If you need also to write it is probably difficult, you must map all the columns.

If you want to use a single entity framework model then you can use dtos to map a subset of data. In this case auto mapper can be a good solution.

于 2013-02-10T22:48:04.070 回答