Monday, March 7, 2011

How to dispose asynchronously?

Let's say I have a class that implements the IDisposable interface. Something like this:

http://www.flickr.com/photos/garthof/3149605015/

MyClass uses some unmanaged resources, hence the Dispose() method from IDisposable releases those resources. MyClass should be used like this:

using ( MyClass myClass = new MyClass() ) {
    myClass.DoSomething();
}


Now, I want to implement a method that calls DoSomething() asynchronously. I add a new method to MyClass:

http://www.flickr.com/photos/garthof/3149605005/

Now, from the client side, MyClass should be used like this:

using ( MyClass myClass = new MyClass() ) {
    myClass.AsyncDoSomething();
}

However, if I don't do anything else, this could fail as the object myClass might be disposed before DoSomething() is called (and throw an unexpected ObjectDisposedException). So, the call to the Dispose() method (either implicit or explicit) should be delayed until the asynchronous call to DoSomething() is done.

I think the code in the Dispose() method should be executed in a asynchronous way, and only once all asynchronous calls are resolved. I'd like to know which could be the best way to accomplish this.

Thanks.

NOTE: For the sake of simplicity, I haven't entered in the details of how Dispose() method is implemented. In real life I usually follow the Dispose pattern.


UPDATE: Thank you so much for your responses. I appreciate your effort. As chakrit has commented, I need that multiple calls to the async DoSomething can be made. Ideally, something like this should work fine:

using ( MyClass myClass = new MyClass() ) {

    myClass.AsyncDoSomething();
    myClass.AsyncDoSomething();

}

I'll study the counting semaphore, it seems what I'm looking for. It could also be a design problem. If I find it convenient, I will share with you some bits of the real case and what MyClass really does.

From stackoverflow
  • Async methods usually have a callback allowing you to do do some action upon completition. If this is your case it would be something like this:

    // The async method taks an on-completed callback delegate
    myClass.AsyncDoSomething(delegate { myClass.Dispose(); });
    

    An other way around this is an async wrapper:

    ThreadPool.QueueUserWorkItem(delegate
    {
        using(myClass)
        {
            // The class doesn't know about async operations, a helper method does that
            myClass.DoSomething();
        }
    });
    
  • I wouldn't alter the code somehow to allow for async disposes. Instead I would make sure when the call to AsyncDoSomething is made, it will have a copy of all the data it needs to execute. That method should be responsible for cleaning up all if its resources.

  • It looks like you're using the event-based async pattern (see here for more info about .NET async patterns) so what you'd typically have is an event on the class that fires when the async operation is completed named DoSomethingCompleted (note that AsyncDoSomething should really be called DoSomethingAsync to follow the pattern correctly). With this event exposed you could write:

    var myClass = new MyClass();
    myClass.DoSomethingCompleted += (sender, e) => myClass.Dispose();
    myClass.DoSomethingAsync();
    

    The other alternative is to use the IAsyncResult pattern, where you can pass a delegate that calls the dispose method to the AsyncCallback parameter (more info on this pattern is in the page above too). In this case you'd have BeginDoSomething and EndDoSomething methods instead of DoSomethingAsync, and would call it something like...

    var myClass = new MyClass();
    myClass.BeginDoSomething(
        asyncResult => {
                           using (myClass)
                           {
                               myClass.EndDoSomething(asyncResult);
                           }
                       },
        null);
    

    But whichever way you do it, you need a way for the caller to be notified that the async operation has completed so it can dispose of the object at the correct time.

  • You could add a callback mechanism and pass a cleanup function as a callback.

    var x = new MyClass();
    
    Action cleanup = () => x.Dispose();
    
    x.DoSomethingAsync(/*and then*/cleanup);
    

    but this would pose problem if you want to run multiple async calls off the same object instance.

    One way would be to implement a simple counting semaphore with the Semaphore class to count the number of running async jobs.

    Add the counter to MyClass and on every AsyncWhatever calls increment the counter, on exits decerement it. When the semaphore is 0, then the class is ready to be disposed.

    var x = new MyClass();
    
    x.DoSomethingAsync();
    x.DoSomethingAsync2();
    
    while (x.RunningJobsCount > 0)
        Thread.CurrentThread.Sleep(500);
    
    x.Dispose();
    

    But I doubt that would be the ideal way. I smell a design problem. Maybe a re-thought of MyClass designs could avoid this?

    Could you share some bit of MyClass implementation? What it's supposed to do?

    Nicholas Mancuso : Sleeping in a loop is considered bad practice. You should be using an EventWaitHandle to fire off an event upon finishing.
    Auron : Unfortunately, I'm limited to .NET CF 1.0, and the class Semaphore is only supported since .NET 2.0.
    chakrit : It's just an "example"... mind you.
  • So, my idea is to keep how many AsyncDoSomething() are pending to complete, and only dispose when this count reaches to zero. My initial approach is:

    public class MyClass : IDisposable {
    
        private delegate void AsyncDoSomethingCaller();
        private delegate void AsyncDoDisposeCaller();
    
        private int pendingTasks = 0;
    
        public DoSomething() {
         // Do whatever.
        }
    
        public AsyncDoSomething() {
         pendingTasks++;
         AsyncDoSomethingCaller caller = new AsyncDoSomethingCaller();
         caller.BeginInvoke( new AsyncCallback( EndDoSomethingCallback ), caller);
        }
    
        public Dispose() {
         AsyncDoDisposeCaller caller = new AsyncDoDisposeCaller();
         caller.BeginInvoke( new AsyncCallback( EndDoDisposeCallback ), caller);
        }
    
        private DoDispose() {
         WaitForPendingTasks();
    
         // Finally, dispose whatever managed and unmanaged resources.
        }
    
        private void WaitForPendingTasks() {
         while ( true ) {
          // Check if there is a pending task.
          if ( pendingTasks == 0 ) {
           return;
          }
    
          // Allow other threads to execute.
          Thread.Sleep( 0 );
         }
        }
    
        private void EndDoSomethingCallback( IAsyncResult ar ) {
         AsyncDoSomethingCaller caller = (AsyncDoSomethingCaller) ar.AsyncState;
         caller.EndInvoke( ar );
         pendingTasks--;
        }
    
        private void EndDoDisposeCallback( IAsyncResult ar ) {
         AsyncDoDisposeCaller caller = (AsyncDoDisposeCaller) ar.AsyncState;
         caller.EndInvoke( ar );
        }
    }
    

    Some issues may occur if two or more threads try to read / write the pendingTasks variable concurrently, so the lock keyword should be used to prevent race conditions:

    public class MyClass : IDisposable {
    
        private delegate void AsyncDoSomethingCaller();
        private delegate void AsyncDoDisposeCaller();
    
        private int pendingTasks = 0;
        private readonly object lockObj = new object();
    
        public DoSomething() {
         // Do whatever.
        }
    
        public AsyncDoSomething() {
         lock ( lockObj ) {
          pendingTasks++;
          AsyncDoSomethingCaller caller = new AsyncDoSomethingCaller();
          caller.BeginInvoke( new AsyncCallback( EndDoSomethingCallback ), caller);
         }
        }
    
        public Dispose() {
         AsyncDoDisposeCaller caller = new AsyncDoDisposeCaller();
         caller.BeginInvoke( new AsyncCallback( EndDoDisposeCallback ), caller);
        }
    
        private DoDispose() {
         WaitForPendingTasks();
    
         // Finally, dispose whatever managed and unmanaged resources.
        }
    
        private void WaitForPendingTasks() {
         while ( true ) {
          // Check if there is a pending task.
          lock ( lockObj ) {
           if ( pendingTasks == 0 ) {
            return;
           }
          }
    
          // Allow other threads to execute.
          Thread.Sleep( 0 );
         }
        }
    
        private void EndDoSomethingCallback( IAsyncResult ar ) {
         lock ( lockObj ) {
          AsyncDoSomethingCaller caller = (AsyncDoSomethingCaller) ar.AsyncState;
          caller.EndInvoke( ar );
          pendingTasks--;
         }
        }
    
        private void EndDoDisposeCallback( IAsyncResult ar ) {
         AsyncDoDisposeCaller caller = (AsyncDoDisposeCaller) ar.AsyncState;
         caller.EndInvoke( ar );
        }
    }
    

    I see a problem with this approach. As the release of resources is asynchronously done, something like this might work:

    MyClass myClass;
    
    using ( myClass = new MyClass() ) {
        myClass.AsyncDoSomething();
    }
    
    myClass.DoSomething();
    

    When the expected behavior should be to launch an ObjectDisposedException when DoSomething() is called outside the using clause. But I don't find this bad enough to rethink this solution.

    Rob McCready : In the using (MyClass myclass = new....) example I think the myclass object becomes unanchored at the end of the using block and available to be Finalized. I'm pretty sure to be safe you need to call and block on a Waiting method before the end of the using block.

How do you attach to events on a ScriptObject in Silverlight?

The HtmlObject provides all the necessary functionality to register managed event handlers for script and DOM events, but what if the class you need to listen to doesn't exist as a DOM element, but a scripting variable (referenced via ScriptObject) instead?

From stackoverflow
  • A javascript object doesn't support the concept of attached events. However it may support the concept of a property holding a reference to function that if assigned will be called at a certain point.

    I take you have such an object?

    If so you use the ScriptObject SetProperty method using the name of the property that should hold a reference to a function and a delegate to Managed method matches the signature that the Javascript object will call.

    Caveat the following is untested at this point but should put you on the right path.

    //Javascript in web page.
    var myObj = new Thing();
    
    function Thing()
    {
         this.doStuff = function()
         {
             if (this.onstuff) this.onstuff("Hello World");
         }
    }
    
    // C# code in a Silverlight app.
    
    class SomeClass
    {
        private ScriptObject myObject;
        public SomeClass(ScriptObject theObject)
        {
             myObject = theObject;
             myObject.SetProperty("onstuff", (Action<string>)onstuff);
        } 
    
        function void onstuff(string message)
        {
             //Do something with message
        }
    
    }
    
  • As stated by AnthonyWJones, Silverlight can't attached to JavaScript events. The right thing to do in this situation is to do the following:

    Enable scripting access in Silverlight:

    1. Mark the class with the ScriptableType attribute, or mark the specific methods with ScriptableMember
    2. Call HtmlPage.RegisterScriptableObject in the constructor.

    Once everything is set up in the Silverlight code, here's what you do in JavaScript:

    1. Obtain a reference to the JavaScript object and register an event handler
    2. Use document.getElementById to get the Silverlight control
    3. Call .Content.. in the JavaScript event handler. For example, silverlight.Content.Page.UpdateText(text).

    So basically, all event handling is performed in JavaScript, but the JavaScript event handlers can be used to call functions in Silverlight.

Can WCF Service have constructors?

When I new a WCF service in my solution, can I do the following, have a constructor with parameter to pass in? If yes, how, when and where does the runtime fill in my required IBusinessLogic object?

[ServiceContract]
public interface IServiceContract
{
    [OperationContract]
    ...
}

public class MyService : IServiceContract
{
    IBusinessLogic _businessLogic;
    public ServiceLayer(IBusinessLogic businessLogic)
    {
        _businessLogic = businessLogic;
    }
    ...
}
From stackoverflow
  • Look at ServiceHostFactory.

  • WCF will only use the default constructor, you can't use parameterised constructors.

  • You can get WCF to (sort of indirectly) call non default constructors, for that to work you need to roll your own instance provider. You would need to implement IInstanceProvider and add a custom Service Behavior. Some links that will show you how to do this in combination with Spring.NET:

    WCF Service Dependency Injection

    Code example WCF Service Dependency Injection

  • Another case, in addition to the other responses, is when creating singleton service - this is when you pass an instance of your service to the ServiceHost (as opposed to a type);

    Obviously as you create the instance you can use whichever constructor;

  • You have to implement IInstanceProvider to be able to call a parametrized service constructor. Of this constructor will not be available in the generated proxy.

Help me find an open source project to join.

Hi.

For quite some time I have been thinking about joining an open source project. First of all, joining a project will help me become a better developer, and I’d like the challenge of working with people from around the world (I’m from Denmark). Secondly, I use a lot of open source software, both at work and in my spare time so it’s about time I at least try to give something back.

Currently I’m a .NET developer (C# mostly) and have been so for about 3 years. Before that I did a lot of classic ASP and VBScript. So I’m primarily a Windows/Microsoft guy, but I’ve recently started to pick up on Ruby/RoR.

I’d like some input to which project I can join which will help me become a better developer. For now I’m thinking it should be concentrated around .NET, as my other language skills are not yet ready to meet the world :-)

I like doing both ASP.NET as well as WinForms, but apart from that I think I’m very open to any suggestions.

Thank you in advance.


A good question, but closed as a duplicate of many questions already cited so thankfully I don't have to search-copy-paste URLs here :)

From stackoverflow
  • I'd be happy to see other people's replies for this. But a good advice is to find a .NET project you really like. Then start to play around with the source. See if you can add features, tighten weak points, find bugs and so on, then submit the pearls to the project's mailinglist or authors.

    Giving something back to the project is a good way of proving that you could be a viable member of a project. After doing this for a while, you shouldn't be surprised to be asked to join in on the development.

    Some places to search:

  • A few places to start looking :-)

    http://stackoverflow.com/questions/392947/what-is-a-good-open-source-c-project-for-me-to-get-involved-in

    http://stackoverflow.com/questions/43649/how-to-get-involved-in-an-open-source-project

    http://stackoverflow.com/questions/38881/how-to-find-opensource-projects-looking-for-help

  • The Microsoft world sure needs Open Source enthusiasts of the right spirit. Crap like DotNetNuke shows it. I would try to filter away projects that are primarily commersial like that.

    For filtering in. I think you should think about where your skills are concentrated in terms of application types and also what you feel the hardest for. And from there find projects that are small enough. Like the rails world where you can find plugin projects of various sizes. Browse Github some and I'm sure you'll stumble across stuff that gets your fingers itching for dirt!

  • My personal advise it to look for projects close to your other spare time leisures/hobbies.

    You might find something in "your" programming languages, or you might find the need for a tool that does not exist yet, and write it in the wanted programming languages and technologies you want to try out.

    Here is what has worked very well for me:

    • Music: Rockbox, a firmware replacement for many mp3-players and portable media players.
    • Photography: CHDK, a firmware addition to numerous Canon compact still cameras.
    frankodwyer : thanks for the pointer to chdk, been wishing my canon could show the actual amount of battery remaining for ages.

Need help on selecting items in simple database in python.

I'm trying to write a circuit schematic drawing tool in Python. I'm creating a simple database based on dictionaries which holds all the components and their properties. I'm also trying to create a simple query language where you can select, for example, all resistors with value>100ohms or footprint='0402'

So far, I can select things using some primitive predicate search and then union and intersection for more complex things.

However, I'm having trouble defining what the semantics ought to be for a purely negative search. For instance, something like

footprint != '0402'

should select all items with footprint not equal to 0402. But doing this with only intersection gives me a blank result. For this to work I need to select all components, then intersect with "not 0402" to remove the ones I don't want.

But this seems kind of brute force and seems like a fishy solution. I'm not interested in using a "real" database and query language, so please don't suggest that. I'm looking for the proper engineering rationale here, not necessarily a workaround to the problem.

This problem was covered to some degree in SICP book, but I got very confused, since I think they were also using continuations and stuff, which I don't get yet.

Can someone please explain what the 'proper' usage ought to be for negatively selecting stuff. I tried this in a commercial cad tool and it worked as expected, but then I've seen some SQL query examples (I think), which selected things first, then removed the unwanted ones.

thanks
michael

From stackoverflow
  • It really depends on how you've implemented this "simple database based on dictionaries" as to why straight negation isn't working - you'll have to give us some more clues.

    There's no reason why a straightforward negation match shouldn't work in Python, e.g.:

    components = [
        { 'name': 'resistor', 'footprint': '0402', },
        { 'name': 'LED', 'footprint': '0100', },
        { 'name': 'speaker', 'footprint': '2000', },
    ]
    
    [comp for comp in components if comp['footprint'] != '0402']
    # [{'footprint': '0100', 'name': 'LED'}, {'footprint': '2000', 'name': 'speaker'}]
    

    At a stretch, this is a simple database based on dictionaries - the specific capabilities are really going to depend on your actual implementation.

    I'm resisting the temptation to suggest use of a real database and query language as I assume this is a learning exercise. It is a learning exercise, right? :)

    Alabaster Codify : In set theory, taking out set members based on their properties doesn't have an atomic notation or operation. It could be expressed as so, which does in a way have a "select all": T = { c : c ∈ C; footprint(c) ≠ 0402 }, if C is the set of components. Feels like you're mixing models, though...
    Alabaster Codify : Oh, and I was happy to play along with "don't recommend using SQL", but please don't expect to get anywhere with a competitor to SQL as a query language unless you are addressing a completely new area. Completely new. Far too much inertia with SQL.
  • You'll be a lot happier with a proper database.

    You have SQLite with your Python distribution. Simply define tables instead of dictionaries and use SQL.

    If you need more power and sophistication, you can add SQLAlchemy (or SQLObject) and you won't struggle with these problems.

    S.Lott : It's amazingly fast -- all in memory. Actually, the entire thing is implemented in "driver". No servers, no config, nothing.
    Parand : +1 on SQLite, I've used it in quite a few situations where I didn't want a "database". You can throw in something like SQLAlchemy if you don't want to directly touch SQL.
    Ben Blank : Best of all, you can use SQLite even with an existing datasource by opening a database named ":memory:" and writing all your data to it. You can then manipulate your data with SQL and, if desired, write it back to your datasource afterward without ever writing anything to disk.
  • I agree with S.Lott that you would be happier with a real DB. SQLite is really light and fast, almost no drawbacks on using it.

    if you really need to expose a simpler, yet complete, query language to the user, check JSONPath or JSONQuery (the second is a superset of the first). The exact syntax is (obviously) thought for JavaScript; but it should give you some ideas.

    also, try to check similarities and differences with XPath and XQuery. it helps to see what is useful and what's culture-specific.

  • Can one of you DB-fluent people translate this google-like query into SQLite for me please, here with dotdict fields in the PyPI xmlrpc db --

    summary: this that - butnotthis  version: > 0.1  author: James | Javier
    

    i.e. d.summary ~ "this" and d.summary ~ "that" and d.summary !~ "butnotthis" and d.version > "0.1" and d.author ~ "James|Javier"

    Aand, does anyone have a grammar and parser for such google-like queries ?

    thanks, cheers -- denis

Is Asp.Net MVC a suitable solution for an enterprise project?

We are preparing to develop an enterprise Human Resources Project using ASP.NET and our team is interested in using MVC instead of WebForms but we are a bit worried about this.

We are not sure that this is a suitable solution for an Enterprise project.

From stackoverflow
  • ASP.NET MVC is just a different model than classic ASP.NET, an extension if you will.

    From here:

    The ASP.NET MVC Framework is essentially the Microsoft's attempt to create an ASP.NET programming environment centered around the MVC pattern. For the time being the MVC Framework should be considered an alternative to Web Forms. To some extent, the MVC Framework and Web Forms have in common more or less what cars and motorcycles share. Both can take you somewhere else, but with different speed, comfort, sense of freedom, size of the trunk.

    stackoverflow for example is built on it and works great.

    More info here: http://stackoverflow.com/questions/30067

    Even more info: http://www.coderjournal.com/2008/12/introducing-aspnet-mvc-part-2-aspnet-mvc-vs-webforms/

  • If your developers have not yet worked on any solutions using a Model-View-Controller concept, they will find that the ASP.Net MVC Framework will have quite a bit of a steep learning curve.

    This is because it's not built on the usual code behind and .aspx physical pages that you normally use in a Web-Forms application, but everything is separated accordingly into a View, a Controller and a Model.

    If the deadline for your application is a strict one, I do not suggest that you build it with the MVC Framework, but rather stick with what the developers already know, being Web-Form based applications.

    But, if the management is willing to give time to your developers to learn about the 'new' (more like, different rather than new) concept and get comfortable with it, then Yes, by all means, go for it and develop it using the MVC Framework.

  • I made the switch from WebForms shortly after the preview releases started coming out and have been using the MVC framework ever since. It makes building and deploying work so much easier and more importantly, I think it makes for better teamwork.

    Two people can simultaneously work on the same "page" in MVC while in WebForms working two people to a page ends up being much more of a challenge. This works great for my team because my designer can lock down the page layout at the same time I am working on the logic for the view.

    It'll take a little bit of time to get used to, but once you get the mechanics of it down you'll find that it really expands capabilities of your web work very nicely.

    EDIT - One of my team members reminded me that while we use MVC as the framework, we are still barely using a tenth of what the project offers and we are still getting amazing mileage out of it. And I think somebody else mentioned it, but it deserves repeating... stackoverflow is developed on ASP.Net MVC.

  • This june I chose WebForms over MVC since MVC wasn't "mature" yet.

    Can't tell you how sorry I am now.

  • Technically, I see no issue with MVC, but one: It's beta, and some stuff is still going to change. Nothing groundbreaking like during the previews, but a little overview of what will change between the current Beta and the upcoming Release Candidate is available here.

    Legally, it's a different thing. While Microsoft explicitely allows live deployment, I am not sure if they are liable yet for any damages due to bugs, since it's beta. I know, Legal Action because of Bugs sounds silly, but some Management teams want to have a bullet proof "This software works, and if not, we pay you"-license, which is not the case with MVC yet.

Difference between Atg and Struts

What is the difference between Atg and Struts?

From stackoverflow
  • Struts is a framework for using within a J2EE web application that tries to provide web applications with an MVC-pattern based approach to coding. It includes some added utilities for form data validation etc. It is an open source project, and has been quite good at solving that particular piece of the web application puzzle, and is limited to only solving that particular peice.

    ATG (ATG Dynamo), on the other hand, is an application platform - a solution and a framework - for building data- and content- driven web applications - largely for commerce and publishing. At the framework level, it is a Java based application platform for hosting web-based applications, as well as RMI accessible business components, with an ORM layer, a component container, an MVC framework, and a set of tag libraries for JSP. The component framework (The Nucleus) is a lightweight container for managing the life cycle and dependency binding (dependency injection) of Java component objects (beans). In that sense it is somewhat similar to the Spring bean container, and is the core of the ATG framework - all other services and frameworks are hosted within it. The ORM layer framework (Repositories) maps objects to and from relational databases (as you would expect). But it can also handles mappping with LDAP, XML and file system data ources using the same consistent data access API. The JSP tags for binding form elements on a page to values on business objects etc. are more elegent and cleaner than the form binding tags in any other framework I have seen. The mechanism of writing your own tag library equivalents (Droplets) is much more consistent with the Servlet API than standard J2EE tags.

    The MVC framework (the basic Formhandler pattern) is somewhat similar to Struts Form and Action classes - but provides a much more basic framework that Struts does. Out of the box, and at the level at which most developers work, the ATG model is page-driven not controller-driven. Internally, it is certainly controller-driven with a pipeline approach to chaining dispatchers and controllers.

    In addition, the framework at the basic level gives you an RMI container, distributed caching, distributed locking and distributed singletons, distributed events and messaging, a task scheduler, a rules engine and a mechanism for defining business workflows with custom actions and outcomes, a graphical editor for business workflows, support for versioned data, support for roles and rights, logging and auditing - all out of the box, and all using very coherent and consistent APIs

    Then at the solution level, you have the components and the APIs for dealing with user profiling, identity management and personalisation, content authoring, versioning and publishing, content search, product catalogs for tangible and intangible goods, product search and guided navigation, pricing, tax calculation, promotions, shopping carts, gift lists and wish lists, payment types, shipping methods, order tracking, customer relationship management etc.

    The extension points and integration points to ATG are usually very well designed and quite well documented. They support integration with pretty much anyone who is anyone in the e-commerce and publishing space for things like authoring and content management, identity management and security, product catalogs, search and guided navigation etc. Also, almost all areas of the framework are extensible and pluggable so you can write your own components to enhance or replace the ones out of the box.

    It does not really make much sense to compare the two. However, given your question, I imagine what you are really interested in is the MVC part of ATG

    For MVC, Struts gives you more than ATG does (but then Spring MVC gives you even more than Struts does). However, you tend to get bogged down in the mechanics of the framework far more with Struts than with ATG.

    Personally, I think that ATG's formhandler based model is more elegant, cleaner and easier to work with than most other web MVC frameworks I have seen, and the APIs are more consistent with the Servlet APIs.

    Bear in mind, also, that most 'web-MVC' frameworks are not like true MVC (i.e. the pattern used for GUI programming in Smalltalk or even Java Swing etc.). Neither Struts nor ATG provide (as designed) true MVC - though ATG actually comes closer. There is a lot of confusion about terminology.

    For example,

    1. the Model in true MVC is not your data model nor your domain model objects. It is the model that represents all the data in a view. If that happens to be a domain model object then well and good - but more often than not, you will find that you need a different set of view or form objects. Also, the model is responsible for keeping itself updated - it is the Model that interacts with business services lower down. ATG tends to fuse the model and the controller into one component - the formhandler. Struts tends to keep the view data model distinct (the form object), but does not encourage its use as a model in the true MVC sense - it is not the form object that interacts with other business services to keep itself updated.

    2. the Controller in MVC is not your business controller. A controller in MVC is a conduit between the view and the model. It reacts to changes in the view, or to actions performed on the view, and instructs the model to update itself accordingly. In Struts the Controller they talk about is not an MVC controller at all - it is really a dispatcher. A lot of the code that belongs in a controller ends up in your Action class. But the way Struts is designed, the Action class is really meant to do what a Model does.

    3. the View in MVC should be populated by the model - it is a push mechanism with the model updating the view, not a pull mechanism with the view querying the model. In most web-MVC frameworks, the view (usually a JSP) pulls state from the model in order to display itself. This is particularly the case with ATG's page-driven approach. If you find that data is being fetched while your page is rendering it means something is wrong with your MVC design.

    In Struts, the function of the MVC Controller is spread across the Struts controller and the Action, while the function of the MVC Model is spread across the Form object and the Action.

    In ATG, the function of the MVC Controller and the MVC Model is all in the Formhandler

    Having said that, due to the request-response nature of HTTP, the function of a Controller in a web-MVC framework is quite limited. With web applications, we tend to get a completely updated view on form submission rather than lots of small changes (e.g. each key press or mouse click, or each changed input field) as we would with a rich UI framework. The use of AJAX is changing that - and we have to think much more about implementing MVC correctly.

    Remember, MVC is a design pattern - i.e. it is a design-time principle to be used when designing the GUI aspect of applications. Struts, and ATG are frameworks - i.e. they are classes and objects to be extended, implemented or configured when building your application. A framework cannot enforce the use of a design pattern - it can merely encourage it. Choosing to use a particular framework will not make you design your ciode better - at most it may encourage a certain discipline.

    If you design your MVC well, it will not make a huge difference whether you use Struts classes or ATG classes to implement it or whether you. Likewise, if you design your MVC badly, hoping that your choice of framework will make up for your shortfalls, it will not make a huge difference whether you use Struts or ATG. If you understand and work with the design principles, you will find it very easy to switch back and forth between frameworks.

    The best code will be that which adheres to a good design principle (say, true MVC) in the abstract, and implements it (realises it) using the right tools available in the chosen framework in the way they are intended to be used.

    Coming back to your question;

    If you are working on an ATG project, you should use the frameworks that ATG provides. It is certainly possible to shoehorn Struts into an ATG application - I have done this myself many years ago - but it is far more effort than it is worth - and you are giving up a lot of what ATG provides out of the box in terms of object lifecycle management, form data binding etc..

    If you are about to start work on a new project and have a choice of frameworks to use - I would personally recommend an open source application server (like JBoss) and the Spring Framework - it gives you the best of what ATG and Struts provide. It has a Nucleus-like component container (the Application Context), it integrates with all good ORM solutions (such as Hibernate) and includes an MVC framework that in my opinion has far eclipsed Struts. In addition, I would recommend looking at Spring WebFlow for higher level GUI flow design.

    Cameron : Wow. When I read the question, I thought "The answer to that question would be too long to write out." You proved me wrong!
  • ATG Framework have any future, developer perspective what it is its growth,If he is working on ATG

  • The main difference in the UK is that as an ATG contractor you can get £500 per day, but as a general struts guy your lucky to get £350.

    Not that I'm bitter at all.

    Miro A. : If your ATG portfolio includes Commerce Search, CSC, ACI, you can go even higher. Do not be bitter, join the eCommerce guild :-))
  • ATG is proprietary software... and resources are less ...

Using a UIImage as the application icon

Is possible to set an image as an iPhone application unique icon in code instead of adding a PNG format image file and naming it Icon.png?

From stackoverflow
  • a) No, not for developer applications. Also note that Apple's preload applications can demonstrably alter Default.png (see the Notes app) but that can't be done by the rest of us.

    b) The app icon can also be a jpeg and can be called whatever you like - you just need to set it appropriately in Info.plist

  • can state clearly how to set the Info.plist in order to call the image as the app icon

  • Here is an example of what you would add to your Info.plist:

    <key>CFBundleIconFile</key>
    <string>app.png</string>
    

    This (as well as other related options) is well documented in the iPhone Application Programming Guide.

    Note: you may need an Apple Developer account to see that link.

Extended slice that goes to beginning of sequence with negative stride

Bear with me while I explain my question. Skip down to the bold heading if you already understand extended slice list indexing.

In python, you can index lists using slice notation. Here's an example:

>>> A = list(range(10))
>>> A[0:5]
[0, 1, 2, 3, 4]

You can also include a stride, which acts like a "step":

>>> A[0:5:2]
[0, 2, 4]

The stride is also allowed to be negative, meaning the elements are retrieved in reverse order:

>>> A[5:0:-1]
[5, 4, 3, 2, 1]

But wait! I wanted to see [4, 3, 2, 1, 0]. Oh, I see, I need to decrement the start and end indices:

>>> A[4:-1:-1]
[]

What happened? It's interpreting -1 as being at the end of the array, not the beginning. I know you can achieve this as follows:

>>> A[4::-1]
[4, 3, 2, 1, 0]

But you can't use this in all cases. For example, in a method that's been passed indices.

My question is:

Is there any good pythonic way of using extended slices with negative strides and explicit start and end indices that include the first element of a sequence?

This is what I've come up with so far, but it seems unsatisfying.

>>> A[0:5][::-1]
[4, 3, 2, 1, 0]
From stackoverflow
  • a[4::-1]
    

    Example:

    Python 2.6 (r26:66714, Dec  4 2008, 11:34:15) 
    [GCC 4.0.1 (Apple Inc. build 5488)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = list(range(10))
    >>> a
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> a[4:0:-1]
    [4, 3, 2, 1]
    >>> a[4::-1]
    [4, 3, 2, 1, 0]
    >>>
    

    The reason is that the second term is interpreted as "while not index ==". Leaving it out is "while index in range".

    recursive : I mentioned that in the question. But you can't use that if you are storing your indices in variables for example.
    Charlie Martin : Oh. I knew I needed a nap.
    recursive : Yeah, I knew that too. I mean me. I need a nap.
  • I believe that the following doesn't satisfy you:

    def getReversedList(aList, end, start, step):
        if step < 0 and start == 0:
             return aList[end::step]
        return aList[end:start:step]
    

    or does it? :-)

    recursive : I had considered that, requiring two separate cases seems like it should be unnecessary. But you've given me an idea...
  • Ok, I think this is probably as good as I will get it. Thanks to Abgan for sparking the idea. This relies on the fact that None in a slice is treated as if it were a missing parameter. Anyone got anything better?

    def getReversedList(aList, end, start, step):
        return aList[end:start if start!=-1 else None:step]
    

    edit: check for start==-1, not 0

    This is still not ideal, because you're clobbering the usual behavior of -1. It seems the problem here is two overlapping definitions of what's supposed to happen. Whoever wins takes away otherwise valid invocations looking for the other intention.

    Abgan : Hah, haven't thought about that. Nice solution, worth remembering :-)
    J.F. Sebastian : You could replace `None` by `-len(a) + start`. See http://stackoverflow.com/questions/399067/extended-slice-that-goes-to-beginning-of-sequence-with-negative-stride#400118
  • [ A[b] for b in range(end,start,stride) ]
    

    Slower, however you can use negative indices, so this should work:

    [ A[b] for b in range(9, -1, -1) ]
    

    I realize this isn't using slices, but thought I'd offer the solution anyway if using slices specifically for getting the result isn't a priority.

  • But you can't use that if you are storing your indices in variables for example.

    Is this satisfactory?

    >>> a = range(10)
    >>> start = 0
    >>> end = 4
    >>> a[4:start-1 if start > 0 else None:-1]
    [4, 3, 2, 1, 0]
    
  • It is error-prone to change the semantics of start and stop. Use None or -(len(a) + 1) instead of 0 or -1. The semantics is not arbitrary. See Edsger W. Dijkstra's article "Why numbering should start at zero".

    >>> a = range(10)
    >>> start, stop, step = 4, None, -1
    

    Or

    >>> start, stop, step = 4, -(len(a) + 1), -1
    >>> a[start:stop:step]
    [4, 3, 2, 1, 0]
    

    Or

    >>> s = slice(start, stop, step)
    >>> a[s]
    [4, 3, 2, 1, 0]
    
  • As you say very few people fully understand everything that you can do with extended slicing, so unless you really need the extra performance I'd do it the "obvious" way:

    rev_subset = reversed(data[start:stop])

Profiling ClickOnce *startup* time.

I am trying to optimize startup time of one of an internal tool at my company.

It however, is deployed via ClickOnce and utilized IsolatedStorage.

I am using JetBrains dotTrace 3.1 to profile it but then it would not be able to start the application due to dependency on IsolatedStorage and ApplicationDeployment which requires that the application be run in ClickOnce context.

I am aware that I can use manual StopWatch-ing to profile everything. But I thought that I'd ask here first since I am not a regular profiler nor a performance freak. If there's a tool or tips that'd save me time then I'd love to know.

Is there a profiling tool that fully supports ClickOnce?

Or any suggestions on profiling ClickOnce startup time with JetBrains dotTrace?

From stackoverflow
  • I would create a specific version your application for profiling that can be run without ClickOnce. Hopefully, it wouldn't be too difficult to remove the isolated storage access and just return some dummy values.

    I'm not sure you could do it via ClickOnce even if you didn't have the isolated storage issue. I haven't used dotTrace but I assume it needs .pdb files to profile your code. I'd be surprised if you were deploying the .pdb files with your app.

  • You can attach to windows explorer using DotTrace 3.1. Do following steps:

    1. Run DotTrace
    2. Run task manager and kill explorer
    3. File-> Profile application -> Executable path: explorer -> click Start Application
    4. Run your click once application

    That's all :)

How to make Html rendering fast.

I am working on a web application developed on c#/asp.net. We are using third party controls for displaying Grids, Tabs, Trees and other complex controls in our pages. The problem is that these controls render a huge amount of Html. Due to this size of pages have grown heavily and browser takes a while to load a page. I want to find out some general techniques to make Html rendering in browser (IE, Firefox, etc) fast.

Note that all the pages have viewstate turned off.

From stackoverflow
  • gzip the HTML - won't increase the rendering speed, but will massively reduce the page size.

    Make sure you aren't using a table based layout, and make sure any javascript or css that's used is minified, gzipped, and linked in the head so that it can be cached.

    Umer Azaz : Gzip compression is already applied. Yes, table layout is used. Thanks for the reply.
    Rich Bradshaw : Gzip is brilliant isn't it - great for speeding things up!
  • 1) compress the HTML using a 3rd party tool or at least by using the IIS6 built-in compression option here.

    2) Evaluate the third party controls to see if they are necessary. If they are, write your own for your own needs and/or use their controls if they are "AJAX-enabled." Most popular 3rd party controls do have AJAX capabilities and would allow the data to be populated after the rest of the page loads thus showing the user some progress.

    3) Turn off ViewState if it is not needed. When using third party controls, ViewState can get huge.

    UPDATE: I've blogged about this at http://weblogs.asp.net/jgaylord/archive/2008/09/29/web-site-performance.aspx

  • I would take a look at the Viewstate of the controls on the page. You should disable it if at all possible, since it gets serialized (and Base64 encoded I think) and stuffed in the page. If your updating the data in the controls on each post-back you should be able to safely disable viewstate and likely save a good chunk of bandwidth.

    annakata : the OP says viewstate is off
  • For third-party controls, all you can do is to bug there support to improve its performance.

    But when coding you can use several techniques. One key is to understand that JavaScript DOM call are way slower than html parsing. So if you set innerHTML = " .... " with thousands of rows, it will be extremely fast compared to rendering it via document.createElement() calls in a loop.

    Here are tips from MSDN:

    http://msdn.microsoft.com/en-us/library/ms533019.aspx

    Umer Azaz : The link provided some nice tips for improving the DOM/javascript performance. Thanks.
  • There's a Firefox extension, YSlow ( http://developer.yahoo.com/yslow/ ) that analyzes any web page and lists the specific changes to be made, to improve the speed. Some of the changes that it suggests are related to the web server, not the content of the HTML, but it's very helpful anyway.
    Screenshot from the YSlow webpage: alt text

  • I would strongly suggest looking at the Yahoo CSS & Javascript compressor which will not only reduce your css & javascript file sizes but also raise any errors & possible duplication in your code. A definate must in any web developers tool box.

  • If the problem rests with the control itself, perhaps shopping around for a new vendor. I do realize this would involve a reinvestment in time and money, but I may need to be tabled for the next major revision if you can not gain the result you need with the previously mentioned compression methods.

    And remember, set EnableViewstate to false where you can

  • Open your normal pages, and type this in the URL, and press enter:

    javascript:var tags = document.getElementsByTagName('*');alert('Page weight: ' + tags.length + ' tags.');
    

    (you can even save it as a Bookmarklet)

    If you have OVER 500 tags, you likely want to look at cleaning up some of the "tag soup" where possible.

    Digg's homepage weighs in around 1,000 tags! thus is very slow to render (first time)

    MSN's homepage weighs in around 700+ tags... thus is quite slow to render

    Yahoo's homepage weighs in around 600 tags... thus renders faster

    Google's homepage weighs in around 92 tags!... thus renders like lightning!

    ojrac : http://finance.google.com/finance It rendered around 1143 tags, and it was super-fast. The script is a great trick, but I just wanted to point out that there are other factors.
    webjunkie : Yes, this is just arbitrary.
  • Compression doesn't accelerate rendering at all, it just accelerate content delivering.

    I would recomment to do some sort of 'profiling' - make test page with a lot of some kind of your html objects (like table row or some common div-container) - and then use plugins like YSlow to test how much time it takes to render for example 10K of such elements. After profiling you will see the actual rendering botleneck..

    Btw, the problem may be actually with content delivering, not rendering. YSlow will also show where it is. You also can use some visual tools to verify site loading speed, like http://Site-Perf.com/

  • To speed up rendering of grids, use grid paging.
    Grid paging will cause less rendering by reducing number of gridlines shown.

    We usually start out with 50 rows per page and always set the number of gridrows as a systemparameter which easily can be decreased or increased after deployment.

    When using standard ASP.NET controls, also found that they are faster when using CSS Friendly control adapters. CSS Friendly control adapters

C# 4.0 Dynamic features

What is the point of having a dynamic class on which you can call methods that may, or may not be there?

From stackoverflow
  • The point is that you'll usually be confident that the method will be present (or handled dynamically - e.g. a FindByAuthor method in a "book repository" class which is translated into an appropriate SQL query) but that you don't know the static type - or where the interfaces are fairly weakly typed (e.g. the Office COM APIs).

    I wouldn't expect dynamic typing to be useful very often in C# - but when it's handy, I suspect it'll be very, very handy.

  • Primarily it allows C# 4 to interop much better with objects provided by the DLR using languages like Python. It also allows much easier interop with typical COM objects without the need to create interop assemblies.

  • One reason is make it easier to use interop with COM using late-binding. So you do not have to use the interop assemblies anymore.

    This is very nice if you need to call different version of the COM server. For example when you need you application to work with different versions of Office.

  • I would certainly not use it ouside interop scenarios. In dealing with assemblies or code written in dynamic languages, it simplifies the design time experience. In these cases you have to make the assumptions about the dynamic types anyway. You will get your exception at run time if your assumptions are failing either way.

    Think of it as a shorthand for Invoke or reflection.

  • Another issue could be in writing a code that should work with some range of assembly versions. Say a plug-in where no assembly bindings are defined. And now one will need to compile same source code for a number of host assembly versions.

    In that case feature like 'duck typing' would be a good solution.

Tix and Python 3.0

Has anyone seen anything in Tix work under python 3.0? I've tried to work through the examples but when creating anything it states that cnf is unsubscriptable.

I also noticed that none of the Dir Select stuff (DirList DirTree) works under 2.6.1.

Why doesn't Python either dump Tix or support it? Its got a lot of good stuff to make easy programs.

From stackoverflow
  • Likely what happened is that no one noticed the bug. (It's very hard to automatically test GUI libraries like Tix and Tkinter.) You should report bugs as you find them to http://bugs.python.org.

    MKaras : There is a bug registered for the 2.6 release for Tix. I'm going to move back to 2.5.4, because it works.
  • Generally speaking, if you're using third-party modules, you're better off avoiding Python 3.0 for now. If you're working on a third-party module yourself, porting forward to Python 3.0 is a good idea, but for the time being general development in it is just going to be a recipe for pain.

  • See this: http://docs.python.org/3.1/library/tkinter.tix.html?highlight=tix#module-tkinter.tix

Memory Efficiency and Performance of String.Replace .NET Framework

 string str1 = "12345ABC...\\...ABC100000"; 
 // Hypothetically huge string of 100000 + Unicode Chars
 str1 = str1.Replace("1", string.Empty);
 str1 = str1.Replace("22", string.Empty);
 str1 = str1.Replace("656", string.Empty);
 str1 = str1.Replace("77ABC", string.Empty);

 // ...  this replace anti-pattern might happen with upto 50 consecutive lines of code.

 str1 = str1.Replace("ABCDEFGHIJD", string.Empty);

I have inherited some code that does the same as the snippet above. It takes a huge string and replaces (removes) constant smaller strings from large string.

I believe this is very memory intensive process given that new large immutable strings are being allocated in memory for each replace, awaitng death via the GC

1. What is the fastest way of replacing these values, ignoring memory concerns?

2. What is the most memory efficient way of achieving the same result?

I am hoping that these are the same answer!

Practical solutions that fit somewhere inbewteen these goals are also appreciated.

Assumptions:

  • All replacements are constant and known in advance
  • Underlying characters do contain some unicode [non-ascii] chars
From stackoverflow
  • StringBuilder: http://msdn.microsoft.com/en-us/library/2839d5h5.aspx

    The performance of the Replace operation itself should be roughly same as string.Replace and according to Microsoft no garbage should be produced.

    nick_alot : Thanks Andreas, can you provide a simple explanation why a SB is a better option.
    Ed Swangren : It is in effect a mutable string.
    Jon Skeet : However, to remove the first character of a string, it still needs to copy the rest into place. That's where the regex option may be better, as it may be able to just build up the final string without shuffling things around for the multiple operations.
    nick_alot : I will hopefully get some time to profile these two approaches and update this post. From experience the regex OR operator never performed that well, but maybe it's very memory efficient, which is my key concern.
  • All characters in a .NET string are "unicode chars". Do you mean they're non-ascii? That shouldn't make any odds - unless you run into composition issues, e.g. an "e + acute accent" not being replaced when you try to replace an "e acute".

    You could try using a regular expression with Regex.Replace, or StringBuilder.Replace. Here's sample code doing the same thing with both:

    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    
    class Test
    {
        static void Main(string[] args)
        {
            string original = "abcdefghijkl";
    
            Regex regex = new Regex("a|c|e|g|i|k", RegexOptions.Compiled);
    
            string removedByRegex = regex.Replace(original, "");
            string removedByStringBuilder = new StringBuilder(original)
                .Replace("a", "")
                .Replace("c", "")
                .Replace("e", "")
                .Replace("g", "")
                .Replace("i", "")
                .Replace("k", "")
                .ToString();
    
            Console.WriteLine(removedByRegex);
            Console.WriteLine(removedByStringBuilder);
        }
    }
    

    I wouldn't like to guess which is more efficient - you'd have to benchmark with your specific application. The regex way may be able to do it all in one pass, but that pass will be relatively CPU-intensive compared with each of the many replaces in StringBuilder.

    nick_alot : Thanks. To answer your question, they are non-ascii. I thought this may relevant if there was an efficient stream based solution.
    nick_alot : or more accurately, some of them are non-ascii.
    Jon Skeet : Shouldn't be relevant, beyond the possible composition issues I mentioned.
    Ahmed Said : Regex (or any regural expressions) is not prefered with large strings
    Jon Skeet : @Ahmed: I'd rather trust actual benchmarks than just received wisdom - I suspect it's *heavily* dependent on the data.
    Atømix : +1 for using `""` instead of `String.Empty` The compiler changes `""` to String.Empty anyway, and the result in this case is more readable.
    Jon Skeet : @Atømix: No, the compiler doesn't change "" to String.Empty. They *are* different things... I just happen to find "" more readable.
    Atømix : Interesting. I had learned that the compiler optimizes `""` and replaces it (an initialized empty String Object) with `String.Empty` I guess I should've checked the IL myself! This page clarified it for me: http://kossovsky.net/index.php/2009/06/string-empty-versus-empty-quotes/ I assume the guy who told me saw: "...advantages of using `""` is that it's in some cases it can be optimized by the compiler before the JIT. For example if you will run the same iteration while comparing `""==null` the whole block will be removed by the optimizer." Apparently, the compiler DOES do some optimization.
  • StringBuilder sb = new StringBuilder("Hello string");
    sb.Replace("string", String.Empty);
    Console.WriteLine(sb);
    

    StringBuilder, a mutable string.

  • if you want a built in class in dotnet i think StringBuilder is the best. to make it manully you can use unsafe code with char* and iterate through your string and replace based on your criteria

  • Since you have multiple replaces on one string, I wolud recomend you to use RegEx over StringBuilder.

    Constantin : AFAIK, you can't pass StringBuilder to Regex.Replace().
  • If you want to be really fast, and I mean really fast you'll have to look beyond the StringBuilder and just write well optimized code.

    One thing your computer doesn't like to do is branching, if you can write a replace method which operates on a fixed array (char *) and doesn't branch you have great performance.

    What you'll be doing is that the replace operation is going to search for a sequence of characters and if it finds any such sub string it will replace it. In effect you'll copy the string and when doing so, preform the find and replace.

    You'll rely on these functions for picking the index of some buffer to read/write. The goal is to preform the replace method such that when nothing has to change you write junk instead of branching.

    You should be able to complete this without a single if statement and remember to use unsafe code. Otherwise you'll be paying for index checking for every element access.

    unsafe
    {
        fixed( char * p = myStringBuffer )
        {
            // Do fancy string manipulation here
        }
    }
    

    I've written code like this in C# for fun and seen significant performance improvements, almost 300% speed up for find and replace. While the .NET BCL (base class library) performs quite well it is riddled with branching constructs and exception handling this will slow down you code if you use the built-in stuff. Also these optimizations while perfectly sound are not preformed by the JIT-compiler and you'll have to run the code as a release build without any debugger attached to be able to observe the massive performance gain.

    I could provide you with more complete code but it is a substantial amount of work. However, I can guarantee you that it will be faster than anything else suggested so far.

    Hosam Aly : I am very interested in your suggestion. Could you please share it when you have time?