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
kingcrimbud
Mar 1, 2007
Oh, Great. Now what?
I'm trying to combine a few components of the Enterprise Library. In this instance, I want to implement some retry logic (Transient Fault Handling) but I want to add this in behind-the-scenes (via Unity Interception). I'm using interception elsewhere but I can't seem to figure out the syntax needed to add these two components together.

For interception, this will essentially be a no-op, but is where I can add my retry logic, within an IInterceptionBehavior:

code:
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
    return getNext()(input, getNext);
}
Let's assume that I want to retry an awaitable service call, that takes in a single request object and returns a single response object:

code:
public async Response DoSomething(Request)
I've injected my RetryPolicy into my implementation of IInterceptionBehavior, which contains the Invoke method above. But how do I combine the two? it'd be along the lines of this, but a whole lot messier I'm assuming:

code:
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
    return this.RetryPolicy.ExecuteAsync(getNext()(input, getNext));
}
Anyone have experience with this?

tl;dr I know how to use Transient Fault Handling and Unity Interception, but don't know how to combine them together

Adbot
ADBOT LOVES YOU

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

kingcrimbud posted:

I'm trying to combine a few components of the Enterprise Library. In this instance, I want to implement some retry logic (Transient Fault Handling) but I want to add this in behind-the-scenes (via Unity Interception). I'm using interception elsewhere but I can't seem to figure out the syntax needed to add these two components together.

For interception, this will essentially be a no-op, but is where I can add my retry logic, within an IInterceptionBehavior:

code:
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
    return getNext()(input, getNext);
}
Let's assume that I want to retry an awaitable service call, that takes in a single request object and returns a single response object:

code:
public async Response DoSomething(Request)
I've injected my RetryPolicy into my implementation of IInterceptionBehavior, which contains the Invoke method above. But how do I combine the two? it'd be along the lines of this, but a whole lot messier I'm assuming:

code:
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
    return this.RetryPolicy.ExecuteAsync(getNext()(input, getNext));
}
Anyone have experience with this?

tl;dr I know how to use Transient Fault Handling and Unity Interception, but don't know how to combine them together

Bueller? Anyone? I still haven't figured this out.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

Sedro posted:

A beautiful example

These lines are a lot simpler than I thought they would be:

code:

            {
                var result = getNext()(input, getNext);
                return result.ReturnValue as Task<Response>;
            });
            return input.CreateMethodReturn(task);

Now sure why I expected a complicated solution, this looks perfect. Thank you!

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?
I'm in the middle of creating a service that consumes something like 100 other services. My service will have its own model. Let's say my model has a Pet entity. And 15 of these other services have their own representations of what a pet is.

If I search these other systems and then try to represent these pets in my system, how can I model that correctly when each system has their own representation of a pet?

We've explored inheritance and creating a type per system. This gets really ugly fast and will be awful to maintain.
We've also explored decorating and compositing but again, this gets really complicated very quickly. In some cases, a Pet entity may have to have 20 new properties tacked on.

I'm currently looking at using the DynamicObject class and having my Pet inherit from it. I'm losing out on some safety and probably performance, but I can just set whatever properties I want on my Pet whenever I want.

Anyone experience a situation like this and come up with a good answer?

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

Dirk Pitt posted:

Ahhh, I was using task.run to avoid having to make an async void.

What am I supposed to do if I need to await an asyncronous method in an override void or constructor? I typically make everything async all the way down. This is an issue I am curious about in a framework I have little control over (xamarin).

Task is the return type for async methods that would otherwise return void.


C# code:
public async Task ReturnsVoid()
{
    await SomeAsyncMethod();
}

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

Ithaqua posted:

If you're dealing with a void method that you're overriding, you can't go and change the method signature to Task. That's the problem. You also can't have an async constructor, for obvious reasons.

You're right, I looked over the changing of the signature part.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?
What is the proper approach to using FileConfigurationSource with a relative path to a config in another project? The value that works locally does not work once deployed. I believe the config has been set to copy if newer. We've also tried adding the config as a link in the project creating the FileConfigurationSource.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

GrumpyDoctor posted:

Here I am patiently waiting for a connection to my HttpListener:
C# code:
var conn = await httpListener.GetContextAsync();
But oh no! Somewhere else, someone is trying to shut the listener down!
C# code:
httpListener.Stop();
And the await throws an exception:
code:
A first chance exception of type 'System.Net.HttpListenerException' occurred in mscorlib.dll

Additional information: The I/O operation has been aborted because of either a thread exit or an application request
What stupid thing am I doing?

You're not running this in a console project are you? I've forgotten to Console.Readkey or whatever before when awaiting in a quick POC project and seen similar results.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

RICHUNCLEPENNYBAGS posted:

Unit testing can be really handy but I don't believe in TDD. Also, I think it's a little bit like a church in that even its adherents don't actually always do it, but they all find guilty about it and try to make other people feel guilty too.

Also proponents of TDD frequently make unjustifiable claims, like saying the tests "prove" their software is defect-free. No, they don't, because the number of possible states is mind-boggling and that assumes your tests are correct. Certainly they can be a useful heuristic.

On the topic, this article is interesting and I agree with it.

Unit tests don't necessarily show that software is 'correct'. They simply allow you to show it does something, so that when you make changes to your software you can quickly see how that something changed. The better your testing, the more accurate you can measure the potential delta.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

RICHUNCLEPENNYBAGS posted:

So we are in agreement.

Yes and no. I personally think TDD makes sense on paper. With what I said before, unit tests can guarantee a minimum state of the classes in your system. Once a class changes, tests can go red or they can stay green.

If you've truly designed your system well (good luck doing that) where you're open for extension and closed for modification, then your tests from TDD will already cover your previous code and you'll never go red. Two problems with this though is that we'll never design it perfectly the first time and requirements will always shift.

Until we get perfect requirements with which we can perfectly design, TDD will not be perfect.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

RICHUNCLEPENNYBAGS posted:

Yeah, that's pretty much the same as EF.


Why is that better than configuring it to use MVC-style routing, exactly?

There's no second guessing what the url of an action is when they're on top of each other.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?
Enterprise Library Semantic Logging question here. We're logging out of process to a few rolling files and it works great except for one small thing. The logs aren't written to the file unless the service is stopped/restarted or the file rolls over. This doesn't allow us to read into the files at 'real-time' and it's a pain. This is not standard behavior apparently and is happening locally and on the server.

I don't think too many people use SLAB but maybe someone has an idea?

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

Boz0r posted:

Now I've got another problem. I'm trying to get this Entity Framework to work with my database. Right now, I've added a couple of objects via code, but when I want to retrieve them, I just get an empty collection.

code:
public ViewResult Index()
{
	using (var ctx = new DatabaseContext())
	{
		ctx.Areas.Add(new Area("First"));
		ctx.Areas.Add(new Area("Second"));
		ctx.Areas.Add(new Area("Third"));
		ctx.SaveChanges();
	}

	IEnumerable<Area> areas = productRepository.Areas;

	return View();
}
code:
public class ProductRepository
{
	private DatabaseContext context = new DatabaseContext();

	public IEnumerable<Area> Areas{ get { return context.Areas; } }
}
    
public class DatabaseContext : DbContext
{
	public DatabaseContext():base("test")
	{
	}
	public DbSet<Area> Areas { get; set; }
}
What did I do wrong?

Your Index() method is not enumerating over your locally declared areas variable and because you're not returning this variable by this method, your database won't even be hit with a select statement. Add this to your config to see what's actually happening in your database because of EF: http://blog.oneunicorn.com/2014/02/09/ef-6-1-turning-on-logging-without-recompiling/

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?
Relating to the Entity Framework talk above, what are people using with Web API and Unity when they need PerRequestLifetimeManager behavior? I don't want to pull in all these MVC packages to use one class (PerRequestLifetimeManager) to manage my DbContexts. And frankly, registering this lifetime manager doesn't seem to be currently working anyway for me even when I register it. I'm still seeing singleton behavior.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

kingcrimbud posted:

Relating to the Entity Framework talk above, what are people using with Web API and Unity when they need PerRequestLifetimeManager behavior? I don't want to pull in all these MVC packages to use one class (PerRequestLifetimeManager) to manage my DbContexts. And frankly, registering this lifetime manager doesn't seem to be currently working anyway for me even when I register it. I'm still seeing singleton behavior.

I figured this out after creating a new solution. Be careful how you register DelegatingHandlers! Their singleton behavior means dependencies will not resolve per your expectations.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

Night Shade posted:

If you want per request scoping to work right in a DelegatingHandler, you need to call request.GetDependencyScope() in SendAsync and use the resulting IDependencyScope as a service locator.

Been there, done that.

I ended up following this http://stackoverflow.com/a/26472999 and using an IUnityContainer dependency instead of a WindsorContainer.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?
Code First Existing Database Entity Framework Question

I'm using Code First with its fluent type configurations currently. I'd like to transform the following table into the following model.

Existing Database:
code:
Customer Table			
ID	FirstName	LastName	Phone (not null)	        WorkPhone (null)	    CellPhone (null)
int	varchar(30)	varchar(30)	varchar(20)	                varchar(20)	            varchar(20)
Proposed model:

code:
    public class Customer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public IEnumerable<Phone> Phones { get; set; }
    }
code:
    public class Phone
    {
        public string AreCode { get; set; }
        public string Number { get; set; }
        public PhoneType? Type { get; set; }
    }
code:
    public enum PhoneType
    {
        Home,
        Work,
        Cell
    }
In this way a customer will have either 1, 2, or 3 phones in its Phones collection, each with a different type.

Is this even possible or am I going to need to mess around with extending the Customer and Phone classes to create "proxy" classes and then "newing" properties in the derived types? There's also another issue that phone number values are stuffed in other columns of other tables as well.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

Inverness posted:

Why not simply have Phone, WorkPhone, and CellPhone properties in the Customer class as it is shown in the existing database?

The existing database is but one system of several that I need to bring together into a common model.

In your example, what if a customer had several cell phones? Why stop at those 3 types of phones? We now have [num of phone type] lists as properties of a customer. I'd prefer one list as a property which is a bit more extensible imo.

Sedro posted:

You could throw in a computed property for Phones and have your ORM ignore it

That's what I was working towards today but it was getting messy with logic in properties, unnecessary inheritance, etc. I wanted to see if I was just completely missing something obvious.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

crashdome posted:

Personally, I would start with the database. Have the DBA (or yourself if that's the case) run a procedure to extract the phone #s to another table. Then code to that.

Or maybe I missed something with your question? Is the table used in another project that's preventing you from altering it?

I would love to but the table is currently backing an old and not going away anytime soon large support application with a lot of traffic.

The question remains; is this a roll-your-own scenario?

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?
Why would a sql query generated from Entity Framework take minutes to execute, yet when the same query is run within SSMS, it takes less than a second?

Entity Framework 6.1.3
SQL Server 2005 Enterprise

I'm grabbing the sql generated using the Infrastructure Interception DatabaseLogger registered in web.config. I'm unable to trace or profile in SSMS due to a current lack of permissions so I don't have any other details yet.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

Calidus posted:

Are you by chance doing a insert? In my experience entity inserts are terribly slow. Entity also seems to take a larger performance hit do to missing keys and indexes than raw sql.

It's a simple select, top 100 from table_x, joined to table_y. Filters are on a few table_y columns.

The database has no constraints and I'm not sure the indexes it does have are even relevant. Hard to tell until I can get additional db permissions.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

epalm posted:

Automapper mapping configurations

Separate your models from how your models are mapped. https://en.wikipedia.org/wiki/Single_responsibility_principle

I also recommend looking into using Automapper non-statically. Testing is a bit easier. Configure mappings via Profiles then inject in IMappingEngine instances as needed to map.

Adbot
ADBOT LOVES YOU

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

fleshweasel posted:

What does it enable you to test? That your program still works even if you substitute in some other Automapper implementation?

I think people should buckle down and write the translators. Pretty soon you will find yourself writing enough logic in a translator that if you start doing all that stuff through the Automapper API you'll be miserable in trying to debug and maintain it.

Injecting interfaces allows for easier testing, especially when using Moq. Mock<>.Verify is a great way to ensure things are called or not called. Using static classes just makes things more difficult to test.

And I agree with your second point in theory, I just haven't seen a case where the situation called for mapping between tiers/systems and Automapper was unable to do it cleanly and consistently.

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