0

Now I want to develop a web application using ASP.NET MVC on an existing database and I'm aiming high testability.

If I will work on an existing db, I have to add an .edmx file to a project in my solution and work with that. Am I wrong about that?

Can you suggest an architecture for this case including generic repository, unit of work patterns and dependency injection? I googled a few hours but I've just found samples on code first approach. Do you know an article or an open source project that I can study on?

Thanks,

3 Answers 3

1

You can use DDD, a MVC App (UI) , a WCF as a Service Layer, etc. Your EDMX file can generate other basic stuff with a little of knowledge about T4. However you need to read a lot about the pattern and try to do your own implementation. Remember that EF Database First is only an approach of a technology of a data access solution, try to review Code First implementation and adapt to Model First approach.

Solution Explorer

You can check CH9 https://channel9.msdn.com/coding4fun/blog/SocialGoal-ASPNET-MVC-5-EF-6-Code-First-AutoMapper-Autofac-TDD-and-DDD-Sample-Site

Sign up to request clarification or add additional context in comments.

1 Comment

While the OP can use DDD the fact that there's already a db means it will be everything but DDD.
1

If I will work on an existing db, I have to add an .edmx file to a project in my solution and work with that. Am I wrong about that?

Yes, when you generate your edmx, you will have generated partial classes (ie: public partial class Car {...}) that contains data properties. You well have to add your own business rules in your own generated partial class of you project (public partial class Car {...}).

You can also get ridden of your edmx and just keep your data classes in case you want the database to evolve according to your application data model. I that case remove the 'partial' keyword and just move your generated classes in a project. It is possible to automatically migrate your database (add or remove columns, tables) when your application starts.

For dependency injection in EF6, you cannot have an object referencing an interface of another object, or the relationship is simply ignored by entity framework. It is a really missing feature (in comparison with NHibernate) but it will be included in EF7.

A Dbcontext implements a unit of work pattern, but you can implement your own for example if you use several databases, for distributed transactions.

1 Comment

you can also use code-first model, generating it from an existing database msdn.microsoft.com/en-us/library/jj200620.aspx
0

Now I want to develop a web application using ASP.NET MVC on an existing database and I'm aiming high testability. [..]Can you suggest an architecture for this case including generic repository, unit of work patterns and dependency injection?

This is almost an oxymoron. If you already have a db you have a problem since everything would be driven by the existing db schema instead of the actual use cases. Technically what you want is to rewrite an app in order to be testable but I think you mean maintainable while keeping the existing data.

It does matter how simple or complex your app needs are. If it's a CRUD app i.e you take data from user ,validate it then put in the db then do some queries then things are pretty easy.

You take each use case (add data, get data by criteria etc) and write a test for it. You don't need repositories, unit of work etc. You can use EF directly (if you really want to use an ORM in the first place). Don't worry about mocking things, your app is no more that an UI for the db, you can test things directly with EF with a database.

If CRUD then KISS. You don't need an architecture, don't waste your time and don't complicate your life.

If your app models business objects (which are more than just validators, they represent concepts and domain processes), it's something that evolves in time, it uses a business specific language, then it's a different story.

However, it will be way to broad for this post (and honestly I highly doubt it you have this kind of app) because it would mean to go the DDD route, which means you have to understand a lot of concepts and patterns before writing any code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.