copy contents of dto to another similar dto - asp.net-mvc

I have 2 very similar dto object. I have shown a sample code below, but the actual 2 dto with which I am working have 39 and 40 properties in it.
My question is that is there an easier way to copy contents of QuoteDto to Quote2Dto.
I am makinga call to a legacy project which gives me QuoteDto object. I than have to make call to a new rest service project which only accepts Quote2Dto object
Let me know if you need any more code.
public abstract class QuoteDto
{
public virtual bool IsWaive { get; set; }
public virtual bool IsExpired { get; set; }
}
public abstract class Quote2Dto
{
public virtual bool IsWaive { get; set; }
public virtual bool IsExpired { get; set; }
public virtual bool IsCancel { get; set; }
}

Usually Automapper (or similar library) is used. It can copy same properties without any pre-configuration needed. But you can always configure more advanced property mappings.
Here you can find Getting Started Guide.

You have two options here.
Your first options is to use AutoMapper to copy properties. There are some advanced configurations to AutoMapper if you would need more advanced configuration.
The second option is to create a method inside your DTO, that takes the other DTO and copies the properties.(Basically copying the properties manually.)
It would look something like this:
class FirstSampleDTO
{
public int RandomProperty { get; set; }
public int RandomProperty2 { get; set; }
public int RandomProperty3 { get; set; }
private void CopyDTOData(SecondSampleDTO dto)
{
dto.RandomProperty = this.RandomProperty;
dto.RandomProperty2 = this.RandomProperty2;
dto.RandomProperty3 = this.RandomProperty3;
}
}
class SecondSampleDTO
{
public int RandomProperty { get; set; }
public int RandomProperty2 { get; set; }
public int RandomProperty3 { get; set; }
private void CopyDTOData(FirstSampleDTO dto)
{
dto.RandomProperty = this.RandomProperty;
dto.RandomProperty2 = this.RandomProperty2;
dto.RandomProperty3 = this.RandomProperty3;
}
}
(This in in case you already have the data of the dto.)
I would suggest using the second method if you are not going to do that much of a mapping.

Related

Saving many to many relationship tables in Asp.Net MVC

I use Asp.Net MVC, Entity Framework. I have a form it looks like below.
Here, dropdownlist is filled from a table(types). Checkboxes is filled from another table(test). Tables are like below:
public class Types
{
public int TypesID{get;set;}
public string TestName { get; set; }
public string TestExplanation { get; set; }
public int TestTime { get; set; }
}
public class Tests
{
public int TestID{get;set;
public string Name { get; set; }
public string Code { get; set; }
}
public class Types_Tests
{
public int Types_TestsID{ get; set; }
public int TypesID { get; set; }
public int TestsID { get; set; }
public virtual Types Types { get; set; }
public virtual Tests Tests { get; set; }
}
Types_test table is relation table between Types and Tests. When I click Kaydet button, it shuld save type and checked tests. I made this operation using ViewBag, javascript and hdnvalue.I added checked checkboz values to a hdntext. I made saving process like below:
[HttpPost]
public ActionResult Index(string drpType, string hdntesttypes)
{
var TypeList = Types.GetAll();
ViewBag.TypesList = new SelectList(TypeList, "Id", "Name");
var testypeList = testTypes.GetAll();
ViewBag.TestTypesList = new SelectList(testypeList, "Id", "TestName");
GenericRepository<TestDisabledTypes> testDisabledRepository = new GenericRepository<TestDisabledTypes>(_context);
if (!string.IsNullOrEmpty(hdntesttypes))
{
string[] disabletypesArray = hdntesttypes.Split(',');
using (TransactionScope trns = new TransactionScope())
{
for (int i = 0; i < disabletypesArray.Length; i++)
{
Test_Types types = new Test_Types ();
types.TestTypesID = Convert.ToInt32(disabletypesArray[i]);
types.TypesID = Convert.ToInt32(drpType);
testDisabledRepository.Insert(types);
}
trns.Complete();
}
}
return View();
}
It wokrs. But I search better solution for this process. Can someone give me any idea?
Thanks.
If you don't need additional attributes for your entity class, you don't need create link table.
Just define the following class, and EF will generate the link table for you automatically.
public class Type
{
public int TypesID{get;set;}
public string TestName { get; set; }
public string TestExplanation { get; set; }
public int TestTime { get; set; }
public ICollection<Test> Tests { get; set; }
}
public class Test
{
public int TestID{get;set;
public string Name { get; set; }
public string Code { get; set; }
public ICollection<Type> Types {get;set;}
}
Well, in EntityFramework if you want to create a many to many relation object you need to create new object of "linking" entity. Unfortunately, it is not possible to add first object, add second object and say "Guys, you are in many to many relationships. Are you happy then?" :) You need to create relation object, set appropriate fields in it (I think these are ids of two objects itself) and add it to relation collection (entity) in your model. But before doing so you need to be sure that objects with data you are linking with are already exists in database. Otherwise you'll get an error
Also it's not necessary to create manually transaction because EF does it for you automatically each time you get/save your data

MVC model duplication

I'm very new to ASP.NET MVC, so forgive me if this is something I should know. I haven't seen any obvious documentation on it, so here goes:
I have a LINQ to Entities data model and a MVC project. I use a lot of javascript/jquery, so have opted to access my data from the client through a WebAPI as json objects. However, I don't want to pass all the entity object properties though to the client, so I have added separate models to my MVC project in which I handle MVC model validation and Binding to my Views. Also, in order to work with it in my jquery, I have created json versions of the models.
This is only the start of the project and I don't want to start it off on the wrong foot. Having three versions of my models for each entity in my business layer is going to be a nightmare! I am sure that the overall structure of my project is a very common one, but can't see many developers settling for such duplication of code. There must be a better way of implementing it.
Any thoughts? Really appreciate any input.
In answer to your comment above - you can create your javascript viewmodel as a standard js object. I tend to use Knockout.js so it would look like this:
jsController.Resource = function (data) {
self.UserId = ko.observable(data.UserId);
self.FullName = ko.observable(data.Name);
self.RoleName = ko.observable(data.RoleName);
self.RoleId = ko.observable(data.RoleId);
}
and then use an ajax post method to post it to your MVC action
jsController.addToUndertaking = function (resource, isAsync) {
mylog.log("UndertakingId at post = " + jsController.undertakingId);
var action = $.ajax({
type: "POST",
url: "/TeamMember/AddUserToUndertaking",
data: resource,
cache: false,
async: isAsync
});
action.done(function () {
resource.AllocatedToUndertaking(true);
//Do other funky stuff
});
};
Create your MVC action so that it accepts a forms collection as so:
public ActionResult AddUserToUndertaking(FormCollection postedResource)
{
if (Request.IsAjaxRequest() == false)
{
const string msg = "Non ajax request received";
Logger.ErrorFormat(msg);
throw new SecurityException(msg);
}
if (postedResource == null)
{
Logger.Debug("Null resource posted - terminating.");
return new HttpStatusCodeResult(500);
}
var resource = new AllocatedResourceAjaxViewModel(postedResource);
//Do something Funky
return new HttpStatusCodeResult(200);
}
and then you create your MVC viewmodel from the forms collection (i tend to do this by passing in the forms collection as a constructor method to the viewmodel).
public class AllocatedResourceAjaxViewModel
{
public int UserId { get; set; }
public string Name { get; set; }
public string RoleName { get; set; }
public int RoleId { get; set; }
public AllocatedResourceAjaxViewModel()
{}
public AllocatedResourceAjaxViewModel(NameValueCollection formData)
{
UserId = JsFormDataConverter.Int(formData["UserId"]);
Name = Convert.ToString(formData["FullName"]);
RoleName = Convert.ToString(formData["RoleName"]);
RoleId = JsFormDataConverter.Int(formData["RoleId"]);
}
}
As a null int in your javascript VM will lead to a string of 'undefined' being passed you need a converter method to safely extract non strings.
public static class JsFormDataConverter
{
public static bool Boolean(string formValue, bool defaultValue = false)
{
if (formValue.ToLower() == "true") return true;
if (formValue.ToLower() == "false") return false;
return defaultValue;
}
public static int Int(string formValue, int defaultValue = 0)
{
int result;
return int.TryParse(formValue, out result)
? result
: defaultValue;
}
}
and there you go. I am sure you can improve on the above but that will get you going.
The way that I have always worked is that you have your Models e.g. Order & OrderLines which are where you store all your data and get hydrated either directly from the database by SQL or (more usually these days ) by an ORM such as NHibernate or Entity Framework.
You then have ViewModels - these are used to transport the data from your application to the views - either directly ie a strongly typed view bound to say an OrderViewModel or via an action returning a JsonResult.
A OrderViewModel is not a duplication of Order as it is designed to only hold the data that is needed to be presented on the screen (If you have many different views displaying an Order in different ways it could be perfectly acceptable to have many different ViewModels -one for each view containing only the fields needed for each view). ViewModels should also not contain any complex types except other ViewModels. this helps keep accidental data access out of the views (think security and performance).
So Given
public class Order
{
public int Id { get; set; }
public DateTime OrderDate { get; set; }
public User User { get; set; }
public string Description { get; set; }
public List<OrderLine> OrderLines { get; set; }
}
public class OrderLine
{
public int Id { get; set; }
public Order Order { get; set; }
public String Description { get; set; }
public int Quantity { get; set; }
public int Weight { get; set; }
public int Price { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
You could have the two ViewModels
public class OrderViewModel
{
public int ID { get; set; }
public List<OrderLineViewModel> OrderLines { get; set; }
public DateTime OrderDate { get; set; }
public int UserId { get; set; }
public string Description { get; set; }
}
public class OrderLineViewModel
{
public int Id { get; set; }
public int OrderId { get; set; }
public String Description { get; set; }
public int Quantity { get; set; }
public int Weight { get; set; }
public int Price { get; set; }
}
The view models could then be serialized into JSON as needed or marked up with validation attributes etc.

Eagerly Load Navigation Property that is List<OfSomeBaseClass>

Using EF Code First and given an Entity that contains a List, how can I eagerly load the entire object graph for that entity:
Example:
public class Foo
{
public int Id { get; set; }
public List<BarBase> Bars { get; set; }
}
public class BarBase
{
public int Id { get; set; }
public string Text { get; set; }
}
public class BarTypeA : BarBase
{
public List<Baz> Bazes { get; set; }
}
public class BarTypeB : BarBase
{
public List<Quux> Quuces { get; set; } { get; set; }
}
If BarBase were not a base class that could contain instances of several different subtypes, I could use
.Include("Bars").Include("Bars.Bazes")
If I try
.Include("BarBase").Include("BarBase.Bazes").Include("BarBase.Quuces")
I get the error
A specified Include path is not valid. The EntityType 'BarBase' does
not declare a navigation property with the name 'Bazes'.
But how do I handle the situation that Bars can contain different concrete types, and I want to eagerly load all of those instances including the List<T> contained in those concrete types?
This is reported problem in EF currently without a solution.

How do I use (Try)UpdateModel?

What is the right way to use (Try)UpdateModel?
When I run this:
TryUpdateModel returns true,
ViewData has no errors,
but my Proxy is not updated.
Action Method
public void Save(string TypeName, int Id, FormCollection idontknow) {
var types = Assembly.GetExecutingAssembly().GetTypes();
var ObjectType=(from t in types where t.Name == TypeName select t).First();
var Proxy = context.Set(ObjectType).Find(Id); // EF 4.1
if (TryUpdateModel(Proxy, TypeName)) {
var x = ViewData.GetModelStateErrors(); // no errors
}
}
Posted Data
TypeName=Thing&Id=1&Thing.Id=1&Thing.Name=hello&Thing.OptionID=2
Thing Class
public class Thing : Base {
public virtual Nullable<int> OptionID { get; set; }
public virtual Option Option { get; set; }
public virtual ICollection<ListItem> ListItems { get; set; }
}
public class Base {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
[NotMapped]
public virtual int? EntityState { get; set; }
}
EDIT: I also tried passing the form collection explicitly
TryUpdateModel(Proxy, TypeName, idontknow)
EDIT #2: (in response to NickLarsen)
Restarted VS and server, no change.
Values are actually in the FormCollection.
Mock data works! I know I must be messing up something here.
Using debugger to check values.
I stripped all the EF stuff and tried to get just that query string to populate the model with the values... and it worked just fine.
//controller class
public ActionResult Save(string TypeName, int Id, FormCollection idontknow)
{
var Proxy = new Thing
{
Id = 33,
OptionID = 2234,
Name = "tony",
};
if (TryUpdateModel(Proxy, TypeName))
{
ViewBag.Message = "WInner";
}
return RedirectToAction("Index");
}
//end controller class
public class Thing : Base
{
public virtual Nullable<int> OptionID { get; set; }
}
public class Base
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
Honestly I can't figure think of what in your code would keep it from working, but I would suggest going through the list 1 by one and testing after each step...
Save your progress and restart VS and your development server
Check that the values are actually in the form data, maybe something is getting in the way there.
Mock up some trash data like I did. (checking if the problem has something to do with EF)
How are you identifying that Proxy isn't being updated? In the debugger, on the page, etc?
Edit your question with the answer to all of the above questions.

Using CreateSourceQuery in CTP4 Code First

I'm guessing this is impossible, but I'll throw it out there anyway. Is it possible to use CreateSourceQuery when programming with the EF4 CodeFirst API, in CTP4? I'd like to eagerly load properties attached to a collection of properties, like this:
var sourceQuery = this.CurrentInvoice.PropertyInvoices.CreateSourceQuery();
sourceQuery.Include("Property").ToList();
But of course CreateSourceQuery is defined on EntityCollection<T>, whereas CodeFirst uses plain old ICollection (obviously). Is there some way to convert?
I've gotten the below to work, but it's not quite what I'm looking for. Anyone know how to go from what's below to what's above (code below is from a class that inherits DbContext)?
ObjectSet<Person> OSPeople = base.ObjectContext.CreateObjectSet<Person>();
OSPeople.Include(Pinner => Pinner.Books).ToList();
Thanks!
EDIT: here's my version of the solution posted by zeeshanhirani - who's book by the way is amazing!
dynamic result;
if (invoice.PropertyInvoices is EntityCollection<PropertyInvoice>)
result = (invoices.PropertyInvoices as EntityCollection<PropertyInvoice>).CreateSourceQuery().Yadda.Yadda.Yadda
else
//must be a unit test!
result = invoices.PropertyInvoices;
return result.ToList();
EDIT2:
Ok, I just realized that you can't dispatch extension methods whilst using dynamic. So I guess we're not quite as dynamic as Ruby, but the example above is easily modifiable to comport with this restriction
EDIT3:
As mentioned in zeeshanhirani's blog post, this only works if (and only if) you have change-enabled proxies, which will get created if all of your properties are declared virtual. Here's another version of what the method might look like to use CreateSourceQuery with POCOs
public class Person {
public virtual int ID { get; set; }
public virtual string FName { get; set; }
public virtual string LName { get; set; }
public virtual double Weight { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Book {
public virtual int ID { get; set; }
public virtual string Title { get; set; }
public virtual int Pages { get; set; }
public virtual int OwnerID { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual Person Owner { get; set; }
}
public class Genre {
public virtual int ID { get; set; }
public virtual string Name { get; set; }
public virtual Genre ParentGenre { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class BookContext : DbContext {
public void PrimeBooksCollectionToIncludeGenres(Person P) {
if (P.Books is EntityCollection<Book>)
(P.Books as EntityCollection<Book>).CreateSourceQuery().Include(b => b.Genres).ToList();
}
It is possible to add a method to you derived context that creates a source query for a given navigation on an entity instance. To do this you need to make use of the underlying ObjectContext which includes a relationship manager which exposes underlying entity collections/references for each navigation:
public ObjectQuery<T> CreateNavigationSourceQuery<T>(object entity, string navigationProperty)
{
var ose = this.ObjectContext.ObjectStateManager.GetObjectStateEntry(entity);
var rm = this.ObjectContext.ObjectStateManager.GetRelationshipManager(entity);
var entityType = (EntityType)ose.EntitySet.ElementType;
var navigation = entityType.NavigationProperties[navigationProperty];
var relatedEnd = rm.GetRelatedEnd(navigation.RelationshipType.FullName, navigation.ToEndMember.Name);
return ((dynamic)relatedEnd).CreateSourceQuery();
}
You could get fancy and accept a Func for the navigation property to avoid having to specify the T, but here is how the above function is used:
using (var ctx = new ProductCatalog())
{
var food = ctx.Categories.Find("FOOD");
var foodsCount = ctx.CreateNavigationSourceQuery<Product>(food, "Products").Count();
}
Hope this helps!
~Rowan
It is definately possible to do so. If you have marked you collection property with virtual keyword, then at runtime, you actual concrete type for ICollection would be EntityCollection which supports CreateSourceQuery and all the goodies that comes with the default code generator. Here is how i would do it.
public class Invoice
{
public virtual ICollection PropertyInvoices{get;set}
}
dynamic invoice = this.Invoice;
dynamic invoice = invoice.PropertyInvoices.CreateSourceQuery().Include("Property");
I wrote a blog post on something similar. Just be aware that it is not a good practice to rely on the inner implementation of ICollection getting converted to EntityCollection.
below is the blog post you might find useful
http://weblogs.asp.net/zeeshanhirani/archive/2010/03/24/registering-with-associationchanged-event-on-poco-with-change-tracking-proxy.aspx

Resources