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
JawnV6
Jul 4, 2004

So hot ...

fleshweasel posted:

Get just the capture group. Groups have automatic number identifiers or can be named. Google knows. Also, write \d{4} not \d\d\d\d.
Yeah sounds like you're grabbing the entire match instead of the specific capture group. You can always breakpoint it and drill into the variables to see where it's putting the information you want.

Adbot
ADBOT LOVES YOU

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

UberJumper posted:

So apparently after writing C# unit tests for half a week. I have apparently just inherited all responsibilities as lead test developer. The company is small (~20 people), and honestly even after saying "This is a bad idea" and i don't know anything about being lead test developer, they still put me in this position.

The old lead left on apparently bad terms, and there is absolutely no documentation. There is apparently a Jenkins server, that seems to be pulling from a SVN (we swapped to git a year ago). Apparently they just want to start from scratch when it comes to testing, and i am at a complete and utter loss. Does anyone have any tips for doing this kind of degree of testing for C#.

Read Roy Osherove's book The Art of Unit Testing. That's first and foremost.

IMO there should be no such thing as a "test developer". Everyone should be writing unit tests while they write their code. The tests should be considered first-class citizens and live in the same solution as the code they're testing. If you're doing true unit testing, the VS test runner should be configured to run tests after every compilation -- this goes for everyone. If a test breaks, fix the test right away. It doesn't matter who wrote the test that's failing. Testing is all about shortening the feedback loop and identifying and correcting bugs faster. It also helps inform the design of your application -- something that's hard to test is probably badly designed and needs to be refactored. Siloing "developer" and "test developer" is counter to the entire idea of shortening the feedback loop and informing design.

The fastest way to have a failed automated testing adoption is to let the tests be second-class citizens that only one or two people thinks about or maintains. The tests stagnate, and eventually they start to fail. By the time anyone gives a poo poo and goes to look at the tests, they have an awful time figuring out whether the tests are invalid (not testing the right things) or evidence of a bug. Then everything gets thrown out, and a year later someone takes another stab at the whole "testing thing", with exactly the same results.

Doing it right requires a culture change, plain and simple.

Hughmoris
Apr 21, 2007
Let's go to the abyss!

fleshweasel posted:

Get just the capture group. Groups have automatic number identifiers or can be named. Google knows. Also, write \d{4} not \d\d\d\d.


Thanks, I was able to figure it out.

I was using foundPAT_ID.Value which gave me everything, turns out I needed foundPAT_ID.Groups[1].Value which gave me just the group I wanted captured.

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Ithaqua posted:

Doing it right requires a culture change, plain and simple.

In other words, if you're now the "Lead Test Developer" and they want to do unit testing right, from scratch, propose this culture change as a part of your new set of responsibilities (after doing research about what exactly that entails). Don't just start being the one guy who writes unit tests. Propose the change to your entire development process that everyone needs to buy into.

Might not work, but if it does you'll get some great experience and bragging rights out of it.

bpower
Feb 19, 2011
asp.net MVC question.

I want a html table with one of the columns a checkbox and then a "Delete Selected" button to post a list of things to delete. I'd post more code but I'm sure where to begin with this. Any advise on how to approach this?

code:
  <table class="table table-striped">
            <thead>
                <tr>
                    <th>@Html.DisplayNameFor(m => m.ThingName)</th>
                    <th>@Html.DisplayNameFor(m => m.ThingType)</th>
                    <th>@Html.DisplayNameFor(m => m.ThingCount)</th>
                    <th>@Html.DisplayNameFor(m => m.Selected)</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var thing in Model)
                {
                    <tr>

                        <td>@Html.DisplayFor(m => thing.ThingName)</td>
                        <td>@Html.DisplayFor(m => thing.ThingType)</td>
                        <td>@Html.DisplayFor(m => thing.ThingCount)</td>
                        <td>@Html.EditorFor(m => thing.Selected)</td>

                    </tr>
                }
            </tbody>
        </table>

<input type="submit" value="delete Selected" class="btn btn-default" />

enthe0s
Oct 24, 2010

In another few hours, the sun will rise!
Sounds like you want to use CheckBoxList, but I've only been doing this for about 3 weeks now so there might be a better way to do it.

bpower
Feb 19, 2011

enthe0s posted:

Sounds like you want to use CheckBoxList, but I've only been doing this for about 3 weeks now so there might be a better way to do it.

Thats just for webforms I think, dont think you can use them in a MVC page.

Dreadrush
Dec 29, 2008

bpower posted:

asp.net MVC question.

I want a html table with one of the columns a checkbox and then a "Delete Selected" button to post a list of things to delete. I'd post more code but I'm sure where to begin with this. Any advise on how to approach this?

code:
  <table class="table table-striped">
            <thead>
                <tr>
                    <th>@Html.DisplayNameFor(m => m.ThingName)</th>
                    <th>@Html.DisplayNameFor(m => m.ThingType)</th>
                    <th>@Html.DisplayNameFor(m => m.ThingCount)</th>
                    <th>@Html.DisplayNameFor(m => m.Selected)</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var thing in Model)
                {
                    <tr>

                        <td>@Html.DisplayFor(m => thing.ThingName)</td>
                        <td>@Html.DisplayFor(m => thing.ThingType)</td>
                        <td>@Html.DisplayFor(m => thing.ThingCount)</td>
                        <td>@Html.EditorFor(m => thing.Selected)</td>

                    </tr>
                }
            </tbody>
        </table>

<input type="submit" value="delete Selected" class="btn btn-default" />


If you want to post to an action with a parameter like IEnumerable<Thing>, then MVC's modelbinder will read back form values with name attributes like:
"[0].ThingName"
"[1].ThingName"
"[2].ThingName"

So what you can do is:

code:
<form method="post" action="route-to-where-i-want-to-post">
	<table>
		<thead>
			<tr>
				<th>Name</th>
				<th>Type</th>
				<th>Count</th>
				<th>Selected</th>
			</tr>
		</thead>
		<tbody>
			@for (var i = 0; i < Model.Count; i++)
			{
				var thing = Model[i];
                        
				<tr>
					<td>@thing.Name</td>
					<td>@thing.Type</td>
					<td>@thing.Count</td>
					<td>
						<input name="[@i].ThingId" type="hidden" value="@thing.ThingId">
						<input name="[@i].IsSelected" type="checkbox" value="true">
						<input name="[@i].IsSelected" type="hidden" value="false">
					</td>
				</tr>
			}
		</tbody>
	</table>

	<button type="submit">Delete selected</button>
</form>
If you want to use the helpers, you can write something like:
code:
<td>
	@Html.HiddenFor(m => m[i].ThingId)
	@Html.CheckBoxFor(m => m[i].IsSelected)
</td>

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Dreadrush posted:

If you want to post to an action with a parameter like IEnumerable<Thing>, then MVC's modelbinder will read back form values with name attributes like:
"[0].ThingName"
"[1].ThingName"
"[2].ThingName"

I don't think that's the hangup (I mean, it might be, the question is vague enough that I don't know if he understands the model binder or if he's just having trouble figuring out how to distinguish between the view and the model with his entities).

bpower, I assume your html view has something like @model IEnumerable<Thing> at the top.

The problem is, Thing is (presumably) an entity that you're storing in a database, and there's no place for a "user wants this deleted" field in the Things table because "being deleted" is not part of a Thing, it's just something the user wants to do to the Thing.

What you want is to create a new class to use as your @model, we call this kind of class a ViewModel.

C# code:
public class DeleteThingViewModel
{
  public Thing Thing {get; set;]

  [Display(Name="Delete")]   //This is what you want Html.DisplayNameFor to use
  public bool IsMarkedForDeletion {get; set;]
}
And in your view, instead of @model IEnumerable<Thing>, it would be @model IEnumerable<DeleteThingViewModel>. That way, you still have a list of Things so that you can display them, but you have that extra bit of information in the form of IsMarkedForDeletion for each item so that your controller can process them in the HttpPost action when the user submits the form. And as Dreadrush said, you do need to include the Id or whatever unique identifier a Thing uses, by using a <hidden> tag or @Html.HiddenFor helper.

The only other sticking point here is that when you have a nested complex class like that, the @Html.DisplayNameFor helper won't work as you have it written; you will have to use an index if you want the compiler to figure it out (basically what it's doing under the hood by default as far as I know, it's just not smart enough to deal with the nested class). Like this:

code:
          <thead>
                <tr>
                    <th>@Html.DisplayNameFor(m => m[0].Thing.ThingName)</th>
                    <th>@Html.DisplayNameFor(m => m[0].Thing.ThingType)</th>
                    <th>@Html.DisplayNameFor(m => m[0].Thing.ThingCount)</th>
                    <th>@Html.DisplayNameFor(m => m.IsMarkedForDeletion)</th> 
                </tr>
            </thead>

brap
Aug 23, 2004

Grimey Drawer
code:
public Thing Thing {get; set;]
This makes me hate C# conventions so much.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Ithaqua posted:

Read Roy Osherove's book The Art of Unit Testing. That's first and foremost.

IMO there should be no such thing as a "test developer". Everyone should be writing unit tests while they write their code. The tests should be considered first-class citizens and live in the same solution as the code they're testing. If you're doing true unit testing, the VS test runner should be configured to run tests after every compilation -- this goes for everyone. If a test breaks, fix the test right away. It doesn't matter who wrote the test that's failing. Testing is all about shortening the feedback loop and identifying and correcting bugs faster. It also helps inform the design of your application -- something that's hard to test is probably badly designed and needs to be refactored. Siloing "developer" and "test developer" is counter to the entire idea of shortening the feedback loop and informing design.

The fastest way to have a failed automated testing adoption is to let the tests be second-class citizens that only one or two people thinks about or maintains. The tests stagnate, and eventually they start to fail. By the time anyone gives a poo poo and goes to look at the tests, they have an awful time figuring out whether the tests are invalid (not testing the right things) or evidence of a bug. Then everything gets thrown out, and a year later someone takes another stab at the whole "testing thing", with exactly the same results.

Doing it right requires a culture change, plain and simple.

Bingo. This is the attitude to have.

Testing is something intrinsic to developing good code. Ultimately as a test lead I would hope you keep everyone else in line when it comes to testing their changes

Che Delilas
Nov 23, 2009
FREE TIBET WEED

fleshweasel posted:

code:
public Thing Thing {get; set;]
This makes me hate C# conventions so much.

It bothers me a little but I don't much like underscores before variable names or putting "My" in front of everything either. Pick your poison.

Inverness
Feb 4, 2009

Fully configurable personal assistant.

Che Delilas posted:

It bothers me a little but I don't much like underscores before variable names or putting "My" in front of everything either. Pick your poison.
I follow the most recent guidelines here: https://github.com/dotnet/corefx/wiki/Contributing#c-coding-style

_private, s_privateStatic, t_privateThreadStatic

Being able to distinguish private variables with a clear underscore which separates them from parameters is great in my opinion.

raminasi
Jan 25, 2005

a last drink with no ice
That doesn't solve the "property name is the same as the property type" problem though.

RICHUNCLEPENNYBAGS
Dec 21, 2010

Inverness posted:

I follow the most recent guidelines here: https://github.com/dotnet/corefx/wiki/Contributing#c-coding-style

_private, s_privateStatic, t_privateThreadStatic

Being able to distinguish private variables with a clear underscore which separates them from parameters is great in my opinion.

OR you could just use this which has the advantage of never being misleadlingly applied.

GrumpyDoctor posted:

That doesn't solve the "property name is the same as the property type" problem though.

That isn't a problem. They specifically designed the language to accommodate the "Color Color" problem where the most sensible name for a field is also the class name.

RICHUNCLEPENNYBAGS fucked around with this message at 06:27 on Mar 8, 2015

Che Delilas
Nov 23, 2009
FREE TIBET WEED

RICHUNCLEPENNYBAGS posted:

That isn't a problem. They specifically designed the language to accommodate the "Color Color" problem where the most sensible name for a field is also the class name.

I see a few rules codifying private fields, but no mention of public properties in that style guide (I use camel for private fields so those are never confusing). Properties are Pascal case pretty much anywhere I've ever seen, that's why it's a problem at all.

RICHUNCLEPENNYBAGS
Dec 21, 2010

Che Delilas posted:

I see a few rules codifying private fields, but no mention of public properties in that style guide (I use camel for private fields so those are never confusing). Properties are Pascal case pretty much anywhere I've ever seen, that's why it's a problem at all.

Huh? I said "the language," right? See here: http://blogs.msdn.com/b/ericlippert/archive/2009/07/06/color-color.aspx

raminasi
Jan 25, 2005

a last drink with no ice

The problem is not miscompilation, the problem is that it looks weird and is potentially confusing.

RICHUNCLEPENNYBAGS
Dec 21, 2010

GrumpyDoctor posted:

The problem is not miscompilation, the problem is that it looks weird and is potentially confusing.

I don't think it is, and, as evidence for the idea that this is sometimes a sensible thing to do, I'm bringing up the fact that the language designers specifically considered this scenario when designing the spec with the idea that it should be allowed.

epswing
Nov 4, 2003

Soiled Meat
I'm not sure how to phrase my question, and google can't yet read my mind.

When I run my MVC site locally, I want to use these ninject bindings. When I run it in production on azure, I want to use those bindings (which are almost the same, except for a couple different bindings).

How do I detect this, and where should this conditional code go?

Che Delilas
Nov 23, 2009
FREE TIBET WEED

Oh whoops, conflated you and another poster.

Mr Shiny Pants
Nov 12, 2012

epalm posted:

I'm not sure how to phrase my question, and google can't yet read my mind.

When I run my MVC site locally, I want to use these ninject bindings. When I run it in production on azure, I want to use those bindings (which are almost the same, except for a couple different bindings).

How do I detect this, and where should this conditional code go?

Create new configuration? Like debug and release and make another Azure one that has the conditionals in it?

RICHUNCLEPENNYBAGS
Dec 21, 2010

epalm posted:

I'm not sure how to phrase my question, and google can't yet read my mind.

When I run my MVC site locally, I want to use these ninject bindings. When I run it in production on azure, I want to use those bindings (which are almost the same, except for a couple different bindings).

How do I detect this, and where should this conditional code go?

I don't know Ninject, as I've always used Unity, but I imagine the solution is pretty much the same:
code:
#if DEBUG

ConfigureWithTestValues();

#else

ConfigureWithProdValues();

#endif

bpower
Feb 19, 2011




Thanks guys, will try this. Yeah, I'm using view models and passing them into the view as IEnumerable<ThingViewModel>

RICHUNCLEPENNYBAGS
Dec 21, 2010

bpower posted:

Thanks guys, will try this. Yeah, I'm using view models and passing them into the view as IEnumerable<ThingViewModel>

You may find your life easier if you switch to IList, because then the "smart" helpers can use the index of the current entry.

Inverness
Feb 4, 2009

Fully configurable personal assistant.

GrumpyDoctor posted:

That doesn't solve the "property name is the same as the property type" problem though.
It's not a problem.

wwb
Aug 17, 2004

UberJumper posted:

So apparently after writing C# unit tests for half a week. I have apparently just inherited all responsibilities as lead test developer. The company is small (~20 people), and honestly even after saying "This is a bad idea" and i don't know anything about being lead test developer, they still put me in this position.

The old lead left on apparently bad terms, and there is absolutely no documentation. There is apparently a Jenkins server, that seems to be pulling from a SVN (we swapped to git a year ago). Apparently they just want to start from scratch when it comes to testing, and i am at a complete and utter loss. Does anyone have any tips for doing this kind of degree of testing for C#.

Greenfield is a lot better than undocumented brownfield so you've got that -- I would requisition a new server ( check out TeamCity if you have a budget ) and and get going there. Do you have an issue tracker? That is probably the 2nd most important thing after source control . . ..

bpower
Feb 19, 2011
Got the check list working, thanks again guys.

Knyteguy
Jul 6, 2005

YES to love
NO to shirts


Toilet Rascal
Is this an acceptable async pattern?

C# code:
public async static void DoFooAsync()
{
	return await Task.Run(() =>
	{
		// do non-asynchronous work
	});
}
C# code:
public async void DoBarAsync()
{
	await DoFooAsync();
}
The stuff in the Task.Run lambda is run async?

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Knyteguy posted:

Is this an acceptable async pattern?

C# code:
public async static void DoFooAsync()
{
	return await Task.Run(() =>
	{
		// do non-asynchronous work
	});
}
C# code:
public async void DoBarAsync()
{
	await DoFooAsync();
}
The stuff in the Task.Run lambda is run async?

In general don't do this.
http://www.ben-morris.com/why-you-shouldnt-create-asynchronous-wrappers-with-task-run/

Also never have an async void method unless it's an event handler.

Knyteguy
Jul 6, 2005

YES to love
NO to shirts


Toilet Rascal

Ithaqua posted:

In general don't do this.
http://www.ben-morris.com/why-you-shouldnt-create-asynchronous-wrappers-with-task-run/

Also never have an async void method unless it's an event handler.

Ah, I had a feeling. Thanks for the info.

duck monster
Dec 15, 2004

UberJumper posted:

So apparently after writing C# unit tests for half a week. I have apparently just inherited all responsibilities as lead test developer. The company is small (~20 people), and honestly even after saying "This is a bad idea" and i don't know anything about being lead test developer, they still put me in this position.

The old lead left on apparently bad terms, and there is absolutely no documentation. There is apparently a Jenkins server, that seems to be pulling from a SVN (we swapped to git a year ago). Apparently they just want to start from scratch when it comes to testing, and i am at a complete and utter loss. Does anyone have any tips for doing this kind of degree of testing for C#.

Jenkins is really quite a simple minded piece of software and its all software configured. Find whatever plugins it needs to support C#, make sure your test harness is capable of making GBS threads out the right sort of XML (or conversely find a jenkins plugin that can handle what your test suite DOES put out) and its all kinda good from there. That said, I only know Jenkins on unix hosts so.....

epswing
Nov 4, 2003

Soiled Meat
EF Migration questions! So many questions.

In this solution, I have
  • FooBarAsp, an asp project (provides a UI for the application)
  • FooBar, a class library (the application)
  • FooBar.Tests, a test project (tests the application)

FooBar uses EF 6 Code First, and contains a number of models, and a DataContext. FooBarAsp uses Microsoft's Identity framework for user authentication, and has a UserContext. Both contexts play nice and work as expected. Global.asax.cs will execute MigrateDatabaseToLatestVersion (right?). FooBar.Tests will execute DropCreateDatabaseAlways and won't care about migrations (right?).

Should I EnableMigrations :aaa: in FooBarAsp, or FooBar? For both contexts, or just DataContext?

Edit: After running EnableMigrations in FooBar (just for kicks), my __MigrationHistory table contains two InitialCreate rows, one with ContextKey=FooBar.Models.DataContext and the other with ContextKey=FooBarAsp.Models.UserContext. So they're both being tracked already? If so, (and since I did not enable automatic migrations), do I need to explicitly run both MigrateDatabaseToLatestVersion<DataContext> and MigrateDatabaseToLatestVersion<UserContext> in Global.asax.cs?

epswing fucked around with this message at 02:50 on Mar 10, 2015

EssOEss
Oct 23, 2006
128-bit approved
Your Identity Framework authentication code should be using your main data context, not a separate one. I forget what it required in detail but it was fairly trivial to do - just sticking a few interfaces on your main data context or something like that.

RICHUNCLEPENNYBAGS
Dec 21, 2010

EssOEss posted:

Your Identity Framework authentication code should be using your main data context, not a separate one. I forget what it required in detail but it was fairly trivial to do - just sticking a few interfaces on your main data context or something like that.

Making your DbContext inherit from IdentityContext<TUser>.

As for migrations, don't turn them on until you're going into prod. The main purpose is making it easy to upgrade the application without losing any data when the new version requires schema changes.

epswing
Nov 4, 2003

Soiled Meat

EssOEss posted:

Your Identity Framework authentication code should be using your main data context, not a separate one. I forget what it required in detail but it was fairly trivial to do - just sticking a few interfaces on your main data context or something like that.

This doesn't make sense to me. My application (FooBar) doesn't know or care about who is referencing it and poking it (whether that be an ASP project (FooBarAsp), or a Test project (FooBar.Tests), or a Console project). My ASP project references my class library, not the other way around. So my DataContext doesn't "know about" ASP or Identity or Sessions or Cookies.

RICHUNCLEPENNYBAGS posted:

Making your DbContext inherit from IdentityContext<TUser>.

As for migrations, don't turn them on until you're going into prod. The main purpose is making it easy to upgrade the application without losing any data when the new version requires schema changes.

I'm going into prod. It's time.

Mr Shiny Pants
Nov 12, 2012

epalm posted:

This doesn't make sense to me. My application (FooBar) doesn't know or care about who is referencing it and poking it (whether that be an ASP project (FooBarAsp), or a Test project (FooBar.Tests), or a Console project). My ASP project references my class library, not the other way around. So my DataContext doesn't "know about" ASP or Identity or Sessions or Cookies.


I'm going into prod. It's time.

I made table that links the ASP id to my own tables. There was no simple way around that I found, it wants a store. So my server app has an ASP identity to local id mapping table. ASP also uses GUIDS to represent IDs and my app uses integers.....

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

If you must maintain XP compatibility and must therefore target .NET 4.0, is it a total no-brainer to grab the BCL Portability Pack to get access to async/await or are there any drawbacks to consider?

RICHUNCLEPENNYBAGS
Dec 21, 2010

epalm posted:

This doesn't make sense to me. My application (FooBar) doesn't know or care about who is referencing it and poking it (whether that be an ASP project (FooBarAsp), or a Test project (FooBar.Tests), or a Console project). My ASP project references my class library, not the other way around. So my DataContext doesn't "know about" ASP or Identity or Sessions or Cookies.

None of your entities hold references to users? That's kind of weird. Usually there's something (like, if you have a blog, the author of the blog is probably a user).

Adbot
ADBOT LOVES YOU

epswing
Nov 4, 2003

Soiled Meat

RICHUNCLEPENNYBAGS posted:

None of your entities hold references to users? That's kind of weird. Usually there's something (like, if you have a blog, the author of the blog is probably a user).

My Blogs have Users, sure, both of which are contained in my class library (FooBar).

The stock ApplicationUser that comes with ASP is a separate entity (and separate DbContext, which happens to point to the same database). ASP's ApplicationUser and the Identity framework take care of authentication, registration, , email verification, login, logout, passwords, password strength, passwords resetting, two factor authentication, cookies, sessions, external login from sources like facebook/google, etc. FooBar doesn't know or care about any of that. FooBar's Users have a UserName, and ASP's ApplicationUsers have a UserName. When a user logs in, I just look up Foobar's User by UserName, and that's who is logged in. So when I create a new Blog, the author is FooBar's User.

The end result is that FooBar is not a Web/Desktop/Console/iPhone/Android application. It's a library that any of those things can reference, interact with, and provide a user interface for. The point is that FooBar is not polluted or biased by any of those user interfaces.

Edit: Note that this means I needed to add a line to Identity's AccountController.Register that creates a FooBar User with the same UserName. But that's honestly about it.

epswing fucked around with this message at 01:47 on Mar 11, 2015

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