Arraylist in Visual Studio .net 2005 - c#-2.0

How can i store three different objects in a single array list for populating tree view?

Well, generally this sounds like a bad idea. Please describe why you want to do this.
If possible, a clean way to do this is to create a base class for all three objects that your tree view would bind to. (There may be other interesting solutions, like creating a Facade/Adapter object over the three.)

Related

Are there Visual Studio Tools For Creating DTO/ViewModel

I'm trying to introduce ASP.Net MVC to my department. I am encouraging them to have a ViewModel per View and AutoMapper for our larger projects (and ideally in general).
Sometimes this means having one large entity and picking 5 of its properties and creating the ViewModel. This is done by looking at the the edmx model (many projects were existing so it was DB first) and then creating matching properties in a ViewModel class. Obviously names etc have to match for AutoMapper to work. Also for navigation properties you have to add the navigation name first. Ideally also being able to type in a display name etc.
I'm trying to ease them into doing this (what they see as extra work). Is there any tool that would load a list of fields and allow you to select via checkbox etc and create the class from that?
I guess the same would apply to DTOs etc
Thanks
Have you tried this http://www.codesmithtools.com/product/generator
you can create a template of the dto and then it will generate the files/dto's for you as needed from any kind of datasource.

Best practice question - Working straight with Linq to sql classes

This is possibly a bit of a stupid question, but I am getting confused due to the ASP.NET MVC book I am currently reading...
Working with Linq-To-SQL it seems to say that it is not good practice to pass the Linq-to-SQL objects straight to the controller, but that each object should be modelled separately first and this should be passed between the controller and the repository.
Say, I have a database of products. Linq-to-SQl creates a product class for me with Name, Price and Whatnotelse properties. I could pass that straight from repository to controller and then view, but instead it seems to recommend that I use and third class, say Product_Entity, with also Name, Price etc. properties and pass that to the controller.
I fail to see the benefit of this approach, except possibly for adding attributes to the properties... But apart from that it seems to have more drawbacks than benefits. Say each product has manufacturer information as well, I don't see how I can model that easily in my third class.
Is this approach really best practice? Or did I misunderstand all that? If so, why is it bad to work straight off the linq-to-sql generated objects? And how do you deal with relationships between objects in y
The huge benefit to this other class you create is that, to use your example, it doesn't necessarily map to either a product or a manufacturer. Think about it like this:
Your Linq to SQL classes are meant for talking in the "data" domain.
Your "data" classes (the ones you're having trouble with) are meant for talking in the "application" domain.
Let's take an example. Suppose in your MVC application you wanted to show a grid of information about products. You want to see their Name, Price (from the Product table) and their Country of Manufacture and Manufacturer name (from the Manufacturer table). What would you name this class? Product_Manufacturer? What if later on you wanted to add properties from yet a third table such as product discounts? Instead of thinking about these objects in purely the data domain, think about them with regard to your application.
So instead of Product_Manufacturer, what about calling it ProductSummaryItem? Each property of the ProductSummaryItem class would map 1:1 with a field shown in your grid on the UI. Your controller would perform the mapping between the information in the data domain (Product, Manufacturer) with the custom class you'd created in the application domain (ProductSummaryItem).
By doing this, you get some awesome benefits:
1) Writing your views becomes really, really simple. All you have to do to display your data is loop through the ProductSummaryItems and wrap them in and tags, and you're done. It also allows for simple aggregation. Say for example you wanted to add a field called ProductsSoldLastYear to your ProductSummaryItem class. You could do that very simply in your views because all it is to them is another property.
2) Since the view is trivial and there's mapping logic in the controller, it becomes much easier to test the controller's output because it's customized to what the view is going to see.
3) Since the ProductSummaryItem class only has the data it needs, your queries can potentially become much faster because they only need to query for the fields that would populate your ProductSummaryItem object, and nothing else. This overhead can become overbearing the more data-domain objects make up your ProductSummaryItem object.
This pattern is called Model View ViewModel (MVVM) and is hugely popular with MVC as well as in frameworks like WPF.
The argument against MVVM is that you have to somewhat reimplement simple classes for CRUD operations. Fair enough, I guess, but you can use a tool like automapper to help out with things like that. I think you'll find fairly quickly, though, that using the MVVM pattern even for CRUD pays dividends, because before you know it, even with simple classes, you'll start wishing you had extra fields which can easily drive your views.

Use object method in asp.net MVC view

A month or two ago when I started doing asp.net MVC work I remember reading that it is not recommended to call object methods like the following in the view:
<%: Html.ListBoxFor( m => Model.SelectedId, Model.SelectItems.ToSelectList() ) %>
That above is just an example, not a direct line of code I am using. I am using extension methods to turn my enumerables into ListBox binding-ready IEnumerable collections. I know there was a concern that someone had raised in an article about this, but I cannot remember what it was. I am not actually coding it this was at the moment, but the way I am doing it is becoming a burden. I am writing extension methods to convert all of my IEnumerable objects into both SelectListItem collections and now a MultiSelectList object. So my view models are becoming bloated with "potential" ways of displaying the data. Instead of their just being an enumerable of available options, I am tracking 4 new pieces of data: SelectListItem collection, SelectListValue object, MultiSelectList control, MultiSelectListValues collection.
I am not opposed to keep going about this, but I would like to remember why it was recommend not to call the extension method on the View if I'm using strongly-typed full/partial views. I tried to find that article and others like it but I could not. I wish my memory was better than this, but hopefully someone out there knows this offhand and would be willing to share it with me!
Thanks in advance!
The idea your talking about is based on a principal that the view in MVC should be as simple as possible. Because it is the most difficult to test unnecessary bloat should be avoided.
Your example line could be paired down a few different ways. You could try writing an extension that takes the enumeration from your model and converts it to a select list that can be fed into a listbox, or you can change the model's property to return an enumeration of list items. The later is my preferred method. The model exists to organize the data to be consumed by the view so theres no reason to have it output data that cannot be consumed by the view directly. Either one of these methods would take complexity out of the view and place it somewhere that is easier to unit test.
Hope this helps.

Converting DTOs to View Models

Does anyone know of a good (read: quick to code) method for converting DTOs to View Models or mapping DTO members to View Model members? Lately I've been finding myself writing many conversion and helper methods but this is a very arduous and tedious task. Moreover, it will often needs to be done twice (DTO -> View Model, View Model -> DTO).
Is there a methodology, technique, or technology which would allow me to do this more quickly and efficiently?
Have a look at Automapper. It is an open source project that addresses exactly the problem you are having.
As suggested by David, Automapper is highly flexible. If you have simple mapping need, check out the object mapping feature of Fasterflect, a library I co-authored. It offers very high performance (use CIL generation in the backend, instead of reflection) and is very easy to use.
I found Otis Mapper better than AutoMapper , which facilitates mapping a collection of entities to DTO collection .
Please find the link below.
Otis
Hope this helps.
Thanks ,
Vijay
I see automapper as the best solution for DTO Mapping

Using a View Model + Data Model in ASP.NET MVC to support typed views?

How do most developers handle Typed Views in ASP.NET MVC when dealing with large applications? We are considering putting View-specific models in the Models folder, then putting all domain objects into a separate project. This way our Controllers can easily add the domain object to the typed view, without the domain object needing to be aware of the View layout itself.
For example, if we have an Employee object with:
Id
First Name
Last Name
Status
Then our Employee View might use a ViewEmployeeModel object with:
Employee object
List to populate Status drop-down
etc
Is this a sensible approach? Are there better ways to accomplish the same thing? It seems a little strange since I'd basically have two models (one for the view, one for the business objects), but isn't it better than using untyped views?
I do this almost as a rule, because:
It lets you design the app view-first instead of DB-first, which is nice when working with customer representatives.
Views typically have a much "flatter" object graph than, say, Entity Framework models. LINQ makes it easy to map these.
The views and the data model can evolve much more independently.
It's typically easier to model bind to a flat view model with, say, FK IDs than an entity model which expects fully materialized related objects.
You don't have to worry about accidentally exposing "secret" properties or whitelisting properties for updates.
Don't have the reputation to comment, but Craig is right. It's a variation of the Model-View-ViewModel pattern. There's a good article on it available at Los Techies.
The article also uses the AutoMapper code that mgroves pointed out so you should be able to kill two birds with one stone.
I think this is a pretty sensible approach. One thing that might help you out is AutoMapper.
Seems fine, you have the advantage that the model only contains information needed by the view and not lots of pesky business logic functions / values.

Resources