Globalization and Entity Framework - asp.net-mvc

I have a project where most of the globalization Information is stored in the resx. files and Dataannotations are extensively used.
But now there is a new Poco class which has a text which needs to be localized. What is the best method to do this?
I would do it like this:
public class example
{
public int ItemID {get; set;}
public string Description_en_US {get; set;}
public string Description_de_DE {get; set;}
}
My main concern is how to use this later on in the razor webpage, maybe I will build a method who select a column depending on the thread locale - or does some better mechanism exist?

Related

.Net core viewmodels for consolidating data

First time posted long time reader. I've done a lot of searching and haven't been able to find anything so please excuse me if I missed a past post or something.
In any case I used to be a .net dev and now getting back into .net core MVC. New to MVC and really liking it!
In any case my question is this... is it possible to use a view model to consolidate data? Basically I am writing a timeline from multiple sources taking only the date stamp and description from each. I am hoping to consolidate all into a timeline viewmodel, but not sure if this is appropriate.
EX:
public class order {
public guid id {get; set;}
public datetime orderDate {get; set;}
public string orderDetail {get; set;}
public int orderQuantity {get; set;}
...
}
public class goodsReceipts {
public guid id {get; set;}
public datetime receiptDate {get; set;}
public int receiptQuantity {get; set}
public order order {get; set;}
...
}
public class otherTransactionaldata {
... there are many different transactions such as goodsreceipt
}
then my view model something like this
public class transactions {
public date transactionDate {get; set;}
public string transactionDetail {get; set;}
}
So I basically want to load all transactional data such as (orders, receipts, returns...) into the view model to create a sorted timeline. Am I going down the wrong path here?
Again sorry for the dumb question. I've been reading like crazy and I'm sure I've missed something so I apologize in advance.
Thank you,
E

MVC 5 possible to use only part of a model for the view?

If I have a database and I base all my models on the database (using LINQ basically to fetch/set the data). Is it possible to only use a part of those models to create my views? Say I have three columbs in a table, and I only need two for my view, can I simply use two of the three using my database model or should I create a new model to use in my views.
And if I have to create new models, any simple way to do this? I'm using Visual Studio 2015.
Thank you
It is common to create View Models to represent a slice of your domain model when putting data in a view.
For example you may have a BookViewModel and a BookEditModel. You could show and permit editing of different fields depending on the model.
class BookViewModel
{
[ReadOnly]
public string ISBN { get; set;}
[ReadOnly]
public string Title { get; set;}
[ReadOnly]
public string Author { get; set;}
}
class BookEditModel
{
[ReadOnly]
public string ISBN { get; set;}
[Validate(...)]
public string Title { get; set;}
[Validate(...)]
public string Author { get; set;}
[ReadOnly]
public DateTime LastUpdated {get;set;}
}
If you're looking into an easy way to create these view models, there is a tool called AutoMapper that allows you to easily map between domain and view models.

Repository and ViewModel in asp.net mvc 3

Here is the situation I am struggling with.
I have an Object Model:
public class MyModel
{
public string Prop1 {get; set;}
public string Prop2 {get; set;}
//etc
}
Then I have Object ModelView
public class MyModelView
{
public MyModel MyModelObject;
public SelectList PropToBeSelected1 {get; set;}
public SelectList PropTobeSelected2 {get; set;}
//etc
}
I have the MyModelRepository class as well that does the delete, update operations for MyModel.
All good and clear so far.
Question:
PropToBeSelected1 and PropTobeSelected2 are drop down lists whose contents come from the database. Should the methods retrieving these contents be put in my MyModelRepository? Or should I create another repository for ViewModel?
Thank you.
First you really don't want domian-ish objects in your viewModel. Your viewModel should be clean with only primitives (like strings, ints... etc). So I'd suggest using a AutoMapper to map your two string props to your viewModel.
With the select list, there are many ways to go about this but I can imagine if they are lists of properties then they are not actual entities, but value objects instead. In this case creating a repository for them is over kill and boarders bad design.
I'd put the 'get' of the property lists in your MyModelRepository. Something like
_myModelRepository.getProperties1For(myModel);
Then AutoMap again on to get your select list.
Edit:
Like #M.Radwan pointed out for complex domain models I'll make viewModels insdie viewModels for easy of mapping.
Domain Model--
public class User : Entity
{
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public string Zip { get; set; }
}
would map to
public class DetailsViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public AddressViewModel Address { get; set; }
public class AddressViewModel
{
public string Street { get; set; }
public string Zip { get; set; }
}
}
which in our experience has been the only reason to add any complexity to your viewModel. We will put SelectLists in our viewModel though but lately we've been using IEnumerables of inner viewModels and calling custom EditorFor or DisplayFor to turn them into a dropdown / list of checkboxes / radio buttons.
The answer is NO, you should not make any repository for them if you really need them with this view. so they probably are value objects as #jasonhooten said and they should connected to the main aggregate object that used by the repository
Finally I don't decide the ViewModel structure until I finish the view and make it working first and this why I founder and create DevMagicFake, by using DevMagicFake you will delay all decisions of the design regarding the structure of the ViewModel or the repository or how you will use service layer, all this will be delayed after full complete your view and make it working first as BDD (Behavior Driven Development) and TDD (Test Driven Development) so you can take the right decisions of the design and the object model itself
So I just create the action method as the following
public ActionResult List(MyModelView myModelView)
{
FakeRepository<MyModelView> repository = new FakeRepository<MyModelView>();
repository.Add(myModelView);
}
This fake repository will enable me to save and retrieve my whole model even it's a complex model until I finish and complete the whole view and make it working first, and then I start thanking on how the real design and the real repository should looks like, and do on
For more information about DevMagicFake and this approach see this link
DevMagicFake on CodePlex

Code-First: How to create a many to many relationship where the source class/table is the target one as well?

Can someone please post an example on how to create a many to many relationship where the source class is also the target one, using code-first ?
Something like:
Toys * <-----> * Toys
Thank you.
Nuno Senica
I don't think this is possible with EF Code First. As a workaround, you could create the mapping table yourself:
public Toy
{
public int ToyID {get; set;}
public ICollection<ToyMapping> Toys {get; set;}
}
public ToyMapping
{
public int ToyOneID {get; set;}
public int ToyTwoID {get; set;}
public ICollection<Toy> ToyOnes {get; set;}
public ICollection<Toy> ToyTwos {get; set;}
}
I'm not sure the actual use-case for this is, otherwise I would have made better named properties.

asp.net mvc automapper parsing

let's say we have something like this
public class Person
{
public string Name {get; set;}
public Country Country {get; set;}
}
public class PersonViewModel
{
public Person Person {get; set;}
public SelectList Countries {get; set;}
}
can automapper be used to perform to parse from Person into PersonViewModel and back ?
Don't use AutoMapper for this - it's not worth it. For example, in the cases where you have a validation failure and you show the form again - AutoMapper is not executed here (usually). We usually go two routes:
If the list is not context-specific, create an HtmlHelper that queries some ISelectListProvider for the select list items: Html.DropDownList(). You'd use your IoC container of choice to locate the personListProvider, query for the list of items, and populate the dropdown list.
If the list is context-specific, just construct the list in the controller action
It sounds like you want to send a Person to the view via the PersonViewModel which has all the bonus info you need to generate and return a new (or updated) Person object.
If this is correct, I don't think you need automapper at all. From what I understand of automapper it is for mapping collection of related objects to a more view model type of state, but in this case, you are sending a Person to the client and trying to receive a Person back. In this case, it seems easier to use your view model to populate the page, but have the page return a Person instead (or extract the updated Person from the view model to save a few keystrokes).
EDIT: That being said, yes you should be able to use automapper to move the info around. Its just a unnecessary layer for this easy scenario.
if one chose to, you could do this:
public class Person
{
public string Name {get; set;}
public Country Country {get; set;}
public Country[] GetCountries
{
... add method for countries here
}
}
Then in your ViewModel you can have your select list pull data from that collection.
public class PersonViewModel
{
public Person Person {get; set;}
public Country[] Countries {get; set;}
public SelectList Countries { get{ .. add new select list code here getting vals from Countries..}}
}
Again, this is for context sensitive lists. You are however muddling concerns a wee bit here (should a person get a list of countries?)

Resources