Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Che Delilas
Nov 23, 2009
FREE TIBET WEED

TheEffect posted:

Editx2- The copies are being copied over just fine now, but not the empty directories. When I try to add the empty directory to my solution it tells me the folder already exists, but it's not in my solution explorer?

I don't think that ClickOnce has the capability to create empty directories on installation.

More to the point, your application should not be blindly relying on a folder to be there in order to function. If it needs to store things in a specific folder, it should be checking for that folder every time it needs it, and attempting to create it if it doesn't exist.

Edit: Okay, reading back I see that the scripts need these folders, not your application. In that case your application should probably check for the folders on startup instead of every access. Or if you have access to the underlying script, make the script do it (why isn't the script doing it).

Che Delilas fucked around with this message at 22:38 on Jul 23, 2014

Adbot
ADBOT LOVES YOU

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Bognar posted:

We attempted to use it for a recent project, but ultimately we threw it out and rolled our own like we have done for years. I wish MS would get away from trying to provide the "one identity solution to rule them all" and instead just provide sane defaults for password hashing, password requirements, and default functionality for email validation, password resets, and the like.

I'm trying to learn MVC for a personal project and to expand my skillset in general, and this Identity poo poo is not making it easy, let me tell you.

Mr Shiny Pants posted:

Yeah I get what you mean, they are always trying to be all things to all men. See WPF, you can do almost anything with it but there are no "slick" standard controls a la OSX.

Meanwhile, it provides a DatePicker but not a DateTimePicker, because why would you have a control that takes advantage of a built-in data type (DateTime) when you can have one that takes advantage of only half of it? I mean, nobody cares about time of day, right? Especially in a business environment.

We got a meeting tomorrow guys. What time? Tomorrow. Something wrong with your ears?

Che Delilas
Nov 23, 2009
FREE TIBET WEED

RICHUNCLEPENNYBAGS posted:

What are you stuck on?

Customizing it to suit my project's needs. I don't understand how all the pieces fit together, and so I don't know what to modify/add to enable things like role-based authorization and administrator approval of new registrants.

I'm pretty sure it's a combination of information overload and inexperience with ASP.NET and web development in general. I'm not very familiar with what goes into authorization and authentication because all my previous experience in that area is "if they can install it with ClickOnce because they're in the right AD group, they're authorized to use it" corporate-style development. Trying to learn anything about it by examining Identity code is just confusing me.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Knyteguy posted:

No luck unfortunately. Thanks though.

I feel like Windows 8.1 Phone development is still in beta stages or something. Not a whole lot of helpful debugging information.

It could be a Visual Studio bug - I hate to suggest this because it sounds so Tier 1 Helpdesk, but have you tried Cleaning your solution, exiting Visual Studio, then restarting it and Rebuilding your solution?

I've had VS gently caress up in similar ways in the past, being unable to find classes that I knew were there and the like, specifically with XAML. Restarting the IDE tended to fix it.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

How much do you want to learn? I ask because what you're talking about doing is more or less the essential foundation of good WPF coding. If this is just a one-off project and all you care about is getting it to work, there are quick and dirty ways. If you actually want to learn WPF and how to do things the "right" way, you're going to need to learn and understand the design pattern called MVVM, which is not an easy thing to wrap your mind around if you haven't seen it or something like it before.

Edit: Ithaqua may organize a lynch mob for me if I teach you the quick and dirty way :v:

Che Delilas
Nov 23, 2009
FREE TIBET WEED

A Tartan Tory posted:

Welp, I think my brain needs a rest at this point, because I didn't even see that.

Ignore the above stuff, I'll have a look when I'm at least marginally refreshed enough to see basic poo poo again.

Other posters have provided a lot of great information on the technicalities, so I'll make a comment on a tangential issue.

You seem a little down on yourself, based on a couple of your posts. Quit it. You're learning something new, and despite what some people have said, it's not really simple. It's not difficult once you figure out how everything fits together, but actually getting to that point takes some work. If you're not familiar with it, especially its declarative nature, it's a fairly alien mindset that you have to train yourself to acquire.

So don't feel bad that you aren't getting it right away, is what I'm saying.

What I would suggest is that once you've gone through whatever videos and tutorials you're going to go through, go make something simple on your own. Put your current project on hold and make an MVVM program that has a textbox and a button that adds 5 to the value in the textbox or something. Make it work perfectly using MVVM and only MVVM. If you can't do it, go back and watch the videos/do the tutorials again.

Repeat until you have the blinding epiphany.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

A Tartan Tory posted:

Honestly, I'm on a massive downer because of this mostly because even after the better part of a week of dedicated study on it, including multiple examples from everyone here and youtube videos etc. I just straight up don't get it.

I don't get it to the extent where I don't even know where to start doing the basic program you say to try and I really don't understand why I can't either. Everything I make at the moment regarding it seems like a massive jumbled up mess that makes no sense, when it made perfect sense when I did it with the console.

I feel like a complete helpless idiot doing this stuff and I'm struggling to think of how to get over this hurdle because nothing I do actually seems to work and it is incredibly frustrating.

I think following Drastic Actions's advice would be a good idea at this point - get yourself a github or bitbucket and make your whole project available for people to look at. There are a bunch of things that you have to do to get MVVM to work right, and chances are you're just missing a piece or two. And as I mentioned before, none of the pieces are really very intuitive.

I'm sure we'll be able to at the very least track down what you're missing.

Che Delilas fucked around with this message at 02:42 on Aug 27, 2014

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Scuzzywuffit posted:

As it turns out, the reason my program had a window that wasn't closing properly was because I left a constructor call in my code from before I refactored it. So I created two windows, and never closed one :suicide:

In other words, I am literally the dumbest person on the entire planet.

Welcome to a very select club, my friend. Let me introduce you to your fellow members: literally everyone who has done object-oriented programming.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

A Tartan Tory posted:

Thanks for this, after a bit of calming rest and relaxation, I was able to use this to build something basic that actually worked. :toot:

Progress!

Oh man, forgot that piece of advice. Stepping away from the problem for a length of time does wonders. Can't tell you how many times I've beat my head against the wall all day at the office, only to have a blinding epiphany 15 minutes into my commute home.

Che Delilas
Nov 23, 2009
FREE TIBET WEED
I definitely prefer the fluent (dot) notation. It looks cleaner and more structured, everything separated out clearly into methods, which you can order the way you like (for me, that means in a way that's as close to an SQL query as possible, since I'm familiar with those to begin with; Select -> Where -> OrderBy -> etc.). Beyond that, the fluent notation has a bunch of extension methods that do not have an equivalent in the query notation, so if you need any of those, you're using the fluent notation anyway. FirstOrDefault, Take and Skip immediately come to mind.

Or you can be a horrible horrible monster and do something like:
C# code:
var result =	(from p in db.Products
				where p.Id == selectedId
				select p).FirstOrDefault();
Ewg.

Che Delilas fucked around with this message at 15:50 on Aug 29, 2014

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Ithaqua posted:

At least with LINQ to Objects, the answer is "of course it makes a difference." I can't speak to the SQL generated by LINQ to Entities. It's usually fairly smart about that kind of thing, but it's safer to assume it's dumb.

So, with Linq to Objects, it actually executes each method immediately, rather than waiting for the results to actually be accessed (the way I understand it does with EF)?

Che Delilas
Nov 23, 2009
FREE TIBET WEED

mortarr posted:

Can't believe I wasted like four hours on this poo poo.

Welcome to programming. Learn to love simple solutions to :bang: problems.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

epalm posted:

In both scenarios data is being "sent", whether I GET or POST /page.html?searchTerm=poop

The idea between GET and POST, is that GET should have no server-side side effects (search for something, get back results), whereas POST may have server-side side effects (save this record).

My question is why can't I use GET and send a complex type? For example say I'm running a search with a list search terms.

Well according to
http://stackoverflow.com/questions/2300871/how-to-take-an-array-of-parameters-as-get-post-in-asp-net-mvc
you can construct your HttpGet action method with a string[] as an input param, so each parameter included in the query string is an element of the array. You have to name each parameter the same thing (?searchterm=Vegetables&searchterm=Fruits&searchterm=Dairy).

As for complex objects, I mean we're still dealing with GET here which uses the query string. You have to encode your complex data into a string of some kind in such a way that you can decode it in the action method on the server side (at least, I'm pretty sure that's the case; I'm fairly new to web dev).

Che Delilas fucked around with this message at 19:09 on Sep 4, 2014

Che Delilas
Nov 23, 2009
FREE TIBET WEED

epalm posted:

JavaScript code:
rows = [
    { Id: 42, Qty: 2 },
    { Id: 44, Qty: 2 },
    { Id: 48, Qty: 4 },
];

$.ajax({
    url: "/path/to/mymethod",
    type: 'POST',
    contentType: "application/json",
    dataType: "html",
    data: JSON.stringify({ model: rows }),
});
C# code:
[HttpPost]
public PartialViewResult MyMethod(int expId, IEnumerable<Row> model)
{
    return PartialView("...");
}

public class Row
{
    public int Id { get; set; }
    public int Qty { get; set; }
}
The above, using POST, works like a charm. Technically I "should" be using GET because I'm just requesting some data/visuals, and the call to MyMethod has no side effects.

If I change type: 'POST', to type: 'GET',
and [HttpPost] to [HttpGet]
then MyMethods model argument will inexplicably be null

What query string do you get when you run that as HttpGet? What that's saying to me is that the output of JSON.stringify({model:rows}) is a string that doesn't work as a URI in a browser's address bar.

Edit: As far as what you 'should' be using, GET's primary advantage is that it generates that URI that can be copy/pasted, so if a user wants to bookmark the 4th page of a set of search results or send that as a link in email, they can. If you don't need that level of convenience, POST is perfectly acceptable.

Che Delilas fucked around with this message at 20:34 on Sep 4, 2014

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Leyburn posted:

The only thing that's really causing me to doubt myself here is that the preconditions for all three tests are identical, so any small change in how the application works would require the same changes to be made in three different places instead of one, which immediately sets off alarm bells. But the advantages of doing it as above is that when I run the tests I get a very clear and precise description of all the expected behavior of this method.

What do we do when we are using identical code in multiple places? Write a function.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Leyburn posted:

Yeah that probably is the best thing to do. I was worried about sacrificing the readability of the tests, but you're right, that's probably not going to be a big problem in this case.

I think your tests are actually going to be far more readable once you encapsulate out all your Arrange logic into a separate PreapareUnitToAttack method or something similar.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

RICHUNCLEPENNYBAGS posted:

..
So because of that I'd like to change things in such a way that an include is unnecessary to avoid another query.

Eager loading of derived types does not appear to be supported, nor are they even working on it.

http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1249289-include-property-of-derived-classes

I can't think of a solution that isn't a huge pain. Refactor your entity model so that it doesn't use inheritance (I know, this is also a huge pain)?

Che Delilas
Nov 23, 2009
FREE TIBET WEED
Edit: oh. you set it.
Double Edit: Goddammit I can't read.

Set the "Minimum" property.

Che Delilas
Nov 23, 2009
FREE TIBET WEED
What are you doing with the "Product?" I mean, once you're done appending a description to it, what happens? I'm guessing it goes into some kind of database of available inventory/products, yes?

If so, that's textbook relational database. Three tables: Products, Gems, ProductGems. Gems has all the description information and special handling and processing instructions for each gem, and when you need to print out a product data sheet, you just join those tables.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Stuff like the actual type of exception and the exception message would help. Wild guess, where are you trying to save your files?

Che Delilas
Nov 23, 2009
FREE TIBET WEED
The column isn't Identity because it's not being generated by the database. What you want it to be is a Primary Key.

Edit: (You can use the [Key] attribute for that if it isn't picking it up on its own)

Che Delilas fucked around with this message at 01:54 on Oct 4, 2014

Che Delilas
Nov 23, 2009
FREE TIBET WEED

epalm posted:

It is a primary key. A column can be a PK and not have an Identity Specification, unless SMSS is lying to me.

Yes, I know it can be. What I'm telling you is that you don't need the column to be Identity if you're setting the value yourself. What you are seeing is expected behavior.

Edit: yourself, not itself

Che Delilas
Nov 23, 2009
FREE TIBET WEED
Okay, that makes sense.

From some quick research it appears that EF just isn't able to handle what you want right now. SET IDENTITY INSERT ON/OFF affects the behavior of SQL Server, but not of Entity Framework, which is why it's doing nothing for you. The best solution I'm able to find does indeed involve hand-crafted queries, unfortunately.

How often are you going to be migrating existing records to this table? If it's just a one-time import of an old database into the new system, you may want to handle that during deployment (or let the DBA do it if you have one). As long as the column is re-seeded so that it won't ever overlap with existing inventory, you shouldn't have to deal with it again.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

epalm posted:

I guess I was just looking for some guidance, I'll proceed and report back with my findings.

The more you use EF, the more you will look into something and find that the answer is, "EF can't do that right now" or "This is a bug in EF," usually accompanied by "We have no plans to work on this issue." What fun! :v:

Che Delilas
Nov 23, 2009
FREE TIBET WEED

epalm posted:

Yes, it's a 1-time import. I'm looking into reseeding (which I suppose I'd do in a migration?).

If you're going to import records into a table with an identity column using SET IDENTITY INSERT ON, you're going to want to re-seed, yeah. Because chances are, once you've done that and start inserting records normally, the database is going to auto-generate an Id value that has already been taken by one of the imported records, and the insert will fail with some kind of duplicate-value error (I don't know the specific one right off). Re-seeding to some value greater than the largest existing value will prevent those problems.

Edit:

I do have another question for you though. Is there some reason you need to keep those old IDs intact? Like, is your new database going to be linked (like, linked servers linked) to an already-in-production database? If you're just migrating all the data over to a new self-contained database, there's no reason to keep those existing IDs at all. They're just references so tables can join.

Che Delilas fucked around with this message at 02:51 on Oct 4, 2014

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Hah! True enough, though C# is remarkably good about this sort of thing in general, outside of EF. I think the only real headaches I've had with it have been related to Visual Studio's design view for WPF projects, and the particular issues I had have been fixed as far as I can tell. And that wasn't a problem with the language, but the IDE.

Che Delilas
Nov 23, 2009
FREE TIBET WEED
ASP.NET MVC5 Identity problem.

I've been beating my head against the wall trying to understand Identity and how to make it play well with anything beyond the default MVC application. I am at a complete loss on how to proceed.

I have an MVC5 project where users can add Products into a database. Each Product entry must keep track of the user who added that Product in the first place. The default MVC5 project includes an IdentityModel.cs file with ApplicationUser and ApplicationDbContext defined. So okay, we've got a DbContext already, so let's fold the rest of my domain model into it to keep things "simple." My question is, how do I get the Identity-related stuff to play nice with my business entities? (Code and specific error follows).

Product.cs
C# code:
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ApplicationUser AddedByUser { get; set; }
}
IdentityModel.cs (Generated by default mvc project with my single addition at the end (DbSet<Product>))
C# code:
public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public DbSet<Product> Products { get; set; } //I added this line
}
ProductController Add method
C# code:
[HttpPost]
[Authorize]
public ActionResult Add(Product p)
{
    if (ModelState.IsValid)
    {
	//I got this line from the AccountController generated by the default mvc project
        var usrmgr = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
  
        p.AddedByUser = usrmgr.FindById(User.Identity.GetUserId());
        using (var db = new ApplicationDbContext())
        {
            db.Products.Add(p);
            db.SaveChanges();
        }
        return RedirectToAction("Index");
    }
    return View();
}
The error I get when I log in and attempt to add a product is an InvalidOperationException with the message, "An entity object cannot be referenced by multiple instances of IEntityChangeTracker." It's on the db.Products.Add(p); line.

I know, or at least I think I know, that the problem is coming from the fact that there are two conflicting contexts: one from the OwinContext that I need to get my UserManager in order to find the logged in user, and one from the new ApplicationDbContext that I'm using to try and add the Product into the database.

How do I get these things to work together? I've been trying to learn ASP.NET MVC for a little while now, and while I understand the basics of controllers and views and routing, the auth/Identity stuff is so twisty and obfuscated that I can't penetrate it. I certainly don't understand enough about auth to just ditch Identity and roll my own solution; even if I could get something to work I'm sure it would be full of security holes.

Additionally, if someone knows a resource that really explains auth and/or Identity, that would be really helpful in general.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

GrumpyDoctor posted:

Ok, I've got a collection databound. The collection has changed. I want the bound view to update. My options are:
  • Use an ObservableCollection (hope I didn't need a more specialized collection type internally!)
  • Create a new collection every time something changes (WPF is about clarity!)
  • Get ahold of the actual binding expression and dick around with it (WPF is elegant!)
Is that right? There's actually no way to force it?

If you need a better collection than ObservableCollection<T>, you could make your own, implementing INotifyCollectionChanged. It's like INotifyPropertyChanged but it's for when the collection itself changes (add, remove elements, capacity changes I think) rather than one of the elements' properties changing.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

aBagorn posted:

This may not be 100% best practices, but I've never stored an actual user entity for audit tracking. You can just as easily just do this:

...

And you will have a Product updated with the Id of the user that updated it. Put a FK constraint on your product table and then when you are querying do a simple join on the Id field to get back the username.

I'm using code first and that's actually how it handles the schema creation when you include the full object in your entity. Product table has a User_Id field with a FK constraint on the ApplicationUsers table. I admit I didn't think of just forcing it that way, though; hack though it may be, bypassing the whole mess might be my best bet. Thanks for the idea, I'll have to play with it and see if I can make it work with the more complex full application.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Bognar posted:

The problem is that the user manager has its own DbContext and you are trying to add an item from that context onto the new one that you're creating.

Yeah, I figured it was something like that.

quote:

Using the Id instead is a good way of doing it, but you need to make sure you still have a foreign key set up. This model should tell EF Code First to make AddedByUserId the foreign key for the AddedByUser relationship.

aBagorn posted:

You also could add a Data Annotation to my solution to force the foreign key relationship.

This is exactly what I ended up doing.

C# code:
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string AddedByUserId { get; set; }

    [ForeignKey("AddedByUserId")]
    public virtual ApplicationUser AddedByUser { get; set; }
}
Lets me work with the Id when I'm assigning things, and lazily loads the full ApplicationUser object if I need it for display purposes (very rare in my application). Thanks guys, this portion of the project is working real smooth now - to the point that I'm a bit worried that I missed something, heh.

GrumpyDoctor posted:

Yeah, I know this is the "right" answer, but the way that WPF is missing certain kinds of duct tape is infuriating sometimes. :(

WPF is a bit of an odd duck sometimes. It's like they built parts of it with the goal of eventually adding a few more tools, and then just forgot. RelayCommand is the perfect example - it's so core to getting Command binding to work that Microsoft put out an article with a code snippet that they tell you to just copy and paste into a class for your projects to use. Just loving build it into the framework already!

Also for some reason they don't have a DatePicker control built in because reasons.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

aBagorn posted:

Random Entity Framework question:

When doing Code First, is there any tangible drawback to not including navigation properties on one end of a relationship?

By not having a virtual Foo in my Bar class, I miss out on being able to call Bar.Foo, but is that it? Keeping the properties one sided allows for easier JSON serialization, and the way the data is setup Bar should not be accessed directly anyway, always through Foo.

Am I making a huge boo boo?

I'm not an expert, but as far as I know, the only thing you're missing out on is being able to call Bar.Foo as you said. If you never have access to a Bar without also knowing what its Foo is already, I don't see a downside. The relationship is going to be created correctly on the database's end with just the ICollection on the Foo. You should be fine.

Che Delilas
Nov 23, 2009
FREE TIBET WEED
I have an ASP.NET MVC question.

My site requires user registration and an administrator has to approve each new user before they can log in. I want the admins to be able to see the list of users and be able to approve/reject new ones with a single click, no going to a confirmation page or anything. As I understand these things, since these will be operations that affect the database I want to do them in POST methods.

My HTML looks something like this:

HTML code:
@foreach (var user in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(m => user.UserName)
        </td>
        <td>
            @Html.DisplayFor(m => user.FirstName)
        </td>
        <td>
            @Html.DisplayFor(m => user.LastName)
        </td>
        <td>
            @* Button to approve user *@ | @* Button to reject user *@
        </td>
    </tr>
}
My question is, how do I handle form submissions in this case? Do I just make a separate form for each iteration of the loop? It will never have a large number of users but it could still potentially mean like a dozen separate forms being generated on a single page. Is it even possible to use a single form for this?

Please keep in mind, this is a learner project, so if there's something obvious and fundamental I'm missing, be gentle :shobon:

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Scaramouche posted:

I think I see your confusion; you're struggling with the idea that each button could potentially be a submit and how to handle them independently? What I'm wondering is, why have a button for each one and the round-trip that implies? You could have checkboxes or radios instead (with the default set to approve/reject depending how often each comes up). Then the form can handle each box/id in a FOR...EACH operation with only one submit.

Mostly I'm dealing with people who I know really struggle with anything computer. I didn't want to introduce an extra step, and "push button, thing happen" is as simple as I came up with. Radio buttons probably are a better plan though.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Scaramouche posted:

I got you. If you're still married to the idea, you could fake it out with link buttons. I have no idea how 'MVC' that is, but instead of doing a full submit just have a link button with href="blah.aspx?userid=xx&approve=1" and then let the code behind process it. Insecure as hell and messy to boot.

I don't think there's a clean way to do it the way I want to. Another problem is that you can do this from multiple places (a "manage users" page and the main admin control panel 'dashboard' thing), and approval/rejection is not the only thing you can do with users. Like you can delete/deactivate accounts and manage their roles too.

I think what I'm going to do is reorganize everything so you get to do one function at a time. So no more "manage users" uberpage with all the functionality right there, instead we're going to have a "These are the users awaiting approval" page (with the single form, radio button approach), a "Change User Roles" page, and a Deactivate Users page. That way, each function can be accessed from multiple places if I need to (dashboard/manage users page), each page has one thing you can do on it, and it won't flood anyone with information. It's more clicks but it'll probably be less confusing, and this isn't an area that anyone is going to need to spend a lot of time in; extra clicks aren't a huge deal.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Munkeymon posted:

I'm trying to union a couple of lists of objects on some subset of their properties and this is the thing I came up with:

Am I missing something less slow and convoluted? Should I just go ahead and post this in the coding horrors thread?

You're using tuples in your example, but if you make the data type a proper class and create a corresponding implementation of IEqualityComparer, you can just use a Linq Union:
(var resultList = List1.Union(List2, Comparer);)

http://msdn.microsoft.com/en-us/library/bb358407.aspx
http://stackoverflow.com/questions/5276169/linq-query-with-distinct-and-union

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Munkeymon posted:

No, I tried that first and I can't because it'll only call the loving GetHashCode method for reasons probably having to do with performance but totally obviating my actual intent.

GetHashCode is part of that interface. Implement it so that the objects are equal when they're supposed to be, don't just call base.GetHashCode.

Edit: I mean, I suppose it's not simple to choose a good hash function for what you're working with. Might be worth looking into though.

Edit the 2nd: http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode/263416#263416

Che Delilas fucked around with this message at 03:24 on Nov 19, 2014

Che Delilas
Nov 23, 2009
FREE TIBET WEED

bpower posted:

I expect my db to change a bit during the early stages of dev, or at least I must develop with that possibility in mind. Does that almost rule out EF?

Not at all.

Che Delilas
Nov 23, 2009
FREE TIBET WEED
Web security-related question here. I'm coding up a set of pages that will let administrators manage user accounts for a web site that uses ASP.NET Identity. Is it dangerous at all to return full entries from the Users table to clients? For example, on one page I'm displaying a list of users and allowing each one to be set to active or inactive.

The full User entity contains fields like PasswordHash and SecurityToken, which I don't need to display to the client. If I return View(context.Users.ToList()); then those fields will be transmitted to the client even if the view doesn't make use of them, right? Can someone who knows how do anything evil with those values?

I know about one-way encryption and password hashing, so I don't need the 101 version, but security is such a complex topic and I've never taken the time to really dive into it, so I'm curious. Oh, and I know how to render the issue moot by using a ViewModel containing only the values the View cares about; it really is just an academic question at the moment.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

chmods please posted:

No, the values only go as far as the view rendering code, and go away once that finishes. If you never use them in the view, they will never go to the client.

Oh, the View (the .cshtml file in my case) isn't what the client sees, is it? It describes how to generate the final HTML, which is then sent to the client. That's never really clicked in my head until now.

Thanks, that's a big help, especially when it comes to getting into the right mindset for web dev.

Adbot
ADBOT LOVES YOU

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Funking Giblet posted:

I would still use an object for the view that contains only what you want, as you may run into trouble later if you say, need to post values back and are trying to save the user back to the database, or a junior decides to output a property by accident.

Look at AutoMapper which will help you create viewmodels from models using conventions and some configuration, which are just representations of your data which is just used for the view.

I'll keep AutoMapper in mind - this project is pretty simple, so I'm happy just manually creating all my viewmodels, but I've seen it mentioned before a number of times as a real energy saver.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply