ASP.NET MVC add HiddenInput attribute to Model in DLL - asp.net-mvc

I'm working on an asp.net MVC 3 application which is using Data Models from a compiled library. However I would like to be able to add the following declaration to some data model properties in the compiled dll:
[HiddenInput(DisplayValue = false)]
The problem is that I don't have the source for the DLL and the author doesn't want to introduce a dependency on System.Web.Mvc. Is there a way, using partial classes or something like that, that would allow me add this attribute?
Mark

No, there is no way. Attributes are baked in the metadata of the assembly at compile time and existing classes cannot be modified at runtime. As far as partial classes are concerned they only work within the same assembly.
Also if the authors of this assembly don't want to introduce a dependency in their library with System.Web.Mvc they probably have reasons for this. Obviously, you, as an MVC developer should use view models which are classes specifically tailored to the needs of your views and then map between the domain models (stuff that comes from different libraries, ...) and view models. Then you would pass those view models to the view and not the domain models. Of course your view models will have all the necessary metadata and formatting such as DisplayName, Hidden, ... To ease the mapping between those two classes you could use AutoMapper.

You could map your compiled library Data Models to a set of local models.
If you were to map your compiled library Data Models to your own set of local models you could do what you like.
You can do this manually or look at a tool like AutoMapper.

Add reference to system.web.mvc to your class project.

Related

Entity Framework 5.0 Database First Approach

I am trying to implement Database first approach using Entity Framework 5.0 but somehow I am not getting it right. I have the following doubts which needs to be cleared.
1.After adding the Ado.Net Entity Data Model a DBContext class 'Model.Context.cs' gets auto-created in the folder under "Model.Context.tt".
Do i need to add DbContext Generator again?(I have found this recommended by others but i could not make out why!)
2.How to scaffold a controller from the edmx files?
Suppose I have an entity, say A (which I want to scaffold to a controller),having one to many relationship with entity B, where will I define this relationship? In the auto-generated model classes from edmx files or do i create classes A & B and define again and then scaffold Model A?
Any help will be very much appreciated. Thank you
Abhatt:
What t4 templates do is generating classes for you and you need to keep them, unless you decide to use another t4 template.
For instance, you may want to design you database but after that decide to use code first to take advantage of code first approach, in that case after designing the database you will add another t4 template named "EF 5.x DbContext Fluent Generator for C#" and that template creates the poco class and all mappings for you.
Whenever you are adding a controller mvc uses scaffolding to create controller's methods and views. However, if you want to have more control on how to generate them, you may install MVCScaffolding from package manager console. Having MVCScaffolding installed, you will be able to customize t4 templates.
For more info check out MVC Scaffolding project on CodePlex:
http://mvcscaffolding.codeplex.com/
also there is another good one:
http://www.codeproject.com/Articles/468777/Code-First-with-Entity-Framework-5-using-MVC4-and

Where to put Entity Framework Data Model in MVC application?

Lets consider default ASP.NET MVC application folder structure, so it's looks like this:
-App_data
-Content
-Controllers
HomeController.cs
-Models
AccountModels.cs
-Scripts
-Views
My question is: Where is the best place to put Entity Framework Data Model (EDMX) file? Is it Models folder? Yes - we know that good solution is to introduce new Project and reference it to MVC application, but lets forget about this now.
For a small project, it should be part of the Model. For a larger product, the repository and the associated model could be in a separate assembly.
Well this is debatable, but i'd vote +1 for the Models folder.
The only other candidate would be App_Data, but this is generally for file-based databases (SQL Server CE .MDF, for example) and files you don't want served by IIS.
As the EDMX is an abstraction of the database, it should go into the Models folder.
If the project gets bigger, you should definetely move your EF Model into another project. To future-proof yourself from this, make your Controllers access the EDMX via Repository/Interfaces, so when you move the DAL over to another project, all you'll have to do is add the reference and add in the using statements.
I would put the EF-model (aka physical model) always in its own assembly or in a "core" assembly outside of main MVC application. The same applies for your business-logic / domain-logic / domain-services / etc. Separate the non-web stuff from the MVC-Web-Application.
This will help you re-use the core part of your app. For example when you need to expose it as a service, a command-line tool, migration-tool, etc.
Because storing this in its own assembly is so easy and takes you a few minutes I highly recommend doing this for each and every tiny app too.
My opinion is that you should create
a separate project for domain objects, datacontracts etc. etc...
Like MyProject.Infrastructure including many folders like
DataContracts, Model, Exceptions etc.
a separate project for DataAccess wich contains the DBContexts and the Repositories, this way you can easily manage migrations later on

ASP.NET MVC - Strongly typed view model, where does it belong?

I'm trying to create a strongly typed view model as John Sheehan suggests here. Where should it go? I can make arguments to myself for Model, View, and Controller.
It should go in the "Models" directory of the web app. ViewModels are by definition specific to one or more views and therefore belong in the web app, not the core.
You could define them in the controller that uses them, but this doesn't scale. Same with defining the class in the view code. Even though one-class-per-file means more files, its easier to find code and easier to maintain.
I'll often create a subfolder for each controller, so I end up with things like Web.Models.Foo.BarViewModel.
If have them in my Domain project in a PresentationModel directory and like #Seth Pretry-Johnson, I have them in separate Controller directories.
This is my overall structure of a project:
Website Project
Controllers
Views
Etc
Domain Project
Models
Repositories
Abstract
Services
Abstract
PresentationModels
Home
User
Etc
DataAccess Project
Repositories
HTHs (and doesn't raise more questions.. ;-),
Charles
I put the actual model class in the Models folder.
/Controllers
/Models
/Entities
/Mappings
/ValueTypes
/ViewModels
Something like that. I'm a big fan of the Fluent NHibernate.
It can go wherever you want it to go, why do you need someone to tell you where to put a class?
A lot of people have the wrong idea that, unless you put your classes inside some specific directory grouped by functionality, things will not work. This might be true with other frameworks, but with ASP.NET MVC it's not true. Code is compiled to assemblies.

ASP.NET reference a LINQ to SQL class in Silverlight from its origin in the models of MVC?

I am working on an mvc app that uses some silverlight to supplement a page and instead of having to maintain two separate linq to sql classes I want to add a reference to the main project from the silverlight project but this can't be done through the normal method of just adding a reference, anyone have a workaround?
The normal way to do this is to create a new Silverlight class library project, and use "add as link" to add the existing files from your non-Silverlight project. You can then reference the new project in your main Silverlight project without duplicating any of the files.
Note that if you want to add the LINQ to SQL classes, just add the generated .designer.cs file to the new project--AFAIK dbml files themselves aren't supported in Silverlight projects. You will also need to stub out the L2S attributes present in the classes: ColumnAttribute, FunctionAttribute, and so on.
This may be more trouble than it's worth--if your goal is to communicate with the server using classes generated from a database, you might consider using the Entity Data Model with ADO.NET Data Services (the combination of which is intended for this purpose) instead of LINQ to SQL.

asp.net-mvc where do i put my own code

is there any particular directory that i should put my code into in an asp.net mvc project
i have some extentions to the HtmlHelper class. Right now i have it sitting in the Content folder. is this correct? is there a better soluiton?
I usually create a separate project (or projects) for my own code, including my data layer, as class libraries. I then reference the libraries in my MVC web site.
you can put code wherever you want, but typically you want things organised. heres how i do it:
2 assemblies
MyProject.Domain
this contains all my domain code; business logic and entities
MyProject.Web
this contains controller code, views and assets like css/images
Your HtmlHelpers belong in the .Web project because they are mvc related (nothing to do with the domain). You probably want a new folder called Helpers or Extentions. Its really up to you, the key point is to decide where something belongs and to namespace it accordingly
I agree with what everyone else said, here's how one of my solutions would look like:
1- MyProject.WebUI
2- MyProject.DomainModel
3- MyProject.Test
4- MyProject.Extensions
This extensions project is new to me (actually since I knew about extension methods). It usually concludes sub-folders describing what the extension methods are used for, for your particular case, the folder name would be HtmlHelpers. I then reference this project (or its output library when using elsewhere). HTH
If you are going to re-use the same HTMLHelper extensions in different ASP.NET MVC projects, I'd suggest putting them in a class library which is completely seperate from your project.

Resources