There are certain methods in my ViewModel that access a database connection. I know this is not correct since the ViewModel shouldn't be responsible for connecting to a database.
So as a solution, I moved some of my methods to another class as extention methods. I.e.
Public static void (this MainViewModel viewModel){
viewModel.Textbox = "hello";
viewModel.Tables = GetDatabaseConnectionAndReturnTables();
//...//
}
Is this really a way to get database connection functionality out of the viewmodel? This is the only solution I could come up with.
Thanks,
Luke
Is this really a way to get database connection functionality out of
the viewmodel?
Short answer: no.
Not that long answer: extension methods are meant to add methods to types when source code isn't available, you can't derive a class or you want to add them to a struct (struct aren't inheritable...).
Actually you're adding methods to your view models as if they were added as direct class members. Either if you implement them as view model instance members or using extension methods, you're implementing methods in the view model.
If you want to effectively abstract data from view models you should check some design patterns like:
Repository.
Unit of Work.
Domain model.
Service layer.
Inversion of control.
DTO.
It would be hard to provide you more guidance about how to achieve a true separation of concerns in a Q&A format (I would need to write a book here), but I would start learning more about these design patterns.
Also, I would take a look at what's domain-driven design.
Going this route could get pretty wacky. If there was a choice between extension methods and nesting the functions within the view-model, then the latter would be preferred.
However... both are pretty bad practice. You should read up on the DAO and Repository design patterns. Using the DAO pattern, you hide the details of the database, only exposing the knowledge of a persistence layer. Then with the Repository pattern you expose methods that allow you to manipulate a particular data-set without any care for how that data is retrieved and/or stored.
Using dependency injection you would then inject the repository into a model that does a specific task with that data. Again, you would then pass that model into your view-model which simply displays the information that is available from the model.
One of the big reasons why you keep your data access out of the view models in MVVM is to help with testing. One of the huge advantages of using MVVM is in making more of your code testable, without taking a dependency on the View. By making your view model call an extension method, you have no way of abstracting out the data store calls. This means that when you do your unit testing, you must always have data in the database, ready to be queried against. It also means your data base will fill up with a bunch of crap data that is inserted when your unit tests are ran. You want to hit your data-base when you are writing integration tests, but not really when you are just testing the functionality of your view models.
Abstracting the data access out in to a service or repository, that is hidden behind an interface allows you to test your code, without having to deal with having a connection to the database. You would create fakes or mocks.
This way, your code would do this:
private IMyRepository repository;
public MainWindowViewModel(IMyRepository repo)
{
this.repository = repo;
}
public Task SaveObject(MyObject obj)
{
return this.repository.Save(obj);
}
When you unit test, you just provide a mock, or fake, to your view model constructor and let it hit the save method on those instead.
Your use of an extension method still means you are tightly coupled to your data-layer. It might be abstracted in to a different file, but your view-model is still tightly coupled due to the fact that you can't swap out the data-access without re-writing the original extension method.
The other benefit of this, is that other view models may use the same methods if they need to ask for data. This saves having to copy & pasting the code into extension methods for each view model that might need it or creating an instance of a view model just to get access to its extension methods to fetch your data. View models are typically limited to one view, but the data they are provided with can be used on n views.
Related
Based on the answers mentioned here, I understood that I should put the business logic inside the model itself, while in my program I am using EF directly inside the actions of the controller for example to get the list of cars from the database directly I am doing the following:
public ActionResult CarList()
{
using(var _db = new CarRentEntities())
{
var result = _db.Cars.Where(m=>m.Active);
return View(result);
}
}
what is the impact on my website performance if I will use the mentioned above code inside controller or inside Model?
which method I should use? for example if I want to work with a team, is there a standard I should follow to separate the code, kindly advise
for using the repository pattern: I read that we should not use if as mentioned for example here , i will copy some of what mentioned:
The single best reason to not use the repository pattern with Entity
Framework? Entity Framework already implements a repository pattern.
DbContext is your UoW (Unit of Work) and each DbSet is the repository.
Implementing another layer on top of this is not only redundant, but
makes maintenance harder.
if my database contains the following tables: Manufacturers , Cars , Rent , Clients , rent class is the a table with 2 foreign keys between Clients and Cars and contains other detailed fields.
how to deal with Rent Object which need to get data from 2 different repositories Cars and Clients in order to display the renting grid based on search criteria entered by the user, if I will use the repositories Cars and Clients , they have their own dbContext, BOOM my head cannot understand this technique, kindly advise
The answer to your question is, it does not really affect performance but it will definitely become an issue in terms of maintainability as the application grows bigger. You can adopt the SOLID architecture principles: SOLID architecture principles using simple C# examples. This enables you to develop high quality software.
You can create a multi-layered application:
Interface Layer - MVC application
Business Layer - Class Library with classes with logic
Data Access Layer - Database Contexts and Repositories, unit of work with CRUD operations
Shared layer - Logging, AppSettings, validations, utilities, extensions, constants, enums
Having your application in this structure would require you to consider things like inversion of control, dependency injection and many more to ensure loosely coupled classes, easy unit testing and most of all a solid application.
You can also read this: Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application
Generally the Model is a "unit" - i.e. it is a model of the data you want to display. The Controller is an "integrator" - i.e. it pulls together the various resources required to render your web page. You may wish to create a database fascade class which does something like this;
public ActionResult CarList()
{
using(var carStore = new Factory.CreateCarStore())
{
var result = carStore.GetActiveCars();
return View(result);
}
}
To separate your database access from your web controller (this would make it more test-able as well because you can substitute a different CarStore implementation (i.e. a Test XML data set) for testing purposes.
My question is, how do I add a custom query to a model in asp.net mvc , all the tutorial I had seen they make the query in the controller.
So far in my reading everything says that the controller should no be aware of the database, all the queries should be made in the model but I can not seem to find any example of this.
I try to do it but I do not have the DbContext available in the model, so how should I do it?
There are multiple ways to achieve this and each has its own pros and cons. Here are a few appraoches:
1) Domain Model Pattern
The author of the domain model pattern, Martin Fowler, provides this definition (Fowler, 2003):
An object model of the domain that incorporates both behavior and data.
Using this pattern, your domain models would define behaviors, which may or may not translate into DB queries.
2) Repository Pattern
Use a repository to separate the logic that retrieves the data and maps it to the entity model from the business logic that acts on the model. The business logic should be agnostic to the type of data that comprises the data source layer. For example, the data source layer can be a database, a SharePoint list, or a Web service.
As #Mehrdad has pointed out, using this pattern frees up the controller DB concerns and all your DB logic remains in one place.
3) Command Query Separation pattern (my favorite)
It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.
Note: this is CQS and NOT CQRS pattern
The difference between CQS and Repository pattern is that with Entity Framework, DbContext already wraps up repository pattern for you (DbContext being the Unit of Work and DbSets being the repositories). So creating another repository layer is redundant. What CQS gives you is fine grained control over your queries/commands and allows you to extend them via decorators which can handle additional logic without polluting your core business logic. Here are some great links about CQS:
Meanwhile... on the command side of my architecture
Returning data from command handlers
Meanwhile... on the query side of my architecture
This answer is a great example of how repository pattern can get be used with CQS together.
All of this can be quite overwhelming, so I suggest that you take your time, implement Proof-OF-Concept projects using these patterns and decide which one suits your overall architecture better.
You usually can use Repository pattern for that for example if you have an user entity:
public class UserRepository:IUserRepository{
public List<User> GetUsers()
{
//Your code and query here
}
public void AddUser(User user)
{
//Your code and query here
}
}
Then you pass this class to your UserController and call it's functions. As you can see I also added IUserRepository so you can use it if you do Dependency Injection
I have an ASP.NET MVC3 in C# and Razor. The architecture of the application is divided in Data Access Layer (EF classes + Repository), Service Layer, Controller, ViewModels and View.
From my Service Layer ProductServices I call the method GetAllProducts exposed by my Repository ProductRepository , which has the following signature:
IQueryable<Products> GetAllProducts()
Therefore within ProductServices I call (productRepository is an instance of ProductRepository):
var products = productRepository.GetAllProducts();
that populates the variable products. Now I would like to access the name of the product ProductName from productServices. If I use this instruction:
var productNames = products.Select(m => m.ProductName).ToList();
I am creating coupling between ServiceLayer and the EF (bypassing the repository). That means I must add to ProductRepository a method with signature:
IQueryable<string> GetAllProductsName()
However, since I need other product information in my application, shall I create one method in productRepository for each field of the Product class? Is my reasoning correct? Thanks
Theres two schools of thought around this,
A repository explicitly defines the way you interact with the database and should be tightly controlled. Therefore methods on the repository should provide enumerated data
The repository breaks the strong coupling to a specific datasource type but does not need to provide detailed and enumerated datasets.
Personally i subscribe to the second, heres why:
my feeling is that when you get overly explicit within the repository it turns into business logic rather than a decoupling mechanism. I don't really like this as it means that you become more tightly linked to the repository implementation.
I also think that in some cases the repository isnt the right place to enumerate the data, for example I believe paging and sorting is a UI concern, however for performance you want queries to only relate to the current page/sort. this means you either need to let the UI contribute to the query compilation or the repository needs to understand paging and sorting.
Having said that providing un-enumerated datasources does open you up for issues later down the track, and even if you do provide them its very important to enumerate the set as soon as possible.
If you're interested heres my take on the repositories: http://blog.staticvoid.co.nz/2011/10/staticvoid-repository-pattern-nuget.html , all the code is also on github
You have all information in your productServices because you load all information from your Repository with the method productRepository.GetAllProducts(). If you need more information from an other entity then you need to extend your productServices with a new Service.
However, since I need other product information in my application,
shall I create one method in productRepository for each field of the
Product class? Is my reasoning correct? Thanks
In this situation normally I create Extensions method's for the service and not in the repository. The Repository have normally a CRUD Setup and nothing more.
When implementing Repository for database in ASP.NET MVC project, is it correct to place business logic into it or may be better to place logic in controller class? Or use additional service and helper classes for manipulating data?
Ultimately there isn't a perfect place for your business logic besides its own layer (as part of the "Model" layer). Often you can get away with a different implementation, but there are trade offs in every case.
The trade off to creating another layer for the business logic is that you have to actually encapsulate your code. If you're too aggressive, you might also get some duplication between your entities and your domain model (if your DB's relational semantics already take care of your buiness logic).
View
The view is the most brittle part of your app, as it is the most likely part to change.
It is also very hard to get business logic correct in your view, due to having to support all the various view state transitions.
It is extremely well known these days that you just don't do this :)
Repository
The issue here is one of maintenance and purity of abstraction. Violating this can confuse people and make your app hard to maintain.
From the P of EAA article on the Repository pattern:
A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection
A repository is an abstraction that presents your data storage as a collection that holds domain objects.
No domain logic should reside in it. Instead, it should exist in your domain objects (by definition, as your business logic is your domain).
To do otherwise (to make your repository do double duty and also validate domain logic) would be a violation of SRP (Single Responsibility Principle), and would be a code smell.
You can have higher level domain objects that work with collections of domain objects to validate domain logic (such as dependencies within a collection of objects, size limits, etc). They will still use your repositories under the covers to do final storage/retrieval of domain objects, so they won't be doing double duty (so won't violate SRP).
Controller
The controller is also not a good place to put business logic. The controller's job is to mediate between the controller and the model.
The model is the domain, and the domain is your business logic.
Entities
You might consider putting domain data in entities.
But you must be careful when accessing navigation properties if the entities are attached, as you can trigger inadvertent DB queries or exceptions (depending on if your context is disposed or not). Detaching them is also a problem, as it destroys your object graph unless you explicitly reattach the objects to each other after detaching them from the context.
If you make separate domain model classes, you might consider treating entities as DTOs only.
Edit: IValidatableObject
I found out just now about a feature in Entity Framework 4.1 that you may want to check out: the IValidatableObject interface.
You can make your entities partial classes, and in the partial class, implement this interface. When you do, the Entity Framework will call Validate on save, and you can call Validate whenever it makes sense for you to do so.
This might help you avoid splitting your persistence model from your domain model in additional cases.
See this article: http://msdn.microsoft.com/en-us/data/gg193959
Side note: Views/View Models
In case you are thinking about it, I suggest you avoid the temptation to pass entities back to the view. It will break in a lot of cases (e.g. Javascript serialization to store view state), and cause unintentional DB queries in other cases. Pass back simple types instead (strings, ints, lists, hashsets, dictionaries, etc), or construct view model classes to pass to the view.
Add a Service Layer to pass models to Repository, where Service classes corresponding to the controller can be added. For example if you use a UserController and User Model, You can pass the User Model to UserService class.
Here the Service Layer can act as a bridge between the Repository and the controller so that the separation of Controller and Repository is well established.
Business logic should be there in your Domain model.
Please look into this answer.
ASP.NET MVC - Should business logic exist in controllers?
I agree with the above, controllers should not be responsible for business logic simply for returning the appropriate views. I use a service layer to provide business logic and view model creation so that a controller simply passes the model returned from a service to a view.
I also ensure my view models are simple DTOs, and the service simply knows how to populate the properties appropriately.
In my opinion it depends on the business logic. Is the logic based on validation of input and input rules, if so it may be best to be on the model. However, if it is business rules based on workflow then that may need to be in a controller, such as, user chooses option A then gets redirected to a different page/form than if option B was selected. If the business rules have to deal with data persistence then it may need to go in the repository (It would seem strange to put it there to me). There's a ton of discussion about this, but it comes down to you or your team's perspective about how maintainable and flexible the end product will be based on the implementation.
I recently began reading Pro ASP.NET MVC Framework.
The author talks about creating repositories, and using interfaces to set up quick automated tests, which sounds awesome.
But it carries the problem of having to declare yourself all the fields for each table in the database twice: once in the actual database, and once in the C# code, instead of auto-generating the C# data access classes with an ORM.
I do understand that this is a great practice, and enables TDD which also looks awesome. But my question is:
Isn't there any workaround having to declare fields twice: both in the database and the C# code? Can't I use something that auto-generates the C# code but still allows me to do TDD without having to manually create all the business logic in C# and creating a repository (and a fake one too) for each table?
I understand what you mean: most of the POCO classes that you're declaring to have retrieved by repositories look a whole lot like the classes that get auto-generated by your ORM framework. It is tempting, therefore, to reuse those data-access classes as if they were business objects.
But in my experience, it is rare for the data I need in a piece of business logic to be exactly like the data-access classes. Usually I either need some specific subset of data from a data object, or some combination of data produced by joining a few data objects together. If I'm willing to spend another two minutes actually constructing the POCO that I have in mind, and creating an interface to represent the repository methods I plan to use, I find that the code ends up being much easier to refactor when I need to change business logic.
If you use Entity Framework 4, you can generate POCO object automatically from the database. ( Link)
Then you can implement a generic IRepository and its generic SqlRepository, this will allow you to have a repository for all your objects. This is explained here : http://msdn.microsoft.com/en-us/ff714955.aspx
This is a clean way to achieve what you want: you only declare your object once in your database, generate them automatically, and can easily access them with your repository (in addition you can do IoC and unit test :) )
I recommend you to read the second edition of this book which is pure gold and updated with the new features introduced in MVC 2
http://www.amazon.com/ASP-NET-Framework-Second-Experts-Voice/dp/1430228865/ref=sr_1_1?s=books&ie=UTF8&qid=1289851862&sr=1-1
And you should also read about the new features introduced in MVC3 which is now in RC (there is a new view engine really useful) http://weblogs.asp.net/scottgu/archive/2010/11/09/announcing-the-asp-net-mvc-3-release-candidate.aspx
You are not declaring the business logic twice. It's just that this business logic is abstracted behind an interface and in the implementation of this interface you can do whatever you want : hit the database, read from the filesystem, aggregate information from web addresses, ... This interface allows weaker coupling between the controller and the implementation of the repository and among other simplifies TDD. Think of it as a contract between the controller and the business.